From 91960fba49303f9a0fe3281d394c4d904e2d11b6 Mon Sep 17 00:00:00 2001 From: Eric Traut Date: Sat, 20 Jan 2024 23:34:11 -0800 Subject: [PATCH] Added new diagnostic rule `reportPossiblyUnboundVariable`, which is split off from `reportUnboundVariable`. This addresses #6896. (#7071) --- docs/configuration.md | 5 ++++- .../pyright-internal/src/analyzer/checker.ts | 2 +- .../src/common/configOptions.ts | 10 +++++++++- .../src/common/diagnosticRules.ts | 1 + packages/vscode-pyright/package.json | 18 +++++++++++++++++- .../schemas/pyrightconfig.schema.json | 8 +++++++- 6 files changed, 39 insertions(+), 5 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index 09bf9189f..e23d09fc0 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -132,6 +132,8 @@ The following settings control pyright’s diagnostic output (warnings or errors **reportOverlappingOverload** [boolean or string, optional]: Generate or suppress diagnostics for function overloads that overlap in signature and obscure each other or have incompatible return types. The default value for this setting is `"error"`. + **reportPossiblyUnboundVariable** [boolean or string, optional]: Generate or suppress diagnostics for variables that are possibly unbound on some code paths. The default value for this setting is `"error"`. The default value for this setting is `"error"`. + **reportMissingSuperCall** [boolean or string, optional]: Generate or suppress diagnostics for `__init__`, `__init_subclass__`, `__enter__` and `__exit__` methods in a subclass that fail to call through to the same-named method on a base class. The default value for this setting is `"none"`. **reportUninitializedInstanceVariable** [boolean or string, optional]: Generate or suppress diagnostics for instance variables within a class that are not initialized or declared within the class body or the `__init__` method. The default value for this setting is `"none"`. @@ -172,7 +174,7 @@ The following settings control pyright’s diagnostic output (warnings or errors **reportUndefinedVariable** [boolean or string, optional]: Generate or suppress diagnostics for undefined variables. The default value for this setting is `"error"`. - **reportUnboundVariable** [boolean or string, optional]: Generate or suppress diagnostics for unbound and possibly unbound variables. The default value for this setting is `"error"`. + **reportUnboundVariable** [boolean or string, optional]: Generate or suppress diagnostics for unbound variables. The default value for this setting is `"error"`. **reportInvalidStubStatement** [boolean or string, optional]: Generate or suppress diagnostics for statements that are syntactically correct but have no purpose within a type stub file. The default value for this setting is `"none"`. @@ -344,6 +346,7 @@ The following table lists the default severity levels for each diagnostic rule w | reportIncompatibleMethodOverride | "none" | "none" | "error" | "error" | | reportIncompatibleVariableOverride | "none" | "none" | "error" | "error" | | reportOverlappingOverload | "none" | "none" | "error" | "error" | +| reportPossiblyUnboundVariable | "none" | "none" | "error" | "error" | | reportConstantRedefinition | "none" | "none" | "none" | "error" | | reportDeprecated | "none" | "none" | "none" | "error" | | reportDuplicateImport | "none" | "none" | "none" | "error" | diff --git a/packages/pyright-internal/src/analyzer/checker.ts b/packages/pyright-internal/src/analyzer/checker.ts index 1d31c25ed..81d81e961 100644 --- a/packages/pyright-internal/src/analyzer/checker.ts +++ b/packages/pyright-internal/src/analyzer/checker.ts @@ -4266,7 +4266,7 @@ export class Checker extends ParseTreeWalker { ); } else if (isPossiblyUnbound(type)) { this._evaluator.addDiagnostic( - DiagnosticRule.reportUnboundVariable, + DiagnosticRule.reportPossiblyUnboundVariable, LocMessage.symbolIsPossiblyUnbound().format({ name: node.value }), node ); diff --git a/packages/pyright-internal/src/common/configOptions.ts b/packages/pyright-internal/src/common/configOptions.ts index ce712cfd7..dbfaf87a0 100644 --- a/packages/pyright-internal/src/common/configOptions.ts +++ b/packages/pyright-internal/src/common/configOptions.ts @@ -227,6 +227,9 @@ export interface DiagnosticRuleSet { // incompatible return types. reportOverlappingOverload: DiagnosticLevel; + // Report usage of possibly unbound variables. + reportPossiblyUnboundVariable: DiagnosticLevel; + // Report failure to call super().__init__() in __init__ method. reportMissingSuperCall: DiagnosticLevel; @@ -291,7 +294,7 @@ export interface DiagnosticRuleSet { // Report usage of undefined variables. reportUndefinedVariable: DiagnosticLevel; - // Report usage of unbound or possibly unbound variables. + // Report usage of unbound variables. reportUnboundVariable: DiagnosticLevel; // Report statements that are syntactically correct but @@ -402,6 +405,7 @@ export function getDiagLevelDiagnosticRules() { DiagnosticRule.reportIncompatibleVariableOverride, DiagnosticRule.reportInconsistentConstructor, DiagnosticRule.reportOverlappingOverload, + DiagnosticRule.reportPossiblyUnboundVariable, DiagnosticRule.reportMissingSuperCall, DiagnosticRule.reportUninitializedInstanceVariable, DiagnosticRule.reportInvalidStringEscapeSequence, @@ -494,6 +498,7 @@ export function getOffDiagnosticRuleSet(): DiagnosticRuleSet { reportIncompatibleVariableOverride: 'none', reportInconsistentConstructor: 'none', reportOverlappingOverload: 'none', + reportPossiblyUnboundVariable: 'none', reportMissingSuperCall: 'none', reportUninitializedInstanceVariable: 'none', reportInvalidStringEscapeSequence: 'none', @@ -582,6 +587,7 @@ export function getBasicDiagnosticRuleSet(): DiagnosticRuleSet { reportIncompatibleVariableOverride: 'none', reportInconsistentConstructor: 'none', reportOverlappingOverload: 'none', + reportPossiblyUnboundVariable: 'none', reportMissingSuperCall: 'none', reportUninitializedInstanceVariable: 'none', reportInvalidStringEscapeSequence: 'warning', @@ -670,6 +676,7 @@ export function getStandardDiagnosticRuleSet(): DiagnosticRuleSet { reportIncompatibleVariableOverride: 'error', reportInconsistentConstructor: 'none', reportOverlappingOverload: 'error', + reportPossiblyUnboundVariable: 'error', reportMissingSuperCall: 'none', reportUninitializedInstanceVariable: 'none', reportInvalidStringEscapeSequence: 'warning', @@ -758,6 +765,7 @@ export function getStrictDiagnosticRuleSet(): DiagnosticRuleSet { reportIncompatibleVariableOverride: 'error', reportInconsistentConstructor: 'error', reportOverlappingOverload: 'error', + reportPossiblyUnboundVariable: 'error', reportMissingSuperCall: 'none', reportUninitializedInstanceVariable: 'none', reportInvalidStringEscapeSequence: 'error', diff --git a/packages/pyright-internal/src/common/diagnosticRules.ts b/packages/pyright-internal/src/common/diagnosticRules.ts index 512411bda..5e80f8e7d 100644 --- a/packages/pyright-internal/src/common/diagnosticRules.ts +++ b/packages/pyright-internal/src/common/diagnosticRules.ts @@ -56,6 +56,7 @@ export enum DiagnosticRule { reportIncompatibleVariableOverride = 'reportIncompatibleVariableOverride', reportInconsistentConstructor = 'reportInconsistentConstructor', reportOverlappingOverload = 'reportOverlappingOverload', + reportPossiblyUnboundVariable = 'reportPossiblyUnboundVariable', reportMissingSuperCall = 'reportMissingSuperCall', reportUninitializedInstanceVariable = 'reportUninitializedInstanceVariable', reportInvalidStringEscapeSequence = 'reportInvalidStringEscapeSequence', diff --git a/packages/vscode-pyright/package.json b/packages/vscode-pyright/package.json index ebd10e3cd..dc58b7cf0 100644 --- a/packages/vscode-pyright/package.json +++ b/packages/vscode-pyright/package.json @@ -751,6 +751,22 @@ false ] }, + "reportPossiblyUnboundVariable": { + "type": [ + "string", + "boolean" + ], + "description": "Diagnostics for the use of variables that may be unbound on some code paths.", + "default": "error", + "enum": [ + "none", + "information", + "warning", + "error", + true, + false + ] + }, "reportMissingSuperCall": { "type": [ "string", @@ -1108,7 +1124,7 @@ "string", "boolean" ], - "description": "Diagnostics for unbound and possibly unbound variables.", + "description": "Diagnostics for the use of unbound variables.", "default": "error", "enum": [ "none", diff --git a/packages/vscode-pyright/schemas/pyrightconfig.schema.json b/packages/vscode-pyright/schemas/pyrightconfig.schema.json index b55ea1f41..1f35ba6b5 100644 --- a/packages/vscode-pyright/schemas/pyrightconfig.schema.json +++ b/packages/vscode-pyright/schemas/pyrightconfig.schema.json @@ -377,6 +377,12 @@ "title": "Controls reporting of function overloads that overlap in signature and obscure each other or do not agree on return type", "default": "error" }, + "reportPossiblyUnboundVariable": { + "$id": "#/properties/reportPossiblyUnboundVariable", + "$ref": "#/definitions/diagnostic", + "title": "Controls reporting of attempts to use variable that is possibly unbound on some code paths", + "default": "error" + }, "reportMissingSuperCall": { "$id": "#/properties/reportMissingSuperCall", "$ref": "#/definitions/diagnostic", @@ -494,7 +500,7 @@ "reportUnboundVariable": { "$id": "#/properties/reportUnboundVariable", "$ref": "#/definitions/diagnostic", - "title": "Controls reporting of attempts to use an unbound or possibly unbound variable", + "title": "Controls reporting of attempts to use an unbound variable", "default": "error" }, "reportUndefinedVariable": {