Skip to content

Commit

Permalink
Add support for __spec__ (#14739)
Browse files Browse the repository at this point in the history
Fixes #4145

Co-authored-by: Joongi Kim <[email protected]>
  • Loading branch information
hauntsaninja and achimnol authored May 21, 2024
1 parent 3579c61 commit f5afdcd
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 1 deletion.
1 change: 1 addition & 0 deletions mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ def set_line(
"__file__": "__builtins__.str",
"__package__": "__builtins__.str",
"__annotations__": None, # dict[str, Any] bounded in add_implicit_module_attrs()
"__spec__": None, # importlib.machinery.ModuleSpec bounded in add_implicit_module_attrs()
}


Expand Down
18 changes: 18 additions & 0 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@
TypeVarTupleType,
TypeVarType,
UnboundType,
UnionType,
UnpackType,
get_proper_type,
get_proper_types,
Expand Down Expand Up @@ -635,6 +636,7 @@ def add_implicit_module_attrs(self, file_node: MypyFile) -> None:
str_type: Type | None = self.named_type_or_none("builtins.str")
if str_type is None:
str_type = UnboundType("builtins.str")
inst: Type | None
for name, t in implicit_module_attrs.items():
if name == "__doc__":
typ: Type = str_type
Expand All @@ -660,6 +662,22 @@ def add_implicit_module_attrs(self, file_node: MypyFile) -> None:
self.defer()
return
typ = inst
elif name == "__spec__":
if self.options.use_builtins_fixtures:
inst = self.named_type_or_none("builtins.object")
else:
inst = self.named_type_or_none("importlib.machinery.ModuleSpec")
if inst is None:
if self.final_iteration:
inst = self.named_type_or_none("builtins.object")
assert inst is not None, "Cannot find builtins.object"
else:
self.defer()
return
if file_node.name == "__main__":
# https://docs.python.org/3/reference/import.html#main-spec
inst = UnionType.make_union([inst, NoneType()])
typ = inst
else:
assert t is not None, f"type should be specified for {name}"
typ = UnboundType(t)
Expand Down
8 changes: 8 additions & 0 deletions test-data/unit/check-basic.test
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,14 @@ reveal_type(__doc__) # N: Revealed type is "builtins.str"
reveal_type(__file__) # N: Revealed type is "builtins.str"
reveal_type(__package__) # N: Revealed type is "builtins.str"
reveal_type(__annotations__) # N: Revealed type is "builtins.dict[builtins.str, Any]"
# This will actually reveal Union[importlib.machinery.ModuleSpec, None]
reveal_type(__spec__) # N: Revealed type is "Union[builtins.object, None]"

import module
reveal_type(module.__name__) # N: Revealed type is "builtins.str"
# This will actually reveal importlib.machinery.ModuleSpec
reveal_type(module.__spec__) # N: Revealed type is "builtins.object"
[file module.py]
[builtins fixtures/primitives.pyi]


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__", "bar", "x"], "ModuleType": ["__file__", "__getattr__"]}
{"<pack.bar>": ["C", "__annotations__", "__doc__", "__file__", "__name__", "__package__", "__spec__", "bar", "x"], "ModuleType": ["__file__", "__getattr__"]}

[case testInspectModuleDef]
# inspect2: --show=definition --include-kind tmp/foo.py:2:1
Expand Down
6 changes: 6 additions & 0 deletions test-data/unit/pythoneval.test
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,20 @@ f
[case testModuleAttributes]
import math
import typing
print(type(__spec__))
print(math.__name__)
print(math.__spec__.name)
print(type(math.__dict__))
print(type(math.__doc__ or ''))
print(type(math.__spec__).__name__)
print(math.__class__)
[out]
<class 'NoneType'>
math
math
<class 'dict'>
<class 'str'>
ModuleSpec
<class 'module'>

[case testSpecialAttributes]
Expand Down

0 comments on commit f5afdcd

Please sign in to comment.