Added support for new rule: strictParameterNoneValue.

This commit is contained in:
Eric Traut 2019-08-04 15:18:51 -07:00
parent 03dfb1e1cb
commit 1d5475bfcb
3 changed files with 21 additions and 6 deletions

View File

@ -34,6 +34,8 @@ The following settings control pyright's diagnostic output (warnings or errors).
**strictDictionaryInference** [boolean]: When inferring the type of a dictionarys keys and values, use strict type assumptions. For example, the expression `{'a': 1, 'b': 'a'}` could be inferred to be of type `Dict[str, Any]` or `Dict[str, Union[int, str]]`. If this setting is true, it will use the latter (stricter) type. The default value for this setting is 'false'.
**strictParameterNoneValue** [boolean]: PEP 484 indicates that when a function parameter is assigned a default value of None, its type should implicitly be Optional even if the explicit type is not. When enabled, this rule requires that parameter type annotations use Optional explicitly in this case. The default value for this setting is 'false'.
**reportTypeshedErrors** [boolean or string, optional]: Generate or suppress diagnostics for typeshed type stub files. In general, these type stub files should be “clean” and generate no errors. The default value for this setting is 'none'.
**reportMissingImports** [boolean or string, optional]: Generate or suppress diagnostics for imports that have no corresponding imported python file or type stub file. The default value for this setting is 'none', although pyright can do a much better job of static type checking if type stub files are provided for all imports.

View File

@ -331,11 +331,12 @@ export class TypeAnalyzer extends ParseTreeWalker {
// PEP 484 indicates that if a parameter has a default value of 'None'
// the type checker should assume that the type is optional (i.e. a union
// of the specified type and 'None').
// TODO - tighten this up, perhaps using a config setting
if (param.defaultValue instanceof ConstantNode) {
if (param.defaultValue.token.keywordType === KeywordType.None) {
annotatedType = TypeUtils.combineTypes(
[annotatedType, NoneType.create()]);
if (!this._fileInfo.diagnosticSettings.strictParameterNoneValue) {
if (param.defaultValue instanceof ConstantNode) {
if (param.defaultValue.token.keywordType === KeywordType.None) {
annotatedType = TypeUtils.combineTypes(
[annotatedType, NoneType.create()]);
}
}
}

View File

@ -48,6 +48,9 @@ export interface DiagnosticSettings {
// Use strict inference rules for dictionary expressions?
strictDictionaryInference: boolean;
// Use strict type rules for parameters assigned default of None?
strictParameterNoneValue: boolean;
// Report diagnostics in typeshed files?
reportTypeshedErrors: DiagnosticLevel;
@ -140,7 +143,8 @@ export function cloneDiagnosticSettings(
export function getBooleanDiagnosticSettings() {
return [
'strictListInference',
'strictDictionaryInference'
'strictDictionaryInference',
'strictParameterNoneValue'
];
}
@ -179,6 +183,7 @@ export function getStrictDiagnosticSettings(): DiagnosticSettings {
const diagSettings: DiagnosticSettings = {
strictListInference: true,
strictDictionaryInference: true,
strictParameterNoneValue: true,
reportTypeshedErrors: 'error',
reportMissingImports: 'error',
reportMissingTypeStubs: 'error',
@ -214,6 +219,7 @@ export function getDefaultDiagnosticSettings(): DiagnosticSettings {
const diagSettings: DiagnosticSettings = {
strictListInference: false,
strictDictionaryInference: false,
strictParameterNoneValue: false,
reportTypeshedErrors: 'none',
reportMissingImports: 'error',
reportMissingTypeStubs: 'none',
@ -429,6 +435,12 @@ export class ConfigOptions {
configObj.strictDictionaryInference, 'strictDictionaryInference',
defaultSettings.strictDictionaryInference),
// Should a None default value imply that the parameter type
// is Optional?
strictParameterNoneValue: this._convertBoolean(
configObj.strictParameterNoneValue, 'strictParameterNoneValue',
defaultSettings.strictParameterNoneValue),
// Read the "reportTypeshedErrors" entry.
reportTypeshedErrors: this._convertDiagnosticLevel(
configObj.reportTypeshedErrors, 'reportTypeshedErrors',