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
Allow module name override for stub files
  • Loading branch information
pkch committed Apr 2, 2017
commit fbcb6ffc6724ad17047b93c42da597a29f564195
13 changes: 13 additions & 0 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -3353,6 +3353,14 @@ def visit_file(self, file: MypyFile, fnam: str, mod_id: str, options: Options) -
we generate dummy symbol table nodes for the imported names,
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 type.pyi).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

types.pyi

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just in time for the last commit =)


"""
sem = self.sem
self.sem.options = options # Needed because we sometimes call into it
Expand Down Expand Up @@ -3385,6 +3393,11 @@ 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

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