Skip to content

Commit 1dda882

Browse files
author
nightcityblade
committed
Fix implicit __loader__ module attribute
1 parent 8b3e7d8 commit 1dda882

4 files changed

Lines changed: 20 additions & 1 deletion

File tree

mypy/exportjson.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ def convert_symbol_table(self: SymbolTable, cfg: Config) -> Json:
104104
if key == "__builtins__" or value.no_serialize:
105105
continue
106106
if not cfg.implicit_names and key in {
107+
"__loader__",
107108
"__spec__",
108109
"__package__",
109110
"__file__",

mypy/nodes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ def set_line(
176176
"__file__": "__builtins__.str",
177177
"__package__": "__builtins__.str",
178178
"__annotations__": None, # dict[str, Any] bounded in add_implicit_module_attrs()
179+
"__loader__": None, # _typeshed.importlib.LoaderProtocol | None, see semanal.py
179180
"__spec__": None, # importlib.machinery.ModuleSpec bounded in add_implicit_module_attrs()
180181
}
181182

mypy/semanal.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -771,6 +771,23 @@ def add_implicit_module_attrs(self, file_node: MypyFile) -> None:
771771
self.defer()
772772
return
773773
typ = inst
774+
elif name == "__loader__":
775+
if self.options.use_builtins_fixtures:
776+
inst = self.named_type_or_none("builtins.object")
777+
else:
778+
inst = self.named_type_or_none("_typeshed.importlib.LoaderProtocol")
779+
if inst is None:
780+
if (
781+
self.final_iteration
782+
or self.options.clone_for_module("_typeshed.importlib").follow_imports
783+
== "skip"
784+
):
785+
inst = self.named_type_or_none("builtins.object")
786+
assert inst is not None, "Cannot find builtins.object"
787+
else:
788+
self.defer()
789+
return
790+
typ = UnionType.make_union([inst, NoneType()])
774791
elif name == "__spec__":
775792
if self.options.use_builtins_fixtures:
776793
inst = self.named_type_or_none("builtins.object")

test-data/unit/fine-grained-inspect.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ class C: ...
236236
[builtins fixtures/module.pyi]
237237
[out]
238238
==
239-
{"<pack.bar>": ["C", "__annotations__", "__doc__", "__file__", "__name__", "__package__", "__spec__", "bar", "x"], "ModuleType": ["__file__", "__getattr__"]}
239+
{"<pack.bar>": ["C", "__annotations__", "__doc__", "__file__", "__loader__", "__name__", "__package__", "__spec__", "bar", "x"], "ModuleType": ["__file__", "__getattr__"]}
240240

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

0 commit comments

Comments
 (0)