Added support for PEP 562's __getattr__ function.

This commit is contained in:
Eric Traut 2020-01-02 01:37:41 -07:00
parent 70753ed778
commit a25e832ad4
4 changed files with 36 additions and 5 deletions

View File

@ -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
);
}
}
}

View File

@ -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);

View 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

View 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()