Added new config feature: reportUntypedBaseClass.

This commit is contained in:
Eric Traut 2019-04-18 07:45:50 -07:00
parent 5eca4a67b8
commit 2abd802fe6
5 changed files with 26 additions and 6 deletions

View File

@ -121,6 +121,12 @@
"title": "Controls reporting of class decorators without type annotations, which obscure class types",
"default": "none"
},
"reportUntypedBaseClass": {
"$id": "#/properties/reportUntypedBaseClass",
"$ref": "#/definitions/diagnostic",
"title": "Controls reporting of a base class of an unknown type, which obscures most type checking for the class",
"default": "none"
},
"reportPrivateUsage": {
"$id": "#/properties/reportPrivateUsage",
"$ref": "#/definitions/diagnostic",

View File

@ -48,6 +48,8 @@ The following settings control pyright's diagnostic output (warnings or errors).
**reportUntypedClassDecorator** [boolean or string, optional]: Generate or suppress diagnostics for class decorators that have no type annotations. These obscure the class type, defeating many type analysis features.
**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.
**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.

View File

@ -203,12 +203,6 @@ export abstract class SemanticAnalyzer extends ParseTreeWalker {
}
}
if (!argType.isAny()) {
if (argType.category !== TypeCategory.Class) {
this._addError(`Argument to class must be a base class`, arg);
}
}
classType.addBaseClass(argType, isMetaclass);
if (!isMetaclass) {

View File

@ -163,6 +163,17 @@ export class TypeAnalyzer extends ParseTreeWalker {
}
}
if (this._fileInfo.configOptions.reportUntypedBaseClass !== 'none') {
if (argType instanceof UnknownType ||
argType instanceof UnionType && argType.getTypes().some(t => t instanceof UnknownType)) {
this._addDiagnostic(
this._fileInfo.configOptions.reportUntypedBaseClass,
`Base class type is unknown, obscuring type of derived class`,
arg);
}
}
if (classType.updateBaseClassType(index, argType)) {
this._setAnalysisChanged();
}

View File

@ -106,6 +106,9 @@ export class ConfigOptions {
// Report untyped class decorators that obscure the class type?
reportUntypedClassDecorator: DiagnosticLevel = 'none';
// Report untyped base class that obscure the class type?
reportUntypedBaseClass: DiagnosticLevel = 'none';
// Report usage of private variables and functions outside of
// the owning class or module?
reportPrivateUsage: DiagnosticLevel = 'none';
@ -250,6 +253,10 @@ export class ConfigOptions {
this.reportUntypedClassDecorator = this._convertDiagnosticLevel(
configObj.reportUntypedClassDecorator, 'reportUntypedClassDecorator', 'none');
// Read the "reportUntypedBaseClass" entry.
this.reportUntypedBaseClass = this._convertDiagnosticLevel(
configObj.reportUntypedBaseClass, 'reportUntypedBaseClass', 'none');
// Read the "reportPrivateUsage" entry.
this.reportPrivateUsage = this._convertDiagnosticLevel(
configObj.reportPrivateUsage, 'reportPrivateUsage', 'none');