Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Hard code module renaming
  • Loading branch information
pkch committed Apr 3, 2017
commit b53c90427c2fd94d2f7df2dae5e92610d7921d81
21 changes: 9 additions & 12 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@
'typing.typevar': 'typing.TypeVar',
}

# Rename objects placed in _importlib_modulespec due to circular imports
module_rename_map = {
'_importlib_modulespec.ModuleType': 'types.ModuleType',
'_importlib_modulespec.ModuleSpec': 'importlib.machinery.ModuleSpec',
'_importlib_modulespec.Loader': 'importlib.abc.Loader'
}

# Hard coded type promotions (shared between all Python versions).
# These add extra ad-hoc edges to the subtyping relation. For example,
# int is considered a subtype of float, even though there is no
Expand Down Expand Up @@ -3354,13 +3361,6 @@ def visit_file(self, file: MypyFile, fnam: str, mod_id: str, options: Options) -
and these will get resolved in later phases of semantic
analysis.

MYPY_MODULE has a special meaning in stubs. If present, its current
value will be used as the module name prefix for qualified names of
all objects found in the stub. This is to provide correct output from
reveal_type for definitions placed in the "wrong" module for circular
import reasons (e.g., the definition of ModuleType in
_importlib_modulespec.pyi instead of types.pyi).

"""
sem = self.sem
self.sem.options = options # Needed because we sometimes call into it
Expand Down Expand Up @@ -3393,11 +3393,8 @@ def visit_file(self, file: MypyFile, fnam: str, mod_id: str, options: Options) -

for d in defs:
d.accept(self)
if file.is_stub and isinstance(d, AssignmentStmt):
lvalue = d.lvalues[0]
if isinstance(lvalue, NameExpr) and lvalue.name == 'MYPY_MODULE':
assert isinstance(d.rvalue, StrExpr)
sem.cur_mod_id = d.rvalue.value
if isinstance(d, ClassDef):
d.info._fullname = module_rename_map.get(d.info._fullname, d.info._fullname)

# Add implicit definition of literals/keywords to builtins, as we
# cannot define a variable with them explicitly.
Expand Down