Added two new diagnostic rules: reportAssertTypeFailure for type mismatches detected by typing.assert_type and reportUnusedExcept for situations where an except statement is determined to be unreachable. (#7070)

This commit is contained in:
Eric Traut 2024-01-20 22:21:42 -08:00 committed by GitHub
parent 04e0536a52
commit aa64fc5dec
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 70 additions and 2 deletions

View File

@ -90,6 +90,8 @@ The following settings control pyrights diagnostic output (warnings or errors
<a name="reportWildcardImportFromLibrary"></a> **reportWildcardImportFromLibrary** [boolean or string, optional]: Generate or suppress diagnostics for a wildcard import from an external library. The use of this language feature is highly discouraged and can result in bugs when the library is updated. The default value for this setting is `"warning"`.
<a name="reportAssertTypeFailure"></a> **reportAssertTypeFailure** [boolean or string, optional]: Generate or suppress diagnostics for a type mismatch detected by the `typing.assert_type` call. The default value for this setting is `"error"`.
<a name="reportOptionalSubscript"></a> **reportOptionalSubscript** [boolean or string, optional]: Generate or suppress diagnostics for an attempt to subscript (index) a variable with an Optional type. The default value for this setting is `"error"`.
<a name="reportOptionalMemberAccess"></a> **reportOptionalMemberAccess** [boolean or string, optional]: Generate or suppress diagnostics for an attempt to access a member of a variable with an Optional type. The default value for this setting is `"error"`.
@ -182,6 +184,8 @@ The following settings control pyrights diagnostic output (warnings or errors
<a name="reportUnusedCoroutine"></a> **reportUnusedCoroutine** [boolean or string, optional]: Generate or suppress diagnostics for call statements whose return value is not used in any way and is a Coroutine. This identifies a common error where an `await` keyword is mistakenly omitted. The default value for this setting is `"error"`.
<a name="reportUnusedExcept"></a> **reportUnusedExcept** [boolean or string, optional]: Generate or suppress diagnostics for an `except` clause that will never be reached. The default value for this setting is `"error"`.
<a name="reportUnusedExpression"></a> **reportUnusedExpression** [boolean or string, optional]: Generate or suppress diagnostics for simple expressions whose results are not used in any way. The default value for this setting is `"none"`.
<a name="reportUnnecessaryTypeIgnoreComment"></a> **reportUnnecessaryTypeIgnoreComment** [boolean or string, optional]: Generate or suppress diagnostics for a `# type: ignore` or `# pyright: ignore` comment that would have no effect if removed. The default value for this setting is `"none"`.
@ -323,6 +327,7 @@ The following table lists the default severity levels for each diagnostic rule w
| reportUnsupportedDunderAll | "none" | "warning" | "warning" | "error" |
| reportUnusedExpression | "none" | "warning" | "warning" | "error" |
| reportWildcardImportFromLibrary | "none" | "warning" | "warning" | "error" |
| reportAssertTypeFailure | "none" | "error" | "error" | "error" |
| reportGeneralTypeIssues | "none" | "error" | "error" | "error" |
| reportOptionalSubscript | "none" | "error" | "error" | "error" |
| reportOptionalMemberAccess | "none" | "error" | "error" | "error" |
@ -334,6 +339,7 @@ The following table lists the default severity levels for each diagnostic rule w
| reportPrivateImportUsage | "none" | "error" | "error" | "error" |
| reportUnboundVariable | "none" | "error" | "error" | "error" |
| reportUnusedCoroutine | "none" | "error" | "error" | "error" |
| reportUnusedExcept | "none" | "error" | "error" | "error" |
| reportFunctionMemberAccess | "none" | "none" | "error" | "error" |
| reportIncompatibleMethodOverride | "none" | "none" | "error" | "error" |
| reportIncompatibleVariableOverride | "none" | "none" | "error" | "error" |

View File

@ -6902,7 +6902,7 @@ export class Checker extends ParseTreeWalker {
// Were all of the exception types overridden?
if (typesOfThisExcept.length > 0 && typesOfThisExcept.length === overriddenExceptionCount) {
this._evaluator.addDiagnostic(
DiagnosticRule.reportGeneralTypeIssues,
DiagnosticRule.reportUnusedExcept,
LocMessage.unreachableExcept() + diagAddendum.getString(),
except.typeExpression
);

View File

@ -8113,7 +8113,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
const srcDestTypes = printSrcDestTypes(arg0TypeResult.type, assertedType, { expandTypeAlias: true });
addDiagnostic(
DiagnosticRule.reportGeneralTypeIssues,
DiagnosticRule.reportAssertTypeFailure,
LocMessage.assertTypeTypeMismatch().format({
expected: srcDestTypes.destType,
received: srcDestTypes.sourceType,

View File

@ -159,6 +159,9 @@ export interface DiagnosticRuleSet {
// Report use of wildcard import for non-local imports?
reportWildcardImportFromLibrary: DiagnosticLevel;
// Report failure of assert_type call?
reportAssertTypeFailure: DiagnosticLevel;
// Report attempts to subscript (index) an Optional type?
reportOptionalSubscript: DiagnosticLevel;
@ -310,6 +313,9 @@ export interface DiagnosticRuleSet {
// and is not used in any way.
reportUnusedCoroutine: DiagnosticLevel;
// Report except clause that is unreachable.
reportUnusedExcept: DiagnosticLevel;
// Report cases where a simple expression result is not used in any way.
reportUnusedExpression: DiagnosticLevel;
@ -375,6 +381,7 @@ export function getDiagLevelDiagnosticRules() {
DiagnosticRule.reportUnusedVariable,
DiagnosticRule.reportDuplicateImport,
DiagnosticRule.reportWildcardImportFromLibrary,
DiagnosticRule.reportAssertTypeFailure,
DiagnosticRule.reportOptionalSubscript,
DiagnosticRule.reportOptionalMemberAccess,
DiagnosticRule.reportOptionalCall,
@ -421,6 +428,7 @@ export function getDiagLevelDiagnosticRules() {
DiagnosticRule.reportUnsupportedDunderAll,
DiagnosticRule.reportUnusedCallResult,
DiagnosticRule.reportUnusedCoroutine,
DiagnosticRule.reportUnusedExcept,
DiagnosticRule.reportUnusedExpression,
DiagnosticRule.reportUnnecessaryTypeIgnoreComment,
DiagnosticRule.reportMatchNotExhaustive,
@ -465,6 +473,7 @@ export function getOffDiagnosticRuleSet(): DiagnosticRuleSet {
reportUnusedVariable: 'none',
reportDuplicateImport: 'none',
reportWildcardImportFromLibrary: 'none',
reportAssertTypeFailure: 'none',
reportOptionalSubscript: 'none',
reportOptionalMemberAccess: 'none',
reportOptionalCall: 'none',
@ -511,6 +520,7 @@ export function getOffDiagnosticRuleSet(): DiagnosticRuleSet {
reportUnsupportedDunderAll: 'none',
reportUnusedCallResult: 'none',
reportUnusedCoroutine: 'none',
reportUnusedExcept: 'none',
reportUnusedExpression: 'none',
reportUnnecessaryTypeIgnoreComment: 'none',
reportMatchNotExhaustive: 'none',
@ -551,6 +561,7 @@ export function getBasicDiagnosticRuleSet(): DiagnosticRuleSet {
reportUnusedVariable: 'none',
reportDuplicateImport: 'none',
reportWildcardImportFromLibrary: 'warning',
reportAssertTypeFailure: 'error',
reportOptionalSubscript: 'error',
reportOptionalMemberAccess: 'error',
reportOptionalCall: 'error',
@ -597,6 +608,7 @@ export function getBasicDiagnosticRuleSet(): DiagnosticRuleSet {
reportUnsupportedDunderAll: 'warning',
reportUnusedCallResult: 'none',
reportUnusedCoroutine: 'error',
reportUnusedExcept: 'error',
reportUnusedExpression: 'warning',
reportUnnecessaryTypeIgnoreComment: 'none',
reportMatchNotExhaustive: 'none',
@ -637,6 +649,7 @@ export function getStandardDiagnosticRuleSet(): DiagnosticRuleSet {
reportUnusedVariable: 'none',
reportDuplicateImport: 'none',
reportWildcardImportFromLibrary: 'warning',
reportAssertTypeFailure: 'error',
reportOptionalSubscript: 'error',
reportOptionalMemberAccess: 'error',
reportOptionalCall: 'error',
@ -683,6 +696,7 @@ export function getStandardDiagnosticRuleSet(): DiagnosticRuleSet {
reportUnsupportedDunderAll: 'warning',
reportUnusedCallResult: 'none',
reportUnusedCoroutine: 'error',
reportUnusedExcept: 'error',
reportUnusedExpression: 'warning',
reportUnnecessaryTypeIgnoreComment: 'none',
reportMatchNotExhaustive: 'none',
@ -723,6 +737,7 @@ export function getStrictDiagnosticRuleSet(): DiagnosticRuleSet {
reportUnusedVariable: 'error',
reportDuplicateImport: 'error',
reportWildcardImportFromLibrary: 'error',
reportAssertTypeFailure: 'error',
reportOptionalSubscript: 'error',
reportOptionalMemberAccess: 'error',
reportOptionalCall: 'error',
@ -769,6 +784,7 @@ export function getStrictDiagnosticRuleSet(): DiagnosticRuleSet {
reportUnsupportedDunderAll: 'error',
reportUnusedCallResult: 'none',
reportUnusedCoroutine: 'error',
reportUnusedExcept: 'error',
reportUnusedExpression: 'error',
reportUnnecessaryTypeIgnoreComment: 'none',
reportMatchNotExhaustive: 'error',

View File

@ -35,6 +35,7 @@ export enum DiagnosticRule {
reportUnusedVariable = 'reportUnusedVariable',
reportDuplicateImport = 'reportDuplicateImport',
reportWildcardImportFromLibrary = 'reportWildcardImportFromLibrary',
reportAssertTypeFailure = 'reportAssertTypeFailure',
reportOptionalSubscript = 'reportOptionalSubscript',
reportOptionalMemberAccess = 'reportOptionalMemberAccess',
reportOptionalCall = 'reportOptionalCall',
@ -81,6 +82,7 @@ export enum DiagnosticRule {
reportUnsupportedDunderAll = 'reportUnsupportedDunderAll',
reportUnusedCallResult = 'reportUnusedCallResult',
reportUnusedCoroutine = 'reportUnusedCoroutine',
reportUnusedExcept = 'reportUnusedExcept',
reportUnusedExpression = 'reportUnusedExpression',
reportUnnecessaryTypeIgnoreComment = 'reportUnnecessaryTypeIgnoreComment',
reportMatchNotExhaustive = 'reportMatchNotExhaustive',

View File

@ -415,6 +415,22 @@
false
]
},
"reportAssertTypeFailure": {
"type": [
"string",
"boolean"
],
"description": "Diagnostics for a type mismatch detected by a typing.assert_type call.",
"default": "error",
"enum": [
"none",
"information",
"warning",
"error",
true,
false
]
},
"reportOptionalSubscript": {
"type": [
"string",
@ -1151,6 +1167,22 @@
false
]
},
"reportUnusedExcept": {
"type": [
"string",
"boolean"
],
"description": "Diagnostics for unreachable except clause.",
"default": "error",
"enum": [
"none",
"information",
"warning",
"error",
true,
false
]
},
"reportUnusedExpression": {
"type": [
"string",

View File

@ -251,6 +251,12 @@
"title": "Controls reporting of wildcard import from external library",
"default": "warning"
},
"reportAssertTypeFailure": {
"$id": "#/properties/reportAssertTypeFailure",
"$ref": "#/definitions/diagnostic",
"title": "Controls reporting of type mismatch detected by typing.assert_type call",
"default": "error"
},
"reportOptionalSubscript": {
"$id": "#/properties/reportOptionalSubscript",
"$ref": "#/definitions/diagnostic",
@ -527,6 +533,12 @@
"title": "Controls reporting of call expressions that returns Coroutine whose results are not consumed",
"default": "error"
},
"reportUnusedExcept": {
"$id": "#/properties/reportUnusedExcept",
"$ref": "#/definitions/diagnostic",
"title": "Controls reporting of unreachable except clauses",
"default": "error"
},
"reportUnusedExpression": {
"$id": "#/properties/reportUnusedExpression",
"$ref": "#/definitions/diagnostic",