Skip to content

Commit

Permalink
create a separate diagnostic rule for reportExplicitAny for backwar…
Browse files Browse the repository at this point in the history
…ds compatibility
  • Loading branch information
DetachHead committed Nov 16, 2024
1 parent 0b0e6ca commit 529bb2c
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 9 deletions.
16 changes: 15 additions & 1 deletion docs/benefits-over-pyright/new-diagnostic-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,21 @@ def foo(bar, baz: Any) -> Any:
print(baz) # no error
```

basedpyright introduces the `reportAny` option, which will report an error on usages of anything typed as `Any`.
basedpyright introduces the `reportAny` option, which will report an error on usages of anything typed as `Any`:

```py
def foo(baz: Any) -> Any:
print(baz) # error: reportAny
```

## `reportExplicitAny`

similar to [`reportAny`](#reportany), however this rule bans usages of the `Any` type itself rather than expressions that are typed as `Any`:

```py
def foo(baz: Any) -> Any: # error: reportExplicitAny
print(baz) # error: reportAny
```

## `reportIgnoreCommentWithoutRule`

Expand Down
4 changes: 3 additions & 1 deletion docs/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,9 @@ the following additional options are not available in regular pyright:

- <a name="reportUnreachable"></a> **reportUnreachable** [boolean or string, optional]: Generate or suppress diagnostics for unreachable code.

- <a name="reportAny"></a> **reportAny** [boolean or string, optional]: Ban all usages of the `Any` type. this accounts for all scenarios not covered by the `reportUnknown*` rules (since "Unknown" isn't a real type, but a distinction pyright makes to disallow the `Any` type only in certain circumstances).
- <a name="reportAny"></a> **reportAny** [boolean or string, optional]: Generate or suppress diagnostics for expressions that have the `Any` type. this accounts for all scenarios not covered by the `reportUnknown*` rules (since "Unknown" isn't a real type, but a distinction pyright makes to disallow the `Any` type only in certain circumstances).

- <a name="reportExplicitAny"></a> **reportExplicitAny** [boolean or string, optional]: Ban all explicit usages of the `Any` type. While `reportAny` bans expressions typed as `Any`, this rule bans using the `Any` type directly eg. in a type annotation.

- <a name="reportIgnoreCommentWithoutRule"></a> **reportIgnoreCommentWithoutRule** [boolean or string, optional]: Enforce that all `# type:ignore`/`# pyright:ignore` comments specify a rule in brackets (eg. `# pyright:ignore[reportAny]`)

Expand Down
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/analyzer/checker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1482,7 +1482,7 @@ export class Checker extends ParseTreeWalker {
this._reportDeprecatedUseForType(node, type);

if (type && isAny(type) && type.props?.specialForm) {
this._evaluator.addDiagnostic(DiagnosticRule.reportAny, LocMessage.explicitAny(), node);
this._evaluator.addDiagnostic(DiagnosticRule.reportExplicitAny, LocMessage.explicitAny(), node);
}

return true;
Expand Down
8 changes: 8 additions & 0 deletions packages/pyright-internal/src/common/configOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ export interface DiagnosticRuleSet {
failOnWarnings: boolean;
reportUnreachable: DiagnosticLevel;
reportAny: DiagnosticLevel;
reportExplicitAny: DiagnosticLevel;
reportIgnoreCommentWithoutRule: DiagnosticLevel;
reportPrivateLocalImportUsage: DiagnosticLevel;
reportImplicitRelativeImport: DiagnosticLevel;
Expand Down Expand Up @@ -540,6 +541,7 @@ export function getDiagLevelDiagnosticRules() {
DiagnosticRule.reportImplicitOverride,
DiagnosticRule.reportUnreachable,
DiagnosticRule.reportAny,
DiagnosticRule.reportExplicitAny,
DiagnosticRule.reportIgnoreCommentWithoutRule,
DiagnosticRule.reportInvalidCast,
DiagnosticRule.reportImplicitRelativeImport,
Expand Down Expand Up @@ -673,6 +675,7 @@ export function getOffDiagnosticRuleSet(): DiagnosticRuleSet {
failOnWarnings: false,
reportUnreachable: 'hint',
reportAny: 'none',
reportExplicitAny: 'none',
reportIgnoreCommentWithoutRule: 'none',
reportPrivateLocalImportUsage: 'none',
reportImplicitRelativeImport: 'none',
Expand Down Expand Up @@ -787,6 +790,7 @@ export function getBasicDiagnosticRuleSet(): DiagnosticRuleSet {
failOnWarnings: false,
reportUnreachable: 'hint',
reportAny: 'none',
reportExplicitAny: 'none',
reportIgnoreCommentWithoutRule: 'none',
reportPrivateLocalImportUsage: 'none',
reportImplicitRelativeImport: 'none',
Expand Down Expand Up @@ -901,6 +905,7 @@ export function getStandardDiagnosticRuleSet(): DiagnosticRuleSet {
failOnWarnings: false,
reportUnreachable: 'hint',
reportAny: 'none',
reportExplicitAny: 'none',
reportIgnoreCommentWithoutRule: 'none',
reportPrivateLocalImportUsage: 'none',
reportImplicitRelativeImport: 'none',
Expand Down Expand Up @@ -1014,6 +1019,7 @@ export const getRecommendedDiagnosticRuleSet = (): DiagnosticRuleSet => ({
failOnWarnings: true,
reportUnreachable: 'warning',
reportAny: 'warning',
reportExplicitAny: 'warning',
reportIgnoreCommentWithoutRule: 'warning',
reportPrivateLocalImportUsage: 'warning',
reportImplicitRelativeImport: 'error',
Expand Down Expand Up @@ -1124,6 +1130,7 @@ export const getAllDiagnosticRuleSet = (): DiagnosticRuleSet => ({
failOnWarnings: true,
reportUnreachable: 'error',
reportAny: 'error',
reportExplicitAny: 'error',
reportIgnoreCommentWithoutRule: 'error',
reportPrivateLocalImportUsage: 'error',
reportImplicitRelativeImport: 'error',
Expand Down Expand Up @@ -1235,6 +1242,7 @@ export function getStrictDiagnosticRuleSet(): DiagnosticRuleSet {
failOnWarnings: false,
reportUnreachable: 'hint',
reportAny: 'none',
reportExplicitAny: 'none',
reportIgnoreCommentWithoutRule: 'none',
reportPrivateLocalImportUsage: 'none',
reportImplicitRelativeImport: 'none',
Expand Down
1 change: 1 addition & 0 deletions packages/pyright-internal/src/common/diagnosticRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ export enum DiagnosticRule {
failOnWarnings = 'failOnWarnings',
reportUnreachable = 'reportUnreachable',
reportAny = 'reportAny',
reportExplicitAny = 'reportExplicitAny',
reportIgnoreCommentWithoutRule = 'reportIgnoreCommentWithoutRule',
reportPrivateLocalImportUsage = 'reportPrivateLocalImportUsage',
reportImplicitRelativeImport = 'reportImplicitRelativeImport',
Expand Down
18 changes: 14 additions & 4 deletions packages/pyright-internal/src/tests/reportAny.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ test('reportAny', () => {
validateResultsButBased(analysisResults, {
errors: [
{ line: 3, code: DiagnosticRule.reportAny, message: LocMessage.returnTypeAny() },
{ line: 3, code: DiagnosticRule.reportAny, message: LocMessage.explicitAny() },
{ line: 3, code: DiagnosticRule.reportAny, message: LocMessage.explicitAny() },
{
line: 3,
code: DiagnosticRule.reportAny,
Expand All @@ -22,7 +20,6 @@ test('reportAny', () => {
{ line: 4, code: DiagnosticRule.reportAny },
{ line: 5, code: DiagnosticRule.reportAny, message: LocMessage.returnTypeAny() },
{ line: 7, code: DiagnosticRule.reportAny, message: LocMessage.typeAny().format({ name: 'bar' }) },
{ line: 7, code: DiagnosticRule.reportAny, message: LocMessage.explicitAny() },
{
line: 9,
code: DiagnosticRule.reportAny,
Expand All @@ -43,7 +40,6 @@ test('reportAny', () => {
code: DiagnosticRule.reportAny,
message: LocMessage.lambdaReturnTypeAny(),
},
{ line: 15, code: DiagnosticRule.reportAny, message: LocMessage.explicitAny() },
{
line: 18,
code: DiagnosticRule.reportAny,
Expand All @@ -52,3 +48,17 @@ test('reportAny', () => {
],
});
});

test('reportExplicitAny', () => {
const configOptions = new ConfigOptions(Uri.empty());
configOptions.diagnosticRuleSet.reportExplicitAny = 'error';
const analysisResults = typeAnalyzeSampleFiles(['reportAny.py'], configOptions);

validateResultsButBased(analysisResults, {
errors: [3, 3, 7, 15].map((lineNumber) => ({
line: lineNumber,
code: DiagnosticRule.reportExplicitAny,
message: LocMessage.explicitAny(),
})),
});
});
19 changes: 18 additions & 1 deletion packages/vscode-pyright/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -1596,7 +1596,24 @@
"string",
"boolean"
],
"description": "Diagnostics for anything with the `Any` type",
"description": "Diagnostics for expressions with the `Any` type",
"default": "none",
"enum": [
"none",
"hint",
"information",
"warning",
"error",
true,
false
]
},
"reportExplicitAny": {
"type": [
"string",
"boolean"
],
"description": "Diagnostics for type annotations that use the `Any` type",
"default": "none",
"enum": [
"none",
Expand Down
13 changes: 12 additions & 1 deletion packages/vscode-pyright/schemas/pyrightconfig.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -512,7 +512,12 @@
},
"reportAny": {
"$ref": "#/definitions/diagnostic",
"title": "Controls reporting of values typed as `Any`",
"title": "Controls reporting of expressions typed as `Any`",
"default": "none"
},
"reportExplicitAny": {
"$ref": "#/definitions/diagnostic",
"title": "Controls reporting of type annotations that use the `Any` type",
"default": "none"
},
"reportIgnoreCommentWithoutRule": {
Expand Down Expand Up @@ -920,6 +925,9 @@
"reportAny": {
"$ref": "#/definitions/reportAny"
},
"reportExplicitAny": {
"$ref": "#/definitions/reportExplicitAny"
},
"reportIgnoreCommentWithoutRule": {
"$ref": "#/definitions/reportIgnoreCommentWithoutRule"
},
Expand Down Expand Up @@ -1268,6 +1276,9 @@
"reportAny": {
"$ref": "#/definitions/reportAny"
},
"reportExplicitAny": {
"$ref": "#/definitions/reportExplicitAny"
},
"reportIgnoreCommentWithoutRule": {
"$ref": "#/definitions/reportIgnoreCommentWithoutRule"
},
Expand Down

0 comments on commit 529bb2c

Please sign in to comment.