diff --git a/client/schemas/pyrightconfig.schema.json b/client/schemas/pyrightconfig.schema.json index 7dcc5d24d..93995bab0 100644 --- a/client/schemas/pyrightconfig.schema.json +++ b/client/schemas/pyrightconfig.schema.json @@ -139,6 +139,12 @@ "title": "Controls reporting of a base class of an unknown type, which obscures most type checking for the class", "default": "none" }, + "reportUntypedNamedTuple": { + "$id": "#/properties/reportUntypedNamedTuple", + "$ref": "#/definitions/diagnostic", + "title": "Controls reporting of a named tuple definition that does not contain type information", + "default": "none" + }, "reportPrivateUsage": { "$id": "#/properties/reportPrivateUsage", "$ref": "#/definitions/diagnostic", diff --git a/docs/configuration.md b/docs/configuration.md index d5575836c..22cb0bdf6 100644 --- a/docs/configuration.md +++ b/docs/configuration.md @@ -54,6 +54,8 @@ The following settings control pyright's diagnostic output (warnings or errors). **reportUntypedBaseClass** [boolean or string, optional]: Generate or suppress diagnostics for base classes whose type cannot be determined statically. These obscure the class type, defeating many type analysis features. The default value for this setting is 'none'. +**reportUntypedNamedTuple** [boolean or string, optional]: Generate or suppress diagnostics when “namedtuple” is used rather than “NamedTuple”. The former contains no type information, whereas the latter does. The default value for this setting is 'none'. + **reportPrivateUsage** [boolean or string, optional]: Generate or suppress diagnostics for uses of private variables or functions outside of the class or module that declares them. Private variables and functions, by convention, are named starting with a single underscoe (“_”) character. The default value for this setting is 'none'. **reportInvalidStringEscapeSequence** [boolean or string, optional]: Generate or suppress diagnostics for invalid escape sequences used within string literals. The Python specification indicates that such sequences will generate a syntax error in future versions. The default value for this setting is 'warning'. diff --git a/server/src/analyzer/expressionEvaluator.ts b/server/src/analyzer/expressionEvaluator.ts index bfc9c3b5d..d9905cd90 100644 --- a/server/src/analyzer/expressionEvaluator.ts +++ b/server/src/analyzer/expressionEvaluator.ts @@ -1155,6 +1155,10 @@ export class ExpressionEvaluator { // The stdlib collections/__init__.pyi stub file defines namedtuple // as a function rather than a class, so we need to check for it here. if (callType.getBuiltInName() === 'namedtuple') { + this._addDiagnostic( + this._fileInfo.configOptions.reportUntypedNamedTuple, + `'namedtuple' provides no types for tuple entries. Use 'NamedTuple' instead.`, + errorNode); type = this._createNamedTupleType(errorNode, argList, false, cachedCallType); } else { type = this._validateCallArguments(errorNode, argList, callType, new TypeVarMap()); diff --git a/server/src/common/configOptions.ts b/server/src/common/configOptions.ts index 5260b55a9..d6f481409 100644 --- a/server/src/common/configOptions.ts +++ b/server/src/common/configOptions.ts @@ -118,6 +118,9 @@ export class ConfigOptions { // Report untyped base class that obscure the class type? reportUntypedBaseClass: DiagnosticLevel = 'none'; + // Report use of untyped namedtuple factory method? + reportUntypedNamedTuple: DiagnosticLevel = 'none'; + // Report usage of private variables and functions outside of // the owning class or module? reportPrivateUsage: DiagnosticLevel = 'none'; @@ -286,6 +289,10 @@ export class ConfigOptions { this.reportUntypedBaseClass = this._convertDiagnosticLevel( configObj.reportUntypedBaseClass, 'reportUntypedBaseClass', 'none'); + // Read the "reportUntypedNamedTuple" entry. + this.reportUntypedNamedTuple = this._convertDiagnosticLevel( + configObj.reportUntypedNamedTuple, 'reportUntypedNamedTuple', 'none'); + // Read the "reportPrivateUsage" entry. this.reportPrivateUsage = this._convertDiagnosticLevel( configObj.reportPrivateUsage, 'reportPrivateUsage', 'none');