mirror of
https://github.com/microsoft/pyright.git
synced 2024-10-26 19:01:08 +03:00
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:
parent
04e0536a52
commit
aa64fc5dec
@ -90,6 +90,8 @@ The following settings control pyright’s 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="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="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"`.
|
<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 pyright’s 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="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="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"`.
|
<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" |
|
| reportUnsupportedDunderAll | "none" | "warning" | "warning" | "error" |
|
||||||
| reportUnusedExpression | "none" | "warning" | "warning" | "error" |
|
| reportUnusedExpression | "none" | "warning" | "warning" | "error" |
|
||||||
| reportWildcardImportFromLibrary | "none" | "warning" | "warning" | "error" |
|
| reportWildcardImportFromLibrary | "none" | "warning" | "warning" | "error" |
|
||||||
|
| reportAssertTypeFailure | "none" | "error" | "error" | "error" |
|
||||||
| reportGeneralTypeIssues | "none" | "error" | "error" | "error" |
|
| reportGeneralTypeIssues | "none" | "error" | "error" | "error" |
|
||||||
| reportOptionalSubscript | "none" | "error" | "error" | "error" |
|
| reportOptionalSubscript | "none" | "error" | "error" | "error" |
|
||||||
| reportOptionalMemberAccess | "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" |
|
| reportPrivateImportUsage | "none" | "error" | "error" | "error" |
|
||||||
| reportUnboundVariable | "none" | "error" | "error" | "error" |
|
| reportUnboundVariable | "none" | "error" | "error" | "error" |
|
||||||
| reportUnusedCoroutine | "none" | "error" | "error" | "error" |
|
| reportUnusedCoroutine | "none" | "error" | "error" | "error" |
|
||||||
|
| reportUnusedExcept | "none" | "error" | "error" | "error" |
|
||||||
| reportFunctionMemberAccess | "none" | "none" | "error" | "error" |
|
| reportFunctionMemberAccess | "none" | "none" | "error" | "error" |
|
||||||
| reportIncompatibleMethodOverride | "none" | "none" | "error" | "error" |
|
| reportIncompatibleMethodOverride | "none" | "none" | "error" | "error" |
|
||||||
| reportIncompatibleVariableOverride | "none" | "none" | "error" | "error" |
|
| reportIncompatibleVariableOverride | "none" | "none" | "error" | "error" |
|
||||||
|
@ -6902,7 +6902,7 @@ export class Checker extends ParseTreeWalker {
|
|||||||
// Were all of the exception types overridden?
|
// Were all of the exception types overridden?
|
||||||
if (typesOfThisExcept.length > 0 && typesOfThisExcept.length === overriddenExceptionCount) {
|
if (typesOfThisExcept.length > 0 && typesOfThisExcept.length === overriddenExceptionCount) {
|
||||||
this._evaluator.addDiagnostic(
|
this._evaluator.addDiagnostic(
|
||||||
DiagnosticRule.reportGeneralTypeIssues,
|
DiagnosticRule.reportUnusedExcept,
|
||||||
LocMessage.unreachableExcept() + diagAddendum.getString(),
|
LocMessage.unreachableExcept() + diagAddendum.getString(),
|
||||||
except.typeExpression
|
except.typeExpression
|
||||||
);
|
);
|
||||||
|
@ -8113,7 +8113,7 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
|
|||||||
const srcDestTypes = printSrcDestTypes(arg0TypeResult.type, assertedType, { expandTypeAlias: true });
|
const srcDestTypes = printSrcDestTypes(arg0TypeResult.type, assertedType, { expandTypeAlias: true });
|
||||||
|
|
||||||
addDiagnostic(
|
addDiagnostic(
|
||||||
DiagnosticRule.reportGeneralTypeIssues,
|
DiagnosticRule.reportAssertTypeFailure,
|
||||||
LocMessage.assertTypeTypeMismatch().format({
|
LocMessage.assertTypeTypeMismatch().format({
|
||||||
expected: srcDestTypes.destType,
|
expected: srcDestTypes.destType,
|
||||||
received: srcDestTypes.sourceType,
|
received: srcDestTypes.sourceType,
|
||||||
|
@ -159,6 +159,9 @@ export interface DiagnosticRuleSet {
|
|||||||
// Report use of wildcard import for non-local imports?
|
// Report use of wildcard import for non-local imports?
|
||||||
reportWildcardImportFromLibrary: DiagnosticLevel;
|
reportWildcardImportFromLibrary: DiagnosticLevel;
|
||||||
|
|
||||||
|
// Report failure of assert_type call?
|
||||||
|
reportAssertTypeFailure: DiagnosticLevel;
|
||||||
|
|
||||||
// Report attempts to subscript (index) an Optional type?
|
// Report attempts to subscript (index) an Optional type?
|
||||||
reportOptionalSubscript: DiagnosticLevel;
|
reportOptionalSubscript: DiagnosticLevel;
|
||||||
|
|
||||||
@ -310,6 +313,9 @@ export interface DiagnosticRuleSet {
|
|||||||
// and is not used in any way.
|
// and is not used in any way.
|
||||||
reportUnusedCoroutine: DiagnosticLevel;
|
reportUnusedCoroutine: DiagnosticLevel;
|
||||||
|
|
||||||
|
// Report except clause that is unreachable.
|
||||||
|
reportUnusedExcept: DiagnosticLevel;
|
||||||
|
|
||||||
// Report cases where a simple expression result is not used in any way.
|
// Report cases where a simple expression result is not used in any way.
|
||||||
reportUnusedExpression: DiagnosticLevel;
|
reportUnusedExpression: DiagnosticLevel;
|
||||||
|
|
||||||
@ -375,6 +381,7 @@ export function getDiagLevelDiagnosticRules() {
|
|||||||
DiagnosticRule.reportUnusedVariable,
|
DiagnosticRule.reportUnusedVariable,
|
||||||
DiagnosticRule.reportDuplicateImport,
|
DiagnosticRule.reportDuplicateImport,
|
||||||
DiagnosticRule.reportWildcardImportFromLibrary,
|
DiagnosticRule.reportWildcardImportFromLibrary,
|
||||||
|
DiagnosticRule.reportAssertTypeFailure,
|
||||||
DiagnosticRule.reportOptionalSubscript,
|
DiagnosticRule.reportOptionalSubscript,
|
||||||
DiagnosticRule.reportOptionalMemberAccess,
|
DiagnosticRule.reportOptionalMemberAccess,
|
||||||
DiagnosticRule.reportOptionalCall,
|
DiagnosticRule.reportOptionalCall,
|
||||||
@ -421,6 +428,7 @@ export function getDiagLevelDiagnosticRules() {
|
|||||||
DiagnosticRule.reportUnsupportedDunderAll,
|
DiagnosticRule.reportUnsupportedDunderAll,
|
||||||
DiagnosticRule.reportUnusedCallResult,
|
DiagnosticRule.reportUnusedCallResult,
|
||||||
DiagnosticRule.reportUnusedCoroutine,
|
DiagnosticRule.reportUnusedCoroutine,
|
||||||
|
DiagnosticRule.reportUnusedExcept,
|
||||||
DiagnosticRule.reportUnusedExpression,
|
DiagnosticRule.reportUnusedExpression,
|
||||||
DiagnosticRule.reportUnnecessaryTypeIgnoreComment,
|
DiagnosticRule.reportUnnecessaryTypeIgnoreComment,
|
||||||
DiagnosticRule.reportMatchNotExhaustive,
|
DiagnosticRule.reportMatchNotExhaustive,
|
||||||
@ -465,6 +473,7 @@ export function getOffDiagnosticRuleSet(): DiagnosticRuleSet {
|
|||||||
reportUnusedVariable: 'none',
|
reportUnusedVariable: 'none',
|
||||||
reportDuplicateImport: 'none',
|
reportDuplicateImport: 'none',
|
||||||
reportWildcardImportFromLibrary: 'none',
|
reportWildcardImportFromLibrary: 'none',
|
||||||
|
reportAssertTypeFailure: 'none',
|
||||||
reportOptionalSubscript: 'none',
|
reportOptionalSubscript: 'none',
|
||||||
reportOptionalMemberAccess: 'none',
|
reportOptionalMemberAccess: 'none',
|
||||||
reportOptionalCall: 'none',
|
reportOptionalCall: 'none',
|
||||||
@ -511,6 +520,7 @@ export function getOffDiagnosticRuleSet(): DiagnosticRuleSet {
|
|||||||
reportUnsupportedDunderAll: 'none',
|
reportUnsupportedDunderAll: 'none',
|
||||||
reportUnusedCallResult: 'none',
|
reportUnusedCallResult: 'none',
|
||||||
reportUnusedCoroutine: 'none',
|
reportUnusedCoroutine: 'none',
|
||||||
|
reportUnusedExcept: 'none',
|
||||||
reportUnusedExpression: 'none',
|
reportUnusedExpression: 'none',
|
||||||
reportUnnecessaryTypeIgnoreComment: 'none',
|
reportUnnecessaryTypeIgnoreComment: 'none',
|
||||||
reportMatchNotExhaustive: 'none',
|
reportMatchNotExhaustive: 'none',
|
||||||
@ -551,6 +561,7 @@ export function getBasicDiagnosticRuleSet(): DiagnosticRuleSet {
|
|||||||
reportUnusedVariable: 'none',
|
reportUnusedVariable: 'none',
|
||||||
reportDuplicateImport: 'none',
|
reportDuplicateImport: 'none',
|
||||||
reportWildcardImportFromLibrary: 'warning',
|
reportWildcardImportFromLibrary: 'warning',
|
||||||
|
reportAssertTypeFailure: 'error',
|
||||||
reportOptionalSubscript: 'error',
|
reportOptionalSubscript: 'error',
|
||||||
reportOptionalMemberAccess: 'error',
|
reportOptionalMemberAccess: 'error',
|
||||||
reportOptionalCall: 'error',
|
reportOptionalCall: 'error',
|
||||||
@ -597,6 +608,7 @@ export function getBasicDiagnosticRuleSet(): DiagnosticRuleSet {
|
|||||||
reportUnsupportedDunderAll: 'warning',
|
reportUnsupportedDunderAll: 'warning',
|
||||||
reportUnusedCallResult: 'none',
|
reportUnusedCallResult: 'none',
|
||||||
reportUnusedCoroutine: 'error',
|
reportUnusedCoroutine: 'error',
|
||||||
|
reportUnusedExcept: 'error',
|
||||||
reportUnusedExpression: 'warning',
|
reportUnusedExpression: 'warning',
|
||||||
reportUnnecessaryTypeIgnoreComment: 'none',
|
reportUnnecessaryTypeIgnoreComment: 'none',
|
||||||
reportMatchNotExhaustive: 'none',
|
reportMatchNotExhaustive: 'none',
|
||||||
@ -637,6 +649,7 @@ export function getStandardDiagnosticRuleSet(): DiagnosticRuleSet {
|
|||||||
reportUnusedVariable: 'none',
|
reportUnusedVariable: 'none',
|
||||||
reportDuplicateImport: 'none',
|
reportDuplicateImport: 'none',
|
||||||
reportWildcardImportFromLibrary: 'warning',
|
reportWildcardImportFromLibrary: 'warning',
|
||||||
|
reportAssertTypeFailure: 'error',
|
||||||
reportOptionalSubscript: 'error',
|
reportOptionalSubscript: 'error',
|
||||||
reportOptionalMemberAccess: 'error',
|
reportOptionalMemberAccess: 'error',
|
||||||
reportOptionalCall: 'error',
|
reportOptionalCall: 'error',
|
||||||
@ -683,6 +696,7 @@ export function getStandardDiagnosticRuleSet(): DiagnosticRuleSet {
|
|||||||
reportUnsupportedDunderAll: 'warning',
|
reportUnsupportedDunderAll: 'warning',
|
||||||
reportUnusedCallResult: 'none',
|
reportUnusedCallResult: 'none',
|
||||||
reportUnusedCoroutine: 'error',
|
reportUnusedCoroutine: 'error',
|
||||||
|
reportUnusedExcept: 'error',
|
||||||
reportUnusedExpression: 'warning',
|
reportUnusedExpression: 'warning',
|
||||||
reportUnnecessaryTypeIgnoreComment: 'none',
|
reportUnnecessaryTypeIgnoreComment: 'none',
|
||||||
reportMatchNotExhaustive: 'none',
|
reportMatchNotExhaustive: 'none',
|
||||||
@ -723,6 +737,7 @@ export function getStrictDiagnosticRuleSet(): DiagnosticRuleSet {
|
|||||||
reportUnusedVariable: 'error',
|
reportUnusedVariable: 'error',
|
||||||
reportDuplicateImport: 'error',
|
reportDuplicateImport: 'error',
|
||||||
reportWildcardImportFromLibrary: 'error',
|
reportWildcardImportFromLibrary: 'error',
|
||||||
|
reportAssertTypeFailure: 'error',
|
||||||
reportOptionalSubscript: 'error',
|
reportOptionalSubscript: 'error',
|
||||||
reportOptionalMemberAccess: 'error',
|
reportOptionalMemberAccess: 'error',
|
||||||
reportOptionalCall: 'error',
|
reportOptionalCall: 'error',
|
||||||
@ -769,6 +784,7 @@ export function getStrictDiagnosticRuleSet(): DiagnosticRuleSet {
|
|||||||
reportUnsupportedDunderAll: 'error',
|
reportUnsupportedDunderAll: 'error',
|
||||||
reportUnusedCallResult: 'none',
|
reportUnusedCallResult: 'none',
|
||||||
reportUnusedCoroutine: 'error',
|
reportUnusedCoroutine: 'error',
|
||||||
|
reportUnusedExcept: 'error',
|
||||||
reportUnusedExpression: 'error',
|
reportUnusedExpression: 'error',
|
||||||
reportUnnecessaryTypeIgnoreComment: 'none',
|
reportUnnecessaryTypeIgnoreComment: 'none',
|
||||||
reportMatchNotExhaustive: 'error',
|
reportMatchNotExhaustive: 'error',
|
||||||
|
@ -35,6 +35,7 @@ export enum DiagnosticRule {
|
|||||||
reportUnusedVariable = 'reportUnusedVariable',
|
reportUnusedVariable = 'reportUnusedVariable',
|
||||||
reportDuplicateImport = 'reportDuplicateImport',
|
reportDuplicateImport = 'reportDuplicateImport',
|
||||||
reportWildcardImportFromLibrary = 'reportWildcardImportFromLibrary',
|
reportWildcardImportFromLibrary = 'reportWildcardImportFromLibrary',
|
||||||
|
reportAssertTypeFailure = 'reportAssertTypeFailure',
|
||||||
reportOptionalSubscript = 'reportOptionalSubscript',
|
reportOptionalSubscript = 'reportOptionalSubscript',
|
||||||
reportOptionalMemberAccess = 'reportOptionalMemberAccess',
|
reportOptionalMemberAccess = 'reportOptionalMemberAccess',
|
||||||
reportOptionalCall = 'reportOptionalCall',
|
reportOptionalCall = 'reportOptionalCall',
|
||||||
@ -81,6 +82,7 @@ export enum DiagnosticRule {
|
|||||||
reportUnsupportedDunderAll = 'reportUnsupportedDunderAll',
|
reportUnsupportedDunderAll = 'reportUnsupportedDunderAll',
|
||||||
reportUnusedCallResult = 'reportUnusedCallResult',
|
reportUnusedCallResult = 'reportUnusedCallResult',
|
||||||
reportUnusedCoroutine = 'reportUnusedCoroutine',
|
reportUnusedCoroutine = 'reportUnusedCoroutine',
|
||||||
|
reportUnusedExcept = 'reportUnusedExcept',
|
||||||
reportUnusedExpression = 'reportUnusedExpression',
|
reportUnusedExpression = 'reportUnusedExpression',
|
||||||
reportUnnecessaryTypeIgnoreComment = 'reportUnnecessaryTypeIgnoreComment',
|
reportUnnecessaryTypeIgnoreComment = 'reportUnnecessaryTypeIgnoreComment',
|
||||||
reportMatchNotExhaustive = 'reportMatchNotExhaustive',
|
reportMatchNotExhaustive = 'reportMatchNotExhaustive',
|
||||||
|
@ -415,6 +415,22 @@
|
|||||||
false
|
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": {
|
"reportOptionalSubscript": {
|
||||||
"type": [
|
"type": [
|
||||||
"string",
|
"string",
|
||||||
@ -1151,6 +1167,22 @@
|
|||||||
false
|
false
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
|
"reportUnusedExcept": {
|
||||||
|
"type": [
|
||||||
|
"string",
|
||||||
|
"boolean"
|
||||||
|
],
|
||||||
|
"description": "Diagnostics for unreachable except clause.",
|
||||||
|
"default": "error",
|
||||||
|
"enum": [
|
||||||
|
"none",
|
||||||
|
"information",
|
||||||
|
"warning",
|
||||||
|
"error",
|
||||||
|
true,
|
||||||
|
false
|
||||||
|
]
|
||||||
|
},
|
||||||
"reportUnusedExpression": {
|
"reportUnusedExpression": {
|
||||||
"type": [
|
"type": [
|
||||||
"string",
|
"string",
|
||||||
|
@ -251,6 +251,12 @@
|
|||||||
"title": "Controls reporting of wildcard import from external library",
|
"title": "Controls reporting of wildcard import from external library",
|
||||||
"default": "warning"
|
"default": "warning"
|
||||||
},
|
},
|
||||||
|
"reportAssertTypeFailure": {
|
||||||
|
"$id": "#/properties/reportAssertTypeFailure",
|
||||||
|
"$ref": "#/definitions/diagnostic",
|
||||||
|
"title": "Controls reporting of type mismatch detected by typing.assert_type call",
|
||||||
|
"default": "error"
|
||||||
|
},
|
||||||
"reportOptionalSubscript": {
|
"reportOptionalSubscript": {
|
||||||
"$id": "#/properties/reportOptionalSubscript",
|
"$id": "#/properties/reportOptionalSubscript",
|
||||||
"$ref": "#/definitions/diagnostic",
|
"$ref": "#/definitions/diagnostic",
|
||||||
@ -527,6 +533,12 @@
|
|||||||
"title": "Controls reporting of call expressions that returns Coroutine whose results are not consumed",
|
"title": "Controls reporting of call expressions that returns Coroutine whose results are not consumed",
|
||||||
"default": "error"
|
"default": "error"
|
||||||
},
|
},
|
||||||
|
"reportUnusedExcept": {
|
||||||
|
"$id": "#/properties/reportUnusedExcept",
|
||||||
|
"$ref": "#/definitions/diagnostic",
|
||||||
|
"title": "Controls reporting of unreachable except clauses",
|
||||||
|
"default": "error"
|
||||||
|
},
|
||||||
"reportUnusedExpression": {
|
"reportUnusedExpression": {
|
||||||
"$id": "#/properties/reportUnusedExpression",
|
"$id": "#/properties/reportUnusedExpression",
|
||||||
"$ref": "#/definitions/diagnostic",
|
"$ref": "#/definitions/diagnostic",
|
||||||
|
Loading…
Reference in New Issue
Block a user