From 4ee67246e81e715406b68697a7afc720934e33bf Mon Sep 17 00:00:00 2001 From: Max Moroz Date: Mon, 24 Apr 2017 13:50:54 -0700 Subject: [PATCH] Stop redirecting to _importlib.. in py2 --- mypy/fixup.py | 4 +++- mypy/semanal.py | 5 +++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/mypy/fixup.py b/mypy/fixup.py index b9ac5db1c1a0..3d7fae415c1a 100644 --- a/mypy/fixup.py +++ b/mypy/fixup.py @@ -243,7 +243,9 @@ def lookup_qualified(modules: Dict[str, MypyFile], name: str, def lookup_qualified_stnode(modules: Dict[str, MypyFile], name: str, quick_and_dirty: bool) -> Optional[SymbolTableNode]: - name = rev_module_rename_map.get(name, name) + if '_importlib_modulespec' in modules: + # we are using python 3, so renaming is necessary + name = rev_module_rename_map.get(name, name) head = name rest = [] while True: diff --git a/mypy/semanal.py b/mypy/semanal.py index 9668818da15d..d390455f8fbc 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -117,13 +117,18 @@ 'typing.typevar': 'typing.TypeVar', } +# Used for python 3 only # Rename objects placed in _importlib_modulespec due to circular imports +# Safe in python 2 because no one will ask for these keys module_rename_map = { '_importlib_modulespec.ModuleType': 'types.ModuleType', '_importlib_modulespec.ModuleSpec': 'importlib.machinery.ModuleSpec', '_importlib_modulespec.Loader': 'importlib.abc.Loader' } +# Must not be used in python 2 (it will replace correct names with incorrect) +# The check for version is currently made at the point of use +# TODO: consider moving these maps from global scope to somewhere where python version is known rev_module_rename_map = {v: k for (k, v) in module_rename_map.items()} # Hard coded type promotions (shared between all Python versions).