From 562a6b4848844e99e2558f6a8e9e73211ebee704 Mon Sep 17 00:00:00 2001 From: Eric Traut Date: Wed, 16 Jun 2021 18:43:58 -0700 Subject: [PATCH] In basic type checking mode, enabled the following diagnostic checks by default: reportOptionalSubscript, reportOptionalMemberAccess, reportOptionalCall, reportOptionalIterable, reportOptionalContextManager, and reportOptionalOperand. --- docs/configuration.md | 24 +++++++++---------- docs/getting-started.md | 2 +- .../src/common/configOptions.ts | 12 +++++----- .../src/tests/samples/genericTypes28.py | 4 ++-- .../src/tests/samples/import8.py | 2 +- .../src/tests/samples/typeNarrowing1.py | 2 ++ .../src/tests/typeEvaluator1.test.ts | 8 ++++++- packages/vscode-pyright/package.json | 12 +++++----- .../schemas/pyrightconfig.schema.json | 12 +++++----- 9 files changed, 43 insertions(+), 35 deletions(-) diff --git a/docs/configuration.md b/docs/configuration.md index ff4b42f98..84abfc016 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -78,17 +78,17 @@ The following settings control pyright’s diagnostic output (warnings or errors **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'. -**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 'none'. +**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'. -**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 'none'. +**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'. -**reportOptionalCall** [boolean or string, optional]: Generate or suppress diagnostics for an attempt to call a variable with an Optional type. The default value for this setting is 'none'. +**reportOptionalCall** [boolean or string, optional]: Generate or suppress diagnostics for an attempt to call a variable with an Optional type. The default value for this setting is 'error'. -**reportOptionalIterable** [boolean or string, optional]: Generate or suppress diagnostics for an attempt to use an Optional type as an iterable value (e.g. within a `for` statement). The default value for this setting is 'none'. +**reportOptionalIterable** [boolean or string, optional]: Generate or suppress diagnostics for an attempt to use an Optional type as an iterable value (e.g. within a `for` statement). The default value for this setting is 'error'. -**reportOptionalContextManager** [boolean or string, optional]: Generate or suppress diagnostics for an attempt to use an Optional type as a context manager (as a parameter to a `with` statement). The default value for this setting is 'none'. +**reportOptionalContextManager** [boolean or string, optional]: Generate or suppress diagnostics for an attempt to use an Optional type as a context manager (as a parameter to a `with` statement). The default value for this setting is 'error'. -**reportOptionalOperand** [boolean or string, optional]: Generate or suppress diagnostics for an attempt to use an Optional type as an operand to a binary or unary operator (like '+', '==', 'or', 'not'). The default value for this setting is 'none'. +**reportOptionalOperand** [boolean or string, optional]: Generate or suppress diagnostics for an attempt to use an Optional type as an operand to a binary or unary operator (like '+', '==', 'or', 'not'). The default value for this setting is 'error'. **reportTypedDictNotRequiredAccess** [boolean or string, optional]: Generate or suppress diagnostics for an attempt to access a non-required field within a TypedDict without first checking whether it is present. The default value for this setting is 'error'. @@ -279,12 +279,12 @@ The following table lists the default severity levels for each diagnostic rule w | reportUnusedVariable | "none" | "none" | "error" | | reportDuplicateImport | "none" | "none" | "error" | | reportWildcardImportFromLibrary | "none" | "warning" | "error" | -| reportOptionalSubscript | "none" | "none" | "error" | -| reportOptionalMemberAccess | "none" | "none" | "error" | -| reportOptionalCall | "none" | "none" | "error" | -| reportOptionalIterable | "none" | "none" | "error" | -| reportOptionalContextManager | "none" | "none" | "error" | -| reportOptionalOperand | "none" | "none" | "error" | +| reportOptionalSubscript | "none" | "error" | "error" | +| reportOptionalMemberAccess | "none" | "error" | "error" | +| reportOptionalCall | "none" | "error" | "error" | +| reportOptionalIterable | "none" | "error" | "error" | +| reportOptionalContextManager | "none" | "error" | "error" | +| reportOptionalOperand | "none" | "error" | "error" | | reportTypedDictNotRequiredAccess | "none" | "error" | "error" | | reportUntypedFunctionDecorator | "none" | "none" | "error" | | reportUntypedClassDecorator | "none" | "none" | "error" | diff --git a/docs/getting-started.md b/docs/getting-started.md index 46c70739e..1f5ef264b 100644 --- a/docs/getting-started.md +++ b/docs/getting-started.md @@ -11,7 +11,7 @@ Here is a typical progression: 6. Look for type stubs for the packages you use. Some package authors opt to ship stubs as a separate companion package named that has “-stubs” appended to the name of the original package. 7. In cases where type stubs do not yet exist for a package you are using, consider creating a custom type stub that defines the portion of the interface that your source code consumes. Check in your custom type stub files and configure pyright to run as part of your continuous integration (CI) environment to keep the project “type clean”. 8. Incrementally add type annotations to your code files. The annotations that provide most value are on function input parameters, instance variables, and return parameters (in that order). Note that annotation of variables (instance, class and local) requires Python 3.6 or newer. -9. Enable stricter type checking options like "reportOptionalSubscript", "reportOptionalMemberAccess", "reportOptionalCall", and "reportUntypedFunctionDecorator". +9. Enable stricter type checking options like "reportUnknownParameterType", and "reportUntypedFunctionDecorator". 10. On a file-by-file basis, enable all type checking options by adding the comment `# pyright: strict` somewhere in the file. 11. Optionally add entire subdirectories to the `strict` config entry to indicate that all files within those subdirectories should be strictly typed. diff --git a/packages/pyright-internal/src/common/configOptions.ts b/packages/pyright-internal/src/common/configOptions.ts index a47bffffe..b720ca79b 100644 --- a/packages/pyright-internal/src/common/configOptions.ts +++ b/packages/pyright-internal/src/common/configOptions.ts @@ -439,12 +439,12 @@ export function getBasicDiagnosticRuleSet(): DiagnosticRuleSet { reportUnusedVariable: 'none', reportDuplicateImport: 'none', reportWildcardImportFromLibrary: 'warning', - reportOptionalSubscript: 'none', - reportOptionalMemberAccess: 'none', - reportOptionalCall: 'none', - reportOptionalIterable: 'none', - reportOptionalContextManager: 'none', - reportOptionalOperand: 'none', + reportOptionalSubscript: 'error', + reportOptionalMemberAccess: 'error', + reportOptionalCall: 'error', + reportOptionalIterable: 'error', + reportOptionalContextManager: 'error', + reportOptionalOperand: 'error', reportTypedDictNotRequiredAccess: 'error', reportUntypedFunctionDecorator: 'none', reportUntypedClassDecorator: 'none', diff --git a/packages/pyright-internal/src/tests/samples/genericTypes28.py b/packages/pyright-internal/src/tests/samples/genericTypes28.py index 6c33b245c..50bc36bbd 100644 --- a/packages/pyright-internal/src/tests/samples/genericTypes28.py +++ b/packages/pyright-internal/src/tests/samples/genericTypes28.py @@ -15,8 +15,8 @@ def foo1(a: Type[_T1]) -> _T1: a = foo1(Optional[int]) -def foo2(a: Type[_T2]) -> _T2: - return a() +def foo2(a: Type[_T2]) -> Type[_T2]: + return a b = foo2(type(None)) diff --git a/packages/pyright-internal/src/tests/samples/import8.py b/packages/pyright-internal/src/tests/samples/import8.py index 8f5010b24..dc07fa893 100644 --- a/packages/pyright-internal/src/tests/samples/import8.py +++ b/packages/pyright-internal/src/tests/samples/import8.py @@ -4,4 +4,4 @@ def __getattr__(name: str): - return None + return str diff --git a/packages/pyright-internal/src/tests/samples/typeNarrowing1.py b/packages/pyright-internal/src/tests/samples/typeNarrowing1.py index 88976c99c..d212108af 100644 --- a/packages/pyright-internal/src/tests/samples/typeNarrowing1.py +++ b/packages/pyright-internal/src/tests/samples/typeNarrowing1.py @@ -1,6 +1,8 @@ # This file validates type narrowing that involve # conditional binary expressions. +# pyright: reportOptionalMemberAccess=false + class Foo: def bar(self): diff --git a/packages/pyright-internal/src/tests/typeEvaluator1.test.ts b/packages/pyright-internal/src/tests/typeEvaluator1.test.ts index 79fae85fc..3c540d115 100644 --- a/packages/pyright-internal/src/tests/typeEvaluator1.test.ts +++ b/packages/pyright-internal/src/tests/typeEvaluator1.test.ts @@ -779,7 +779,13 @@ test('Operators6', () => { test('Optional1', () => { const configOptions = new ConfigOptions('.'); - // By default, optional diagnostics are ignored. + // Disable diagnostics. + configOptions.diagnosticRuleSet.reportOptionalSubscript = 'none'; + configOptions.diagnosticRuleSet.reportOptionalMemberAccess = 'none'; + configOptions.diagnosticRuleSet.reportOptionalCall = 'none'; + configOptions.diagnosticRuleSet.reportOptionalIterable = 'none'; + configOptions.diagnosticRuleSet.reportOptionalContextManager = 'none'; + configOptions.diagnosticRuleSet.reportOptionalOperand = 'none'; let analysisResults = TestUtils.typeAnalyzeSampleFiles(['optional1.py'], configOptions); TestUtils.validateResults(analysisResults, 0); diff --git a/packages/vscode-pyright/package.json b/packages/vscode-pyright/package.json index cd11ac08b..049b20c9f 100644 --- a/packages/vscode-pyright/package.json +++ b/packages/vscode-pyright/package.json @@ -260,7 +260,7 @@ "reportOptionalSubscript": { "type": "string", "description": "Diagnostics for an attempt to subscript (index) a variable with an Optional type.", - "default": "none", + "default": "error", "enum": [ "none", "information", @@ -271,7 +271,7 @@ "reportOptionalMemberAccess": { "type": "string", "description": "Diagnostics for an attempt to access a member of a variable with an Optional type.", - "default": "none", + "default": "error", "enum": [ "none", "information", @@ -282,7 +282,7 @@ "reportOptionalCall": { "type": "string", "description": "Diagnostics for an attempt to call a variable with an Optional type.", - "default": "none", + "default": "error", "enum": [ "none", "information", @@ -293,7 +293,7 @@ "reportOptionalIterable": { "type": "string", "description": "Diagnostics for an attempt to use an Optional type as an iterable value (e.g. within a for statement).", - "default": "none", + "default": "error", "enum": [ "none", "information", @@ -304,7 +304,7 @@ "reportOptionalContextManager": { "type": "string", "description": "Diagnostics for an attempt to use an Optional type as a context manager (as a parameter to a with statement).", - "default": "none", + "default": "error", "enum": [ "none", "information", @@ -315,7 +315,7 @@ "reportOptionalOperand": { "type": "string", "description": "Diagnostics for an attempt to use an Optional type as an operand to a binary or unary operator (like '+', '==', 'or', 'not').", - "default": "none", + "default": "error", "enum": [ "none", "information", diff --git a/packages/vscode-pyright/schemas/pyrightconfig.schema.json b/packages/vscode-pyright/schemas/pyrightconfig.schema.json index 3d7d3f6f7..7794d1d70 100644 --- a/packages/vscode-pyright/schemas/pyrightconfig.schema.json +++ b/packages/vscode-pyright/schemas/pyrightconfig.schema.json @@ -213,37 +213,37 @@ "$id": "#/properties/reportOptionalSubscript", "$ref": "#/definitions/diagnostic", "title": "Controls reporting of attempts to subscript (index) a variable with Optional type", - "default": "none" + "default": "error" }, "reportOptionalMemberAccess": { "$id": "#/properties/reportOptionalMemberAccess", "$ref": "#/definitions/diagnostic", "title": "Controls reporting of attempts to access a member of a variable with Optional type", - "default": "none" + "default": "error" }, "reportOptionalCall": { "$id": "#/properties/reportOptionalCall", "$ref": "#/definitions/diagnostic", "title": "Controls reporting of attempts to call a variable with Optional type", - "default": "none" + "default": "error" }, "reportOptionalIterable": { "$id": "#/properties/reportOptionalIterable", "$ref": "#/definitions/diagnostic", "title": "Controls reporting of attempts to use an Optional type as an iterable value", - "default": "none" + "default": "error" }, "reportOptionalContextManager": { "$id": "#/properties/reportOptionalContextManager", "$ref": "#/definitions/diagnostic", "title": "Controls reporting of attempts to use an Optional type as a parameter to a with statement", - "default": "none" + "default": "error" }, "reportOptionalOperand": { "$id": "#/properties/reportOptionalOperand", "$ref": "#/definitions/diagnostic", "title": "Controls reporting of attempts to use an Optional type as an operand for a binary or unary operator", - "default": "none" + "default": "error" }, "reportTypedDictNotRequiredAccess": { "$id": "#/properties/reportTypedDictNotRequiredAccess",