Added new diagnostic rule reportPossiblyUnboundVariable, which is split off from reportUnboundVariable. This addresses #6896. (#7071)

This commit is contained in:
Eric Traut 2024-01-20 23:34:11 -08:00 committed by GitHub
parent aa64fc5dec
commit 91960fba49
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 39 additions and 5 deletions

View File

@ -132,6 +132,8 @@ The following settings control pyrights diagnostic output (warnings or errors
<a name="reportOverlappingOverload"></a> **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"`. <a name="reportOverlappingOverload"></a> **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"`.
<a name="reportPossiblyUnboundVariable"></a> **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"`.
<a name="reportMissingSuperCall"></a> **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"`. <a name="reportMissingSuperCall"></a> **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"`.
<a name="reportUninitializedInstanceVariable"></a> **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"`. <a name="reportUninitializedInstanceVariable"></a> **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 pyrights diagnostic output (warnings or errors
<a name="reportUndefinedVariable"></a> **reportUndefinedVariable** [boolean or string, optional]: Generate or suppress diagnostics for undefined variables. The default value for this setting is `"error"`. <a name="reportUndefinedVariable"></a> **reportUndefinedVariable** [boolean or string, optional]: Generate or suppress diagnostics for undefined variables. The default value for this setting is `"error"`.
<a name="reportUnboundVariable"></a> **reportUnboundVariable** [boolean or string, optional]: Generate or suppress diagnostics for unbound and possibly unbound variables. The default value for this setting is `"error"`. <a name="reportUnboundVariable"></a> **reportUnboundVariable** [boolean or string, optional]: Generate or suppress diagnostics for unbound variables. The default value for this setting is `"error"`.
<a name="reportInvalidStubStatement"></a> **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"`. <a name="reportInvalidStubStatement"></a> **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" | | reportIncompatibleMethodOverride | "none" | "none" | "error" | "error" |
| reportIncompatibleVariableOverride | "none" | "none" | "error" | "error" | | reportIncompatibleVariableOverride | "none" | "none" | "error" | "error" |
| reportOverlappingOverload | "none" | "none" | "error" | "error" | | reportOverlappingOverload | "none" | "none" | "error" | "error" |
| reportPossiblyUnboundVariable | "none" | "none" | "error" | "error" |
| reportConstantRedefinition | "none" | "none" | "none" | "error" | | reportConstantRedefinition | "none" | "none" | "none" | "error" |
| reportDeprecated | "none" | "none" | "none" | "error" | | reportDeprecated | "none" | "none" | "none" | "error" |
| reportDuplicateImport | "none" | "none" | "none" | "error" | | reportDuplicateImport | "none" | "none" | "none" | "error" |

View File

@ -4266,7 +4266,7 @@ export class Checker extends ParseTreeWalker {
); );
} else if (isPossiblyUnbound(type)) { } else if (isPossiblyUnbound(type)) {
this._evaluator.addDiagnostic( this._evaluator.addDiagnostic(
DiagnosticRule.reportUnboundVariable, DiagnosticRule.reportPossiblyUnboundVariable,
LocMessage.symbolIsPossiblyUnbound().format({ name: node.value }), LocMessage.symbolIsPossiblyUnbound().format({ name: node.value }),
node node
); );

View File

@ -227,6 +227,9 @@ export interface DiagnosticRuleSet {
// incompatible return types. // incompatible return types.
reportOverlappingOverload: DiagnosticLevel; reportOverlappingOverload: DiagnosticLevel;
// Report usage of possibly unbound variables.
reportPossiblyUnboundVariable: DiagnosticLevel;
// Report failure to call super().__init__() in __init__ method. // Report failure to call super().__init__() in __init__ method.
reportMissingSuperCall: DiagnosticLevel; reportMissingSuperCall: DiagnosticLevel;
@ -291,7 +294,7 @@ export interface DiagnosticRuleSet {
// Report usage of undefined variables. // Report usage of undefined variables.
reportUndefinedVariable: DiagnosticLevel; reportUndefinedVariable: DiagnosticLevel;
// Report usage of unbound or possibly unbound variables. // Report usage of unbound variables.
reportUnboundVariable: DiagnosticLevel; reportUnboundVariable: DiagnosticLevel;
// Report statements that are syntactically correct but // Report statements that are syntactically correct but
@ -402,6 +405,7 @@ export function getDiagLevelDiagnosticRules() {
DiagnosticRule.reportIncompatibleVariableOverride, DiagnosticRule.reportIncompatibleVariableOverride,
DiagnosticRule.reportInconsistentConstructor, DiagnosticRule.reportInconsistentConstructor,
DiagnosticRule.reportOverlappingOverload, DiagnosticRule.reportOverlappingOverload,
DiagnosticRule.reportPossiblyUnboundVariable,
DiagnosticRule.reportMissingSuperCall, DiagnosticRule.reportMissingSuperCall,
DiagnosticRule.reportUninitializedInstanceVariable, DiagnosticRule.reportUninitializedInstanceVariable,
DiagnosticRule.reportInvalidStringEscapeSequence, DiagnosticRule.reportInvalidStringEscapeSequence,
@ -494,6 +498,7 @@ export function getOffDiagnosticRuleSet(): DiagnosticRuleSet {
reportIncompatibleVariableOverride: 'none', reportIncompatibleVariableOverride: 'none',
reportInconsistentConstructor: 'none', reportInconsistentConstructor: 'none',
reportOverlappingOverload: 'none', reportOverlappingOverload: 'none',
reportPossiblyUnboundVariable: 'none',
reportMissingSuperCall: 'none', reportMissingSuperCall: 'none',
reportUninitializedInstanceVariable: 'none', reportUninitializedInstanceVariable: 'none',
reportInvalidStringEscapeSequence: 'none', reportInvalidStringEscapeSequence: 'none',
@ -582,6 +587,7 @@ export function getBasicDiagnosticRuleSet(): DiagnosticRuleSet {
reportIncompatibleVariableOverride: 'none', reportIncompatibleVariableOverride: 'none',
reportInconsistentConstructor: 'none', reportInconsistentConstructor: 'none',
reportOverlappingOverload: 'none', reportOverlappingOverload: 'none',
reportPossiblyUnboundVariable: 'none',
reportMissingSuperCall: 'none', reportMissingSuperCall: 'none',
reportUninitializedInstanceVariable: 'none', reportUninitializedInstanceVariable: 'none',
reportInvalidStringEscapeSequence: 'warning', reportInvalidStringEscapeSequence: 'warning',
@ -670,6 +676,7 @@ export function getStandardDiagnosticRuleSet(): DiagnosticRuleSet {
reportIncompatibleVariableOverride: 'error', reportIncompatibleVariableOverride: 'error',
reportInconsistentConstructor: 'none', reportInconsistentConstructor: 'none',
reportOverlappingOverload: 'error', reportOverlappingOverload: 'error',
reportPossiblyUnboundVariable: 'error',
reportMissingSuperCall: 'none', reportMissingSuperCall: 'none',
reportUninitializedInstanceVariable: 'none', reportUninitializedInstanceVariable: 'none',
reportInvalidStringEscapeSequence: 'warning', reportInvalidStringEscapeSequence: 'warning',
@ -758,6 +765,7 @@ export function getStrictDiagnosticRuleSet(): DiagnosticRuleSet {
reportIncompatibleVariableOverride: 'error', reportIncompatibleVariableOverride: 'error',
reportInconsistentConstructor: 'error', reportInconsistentConstructor: 'error',
reportOverlappingOverload: 'error', reportOverlappingOverload: 'error',
reportPossiblyUnboundVariable: 'error',
reportMissingSuperCall: 'none', reportMissingSuperCall: 'none',
reportUninitializedInstanceVariable: 'none', reportUninitializedInstanceVariable: 'none',
reportInvalidStringEscapeSequence: 'error', reportInvalidStringEscapeSequence: 'error',

View File

@ -56,6 +56,7 @@ export enum DiagnosticRule {
reportIncompatibleVariableOverride = 'reportIncompatibleVariableOverride', reportIncompatibleVariableOverride = 'reportIncompatibleVariableOverride',
reportInconsistentConstructor = 'reportInconsistentConstructor', reportInconsistentConstructor = 'reportInconsistentConstructor',
reportOverlappingOverload = 'reportOverlappingOverload', reportOverlappingOverload = 'reportOverlappingOverload',
reportPossiblyUnboundVariable = 'reportPossiblyUnboundVariable',
reportMissingSuperCall = 'reportMissingSuperCall', reportMissingSuperCall = 'reportMissingSuperCall',
reportUninitializedInstanceVariable = 'reportUninitializedInstanceVariable', reportUninitializedInstanceVariable = 'reportUninitializedInstanceVariable',
reportInvalidStringEscapeSequence = 'reportInvalidStringEscapeSequence', reportInvalidStringEscapeSequence = 'reportInvalidStringEscapeSequence',

View File

@ -751,6 +751,22 @@
false 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": { "reportMissingSuperCall": {
"type": [ "type": [
"string", "string",
@ -1108,7 +1124,7 @@
"string", "string",
"boolean" "boolean"
], ],
"description": "Diagnostics for unbound and possibly unbound variables.", "description": "Diagnostics for the use of unbound variables.",
"default": "error", "default": "error",
"enum": [ "enum": [
"none", "none",

View File

@ -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", "title": "Controls reporting of function overloads that overlap in signature and obscure each other or do not agree on return type",
"default": "error" "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": { "reportMissingSuperCall": {
"$id": "#/properties/reportMissingSuperCall", "$id": "#/properties/reportMissingSuperCall",
"$ref": "#/definitions/diagnostic", "$ref": "#/definitions/diagnostic",
@ -494,7 +500,7 @@
"reportUnboundVariable": { "reportUnboundVariable": {
"$id": "#/properties/reportUnboundVariable", "$id": "#/properties/reportUnboundVariable",
"$ref": "#/definitions/diagnostic", "$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" "default": "error"
}, },
"reportUndefinedVariable": { "reportUndefinedVariable": {