From 6a042f7b5826054bd7be91677e00a47b662e4d3b Mon Sep 17 00:00:00 2001 From: Eric Traut Date: Tue, 12 Mar 2024 00:34:58 -0600 Subject: [PATCH] Added new `reportUnhashable` diagnostic rule. This addresses #7462. (#7464) --- docs/configuration.md | 3 +++ .../src/analyzer/typeEvaluator.ts | 2 +- .../pyright-internal/src/common/configOptions.ts | 8 ++++++++ .../src/common/diagnosticRules.ts | 1 + packages/vscode-pyright/package.json | 16 ++++++++++++++++ .../schemas/pyrightconfig.schema.json | 6 ++++++ 6 files changed, 35 insertions(+), 1 deletion(-) diff --git a/docs/configuration.md b/docs/configuration.md index a7a299773..ee74de876 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -200,6 +200,8 @@ The following settings control pyright’s diagnostic output (warnings or errors **reportUnboundVariable** [boolean or string, optional]: Generate or suppress diagnostics for unbound variables. The default value for this setting is `"error"`. + **reportUnhashable** [boolean or string, optional]: Generate or suppress diagnostics for the use of an unhashable object in a container that requires hashability. + **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"`. **reportIncompleteStub** [boolean or string, optional]: Generate or suppress diagnostics for a module-level `__getattr__` call in a type stub file, indicating that it is incomplete. The default value for this setting is `"none"`. @@ -376,6 +378,7 @@ The following table lists the default severity levels for each diagnostic rule w | reportTypedDictNotRequiredAccess | "none" | "error" | "error" | "error" | | reportPrivateImportUsage | "none" | "error" | "error" | "error" | | reportUnboundVariable | "none" | "error" | "error" | "error" | +| reportUnhashable | "none" | "error" | "error" | "error" | | reportUnusedCoroutine | "none" | "error" | "error" | "error" | | reportUnusedExcept | "none" | "error" | "error" | "error" | | reportFunctionMemberAccess | "none" | "none" | "error" | "error" | diff --git a/packages/pyright-internal/src/analyzer/typeEvaluator.ts b/packages/pyright-internal/src/analyzer/typeEvaluator.ts index 9b5012f88..5b05c797e 100644 --- a/packages/pyright-internal/src/analyzer/typeEvaluator.ts +++ b/packages/pyright-internal/src/analyzer/typeEvaluator.ts @@ -13789,7 +13789,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions const message = isDictKey ? LocMessage.unhashableDictKey() : LocMessage.unhashableSetEntry(); - addDiagnostic(DiagnosticRule.reportGeneralTypeIssues, message + diag.getString(), entry); + addDiagnostic(DiagnosticRule.reportUnhashable, message + diag.getString(), entry); } } diff --git a/packages/pyright-internal/src/common/configOptions.ts b/packages/pyright-internal/src/common/configOptions.ts index d96140beb..95381a3fe 100644 --- a/packages/pyright-internal/src/common/configOptions.ts +++ b/packages/pyright-internal/src/common/configOptions.ts @@ -333,6 +333,9 @@ export interface DiagnosticRuleSet { // Report usage of unbound variables. reportUnboundVariable: DiagnosticLevel; + // Report use of unhashable type in a dictionary. + reportUnhashable: DiagnosticLevel; + // Report statements that are syntactically correct but // have no semantic meaning within a type stub file. reportInvalidStubStatement: DiagnosticLevel; @@ -474,6 +477,7 @@ export function getDiagLevelDiagnosticRules() { DiagnosticRule.reportSelfClsParameterName, DiagnosticRule.reportImplicitStringConcatenation, DiagnosticRule.reportUndefinedVariable, + DiagnosticRule.reportUnhashable, DiagnosticRule.reportUnboundVariable, DiagnosticRule.reportInvalidStubStatement, DiagnosticRule.reportIncompleteStub, @@ -579,6 +583,7 @@ export function getOffDiagnosticRuleSet(): DiagnosticRuleSet { reportSelfClsParameterName: 'none', reportImplicitStringConcatenation: 'none', reportUnboundVariable: 'none', + reportUnhashable: 'none', reportUndefinedVariable: 'warning', reportInvalidStubStatement: 'none', reportIncompleteStub: 'none', @@ -680,6 +685,7 @@ export function getBasicDiagnosticRuleSet(): DiagnosticRuleSet { reportSelfClsParameterName: 'warning', reportImplicitStringConcatenation: 'none', reportUnboundVariable: 'error', + reportUnhashable: 'error', reportUndefinedVariable: 'error', reportInvalidStubStatement: 'none', reportIncompleteStub: 'none', @@ -781,6 +787,7 @@ export function getStandardDiagnosticRuleSet(): DiagnosticRuleSet { reportSelfClsParameterName: 'warning', reportImplicitStringConcatenation: 'none', reportUnboundVariable: 'error', + reportUnhashable: 'error', reportUndefinedVariable: 'error', reportInvalidStubStatement: 'none', reportIncompleteStub: 'none', @@ -882,6 +889,7 @@ export function getStrictDiagnosticRuleSet(): DiagnosticRuleSet { reportSelfClsParameterName: 'error', reportImplicitStringConcatenation: 'none', reportUnboundVariable: 'error', + reportUnhashable: 'error', reportUndefinedVariable: 'error', reportInvalidStubStatement: 'error', reportIncompleteStub: 'error', diff --git a/packages/pyright-internal/src/common/diagnosticRules.ts b/packages/pyright-internal/src/common/diagnosticRules.ts index 6d49e8703..0201e93ca 100644 --- a/packages/pyright-internal/src/common/diagnosticRules.ts +++ b/packages/pyright-internal/src/common/diagnosticRules.ts @@ -90,6 +90,7 @@ export enum DiagnosticRule { reportImplicitStringConcatenation = 'reportImplicitStringConcatenation', reportUndefinedVariable = 'reportUndefinedVariable', reportUnboundVariable = 'reportUnboundVariable', + reportUnhashable = 'reportUnhashable', reportInvalidStubStatement = 'reportInvalidStubStatement', reportIncompleteStub = 'reportIncompleteStub', reportUnsupportedDunderAll = 'reportUnsupportedDunderAll', diff --git a/packages/vscode-pyright/package.json b/packages/vscode-pyright/package.json index fe6913aca..bf560a8f3 100644 --- a/packages/vscode-pyright/package.json +++ b/packages/vscode-pyright/package.json @@ -1327,6 +1327,22 @@ false ] }, + "reportUnhashable": { + "type": [ + "string", + "boolean" + ], + "description": "Diagnostics for the use of an unhashable object in a container that requires hashability.", + "default": "error", + "enum": [ + "none", + "information", + "warning", + "error", + true, + false + ] + }, "reportUnsupportedDunderAll": { "type": [ "string", diff --git a/packages/vscode-pyright/schemas/pyrightconfig.schema.json b/packages/vscode-pyright/schemas/pyrightconfig.schema.json index 831583b45..c23d7a8e1 100644 --- a/packages/vscode-pyright/schemas/pyrightconfig.schema.json +++ b/packages/vscode-pyright/schemas/pyrightconfig.schema.json @@ -575,6 +575,12 @@ "title": "Controls reporting of attempts to use an unbound variable", "default": "error" }, + "reportUnhashable": { + "$id": "#/properties/reportUnhashable", + "$ref": "#/definitions/diagnostic", + "title": "Controls reporting of unhashable object in container that requires hashability", + "default": "error" + }, "reportUndefinedVariable": { "$id": "#/properties/reportUndefinedVariable", "$ref": "#/definitions/diagnostic",