In basic type checking mode, enabled the following diagnostic checks by default: reportOptionalSubscript, reportOptionalMemberAccess, reportOptionalCall, reportOptionalIterable, reportOptionalContextManager, and reportOptionalOperand.

This commit is contained in:
Eric Traut 2021-06-16 18:43:58 -07:00
parent b1a1a68837
commit 562a6b4848
9 changed files with 43 additions and 35 deletions

View File

@ -78,17 +78,17 @@ The following settings control pyrights 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" |

View File

@ -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.

View File

@ -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',

View File

@ -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))

View File

@ -4,4 +4,4 @@
def __getattr__(name: str):
return None
return str

View File

@ -1,6 +1,8 @@
# This file validates type narrowing that involve
# conditional binary expressions.
# pyright: reportOptionalMemberAccess=false
class Foo:
def bar(self):

View File

@ -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);

View File

@ -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",

View File

@ -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",