Added special-case handling of __import__ built-in call so it always returns Any type.

This commit is contained in:
Eric Traut 2019-10-22 12:35:36 -07:00
parent 55adbf4b29
commit 619f18b1df
4 changed files with 19 additions and 0 deletions

View File

@ -1907,6 +1907,13 @@ export class ExpressionEvaluator {
} else {
type = this._validateCallArguments(errorNode, argList, callType,
new TypeVarMap(), specializeReturnType);
if (FunctionType.getBuiltInName(callType) === '__import__') {
// For the special __import__ type, we'll override the return type to be "Any".
// This is required because we don't know what module was imported, and we don't
// want to fail type checks when accessing members of the resulting module type.
type = AnyType.create();
}
}
if (!type) {

View File

@ -166,6 +166,7 @@ export class SourceFile {
this._isBuiltInStubFile = false;
if (this._isStubFile) {
if (this._filePath.endsWith(normalizeSlashes('/collections/__init__.pyi')) ||
fileName === 'builtins.pyi' ||
fileName === '_importlib_modulespec.pyi' ||
fileName === 'dataclasses.pyi' ||
fileName === 'abc.pyi' ||

View File

@ -0,0 +1,6 @@
# This sample tests the type analyzer's handling of the built-in
# __import__ function.
# This should not generate a type error.
__path__ = __import__('pkgutil').extend_path(__path__, __name__)

View File

@ -741,3 +741,8 @@ test('AssignmentExpr3', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['assignmentExpr3.py']);
validateResults(analysisResults, 3);
});
test('Import1', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['import1.py']);
validateResults(analysisResults, 0);
});