mirror of
https://github.com/microsoft/pyright.git
synced 2024-10-05 20:38:25 +03:00
Added support for PEP 562's __getattr__ function.
This commit is contained in:
parent
70753ed778
commit
a25e832ad4
@ -6770,11 +6770,20 @@ export function createTypeEvaluator(importLookup: ImportLookup): TypeEvaluator {
|
||||
|
||||
// If we were able to resolve the import, report the error as
|
||||
// an unresolved symbol.
|
||||
if (importLookup(resolvedPath)) {
|
||||
addError(
|
||||
`'${ node.name.value }' is unknown import symbol`,
|
||||
node.name
|
||||
);
|
||||
const importLookupInfo = importLookup(resolvedPath);
|
||||
if (importLookupInfo) {
|
||||
const fileInfo = getFileInfo(node);
|
||||
|
||||
// Handle PEP 562 support for module-level __getattr__ function,
|
||||
// introduced in Python 3.7.
|
||||
if (fileInfo.executionEnvironment.pythonVersion < PythonVersion.V37 ||
|
||||
!importLookupInfo.symbolTable.get('__getattr__')) {
|
||||
|
||||
addError(
|
||||
`'${ node.name.value }' is unknown import symbol`,
|
||||
node.name
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -980,6 +980,11 @@ test('Import7', () => {
|
||||
validateResults(analysisResults, 2);
|
||||
});
|
||||
|
||||
test('Import9', () => {
|
||||
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['import9.py']);
|
||||
validateResults(analysisResults, 0);
|
||||
});
|
||||
|
||||
test('Overload1', () => {
|
||||
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['overload1.py']);
|
||||
validateResults(analysisResults, 2);
|
||||
|
9
server/src/tests/samples/import8.py
Normal file
9
server/src/tests/samples/import8.py
Normal file
@ -0,0 +1,9 @@
|
||||
# This sample is imported by import9.py.
|
||||
|
||||
# Implement __getattr__ function as described in PEP 562.
|
||||
|
||||
def __getattr__(name: str):
|
||||
return None
|
||||
|
||||
|
||||
|
8
server/src/tests/samples/import9.py
Normal file
8
server/src/tests/samples/import9.py
Normal file
@ -0,0 +1,8 @@
|
||||
# This sample tests support for PEP 562's __getattr__ function.
|
||||
|
||||
# This should not generate an error because import9 has
|
||||
# a __getattr__ method.
|
||||
from .import8 import foo
|
||||
|
||||
foo()
|
||||
|
Loading…
Reference in New Issue
Block a user