mirror of
https://github.com/microsoft/pyright.git
synced 2024-09-11 16:06:39 +03:00
Added special-case handling of __import__
built-in call so it always returns Any
type.
This commit is contained in:
parent
55adbf4b29
commit
619f18b1df
@ -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) {
|
||||
|
@ -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' ||
|
||||
|
6
server/src/tests/samples/import1.py
Normal file
6
server/src/tests/samples/import1.py
Normal 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__)
|
||||
|
@ -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);
|
||||
});
|
||||
|
Loading…
Reference in New Issue
Block a user