Skip to content

Commit 28ba99c

Browse files
pkchgvanrossum
authored andcommitted
Stop redirecting to _importlib.. in py2 (#3235)
1 parent c6bd0c6 commit 28ba99c

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

mypy/fixup.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,9 @@ def lookup_qualified(modules: Dict[str, MypyFile], name: str,
243243

244244
def lookup_qualified_stnode(modules: Dict[str, MypyFile], name: str,
245245
quick_and_dirty: bool) -> Optional[SymbolTableNode]:
246-
name = rev_module_rename_map.get(name, name)
246+
if '_importlib_modulespec' in modules:
247+
# we are using python 3, so renaming is necessary
248+
name = rev_module_rename_map.get(name, name)
247249
head = name
248250
rest = []
249251
while True:

mypy/semanal.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,18 @@
117117
'typing.typevar': 'typing.TypeVar',
118118
}
119119

120+
# Used for python 3 only
120121
# Rename objects placed in _importlib_modulespec due to circular imports
122+
# Safe in python 2 because no one will ask for these keys
121123
module_rename_map = {
122124
'_importlib_modulespec.ModuleType': 'types.ModuleType',
123125
'_importlib_modulespec.ModuleSpec': 'importlib.machinery.ModuleSpec',
124126
'_importlib_modulespec.Loader': 'importlib.abc.Loader'
125127
}
126128

129+
# Must not be used in python 2 (it will replace correct names with incorrect)
130+
# The check for version is currently made at the point of use
131+
# TODO: consider moving these maps from global scope to somewhere where python version is known
127132
rev_module_rename_map = {v: k for (k, v) in module_rename_map.items()}
128133

129134
# Hard coded type promotions (shared between all Python versions).

0 commit comments

Comments
 (0)