Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
1 change: 1 addition & 0 deletions mypy/exportjson.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ def convert_symbol_table(self: SymbolTable, cfg: Config) -> Json:
if key == "__builtins__" or value.no_serialize:
continue
if not cfg.implicit_names and key in {
"__loader__",
"__spec__",
"__package__",
"__file__",
Expand Down
1 change: 1 addition & 0 deletions mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ def set_line(
"__file__": "__builtins__.str",
"__package__": "__builtins__.str",
"__annotations__": None, # dict[str, Any] bounded in add_implicit_module_attrs()
"__loader__": None, # _typeshed.importlib.LoaderProtocol | None, see semanal.py
"__spec__": None, # importlib.machinery.ModuleSpec bounded in add_implicit_module_attrs()
}

Expand Down
17 changes: 17 additions & 0 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,23 @@ def add_implicit_module_attrs(self, file_node: MypyFile) -> None:
self.defer()
return
typ = inst
elif name == "__loader__":
if self.options.use_builtins_fixtures:
inst = self.named_type_or_none("builtins.object")
else:
inst = self.named_type_or_none("_typeshed.importlib.LoaderProtocol")
if inst is None:
if (
self.final_iteration
or self.options.clone_for_module("_typeshed.importlib").follow_imports
== "skip"
):
inst = self.named_type_or_none("builtins.object")
assert inst is not None, "Cannot find builtins.object"
else:
self.defer()
return
typ = UnionType.make_union([inst, NoneType()])
elif name == "__spec__":
if self.options.use_builtins_fixtures:
inst = self.named_type_or_none("builtins.object")
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/fine-grained-inspect.test
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ class C: ...
[builtins fixtures/module.pyi]
[out]
==
{"<pack.bar>": ["C", "__annotations__", "__doc__", "__file__", "__name__", "__package__", "__spec__", "bar", "x"], "ModuleType": ["__file__", "__getattr__"]}
{"<pack.bar>": ["C", "__annotations__", "__doc__", "__file__", "__loader__", "__name__", "__package__", "__spec__", "bar", "x"], "ModuleType": ["__file__", "__getattr__"]}

[case testInspectModuleDef]
# inspect2: --show=definition --include-kind tmp/foo.py:2:1
Expand Down
Loading