Skip to content

Commit f9dadb1

Browse files
authored
Fix __annotations__ being undefined (#10969)
1 parent 61b621b commit f9dadb1

File tree

4 files changed

+16
-23
lines changed

4 files changed

+16
-23
lines changed

mypy/nodes.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,8 @@ def get_column(self) -> int:
106106
'__doc__': None, # depends on Python version, see semanal.py
107107
'__path__': None, # depends on if the module is a package
108108
'__file__': '__builtins__.str',
109-
'__package__': '__builtins__.str'
109+
'__package__': '__builtins__.str',
110+
'__annotations__': None, # dict[str, Any] bounded in add_implicit_module_attrs()
110111
}
111112

112113

mypy/semanal.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,6 +447,13 @@ def add_implicit_module_attrs(self, file_node: MypyFile) -> None:
447447
node = sym.node
448448
assert isinstance(node, TypeInfo)
449449
typ = Instance(node, [self.str_type()])
450+
elif name == '__annotations__':
451+
sym = self.lookup_qualified("__builtins__.dict", Context(), suppress_errors=True)
452+
if not sym:
453+
continue
454+
node = sym.node
455+
assert isinstance(node, TypeInfo)
456+
typ = Instance(node, [self.str_type(), AnyType(TypeOfAny.special_form)])
450457
else:
451458
assert t is not None, 'type should be specified for {}'.format(name)
452459
typ = UnboundType(t)

mypy/stubtest.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -978,6 +978,7 @@ def verify_typealias(
978978
"__cached__",
979979
"__loader__",
980980
"__spec__",
981+
"__annotations__",
981982
"__path__", # mypy adds __path__ to packages, but C packages don't have it
982983
"__getattr__", # resulting behaviour might be typed explicitly
983984
# TODO: remove the following from this list

test-data/unit/check-basic.test

Lines changed: 6 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -236,32 +236,16 @@ from typing import Any
236236
def f() -> Any:
237237
yield
238238

239-
[case testModule__name__]
239+
[case testModuleImplicitAttributes]
240240
import typing
241-
x = __name__ # type: str
242-
a = __name__ # type: A # E: Incompatible types in assignment (expression has type "str", variable has type "A")
243241
class A: pass
242+
reveal_type(__name__) # N: Revealed type is "builtins.str"
243+
reveal_type(__doc__) # N: Revealed type is "builtins.str"
244+
reveal_type(__file__) # N: Revealed type is "builtins.str"
245+
reveal_type(__package__) # N: Revealed type is "builtins.str"
246+
reveal_type(__annotations__) # N: Revealed type is "builtins.dict[builtins.str, Any]"
244247
[builtins fixtures/primitives.pyi]
245248

246-
[case testModule__doc__]
247-
import typing
248-
x = __doc__ # type: str
249-
a = __doc__ # type: A # E: Incompatible types in assignment (expression has type "str", variable has type "A")
250-
class A: pass
251-
[builtins fixtures/primitives.pyi]
252-
253-
[case testModule__file__]
254-
import typing
255-
x = __file__ # type: str
256-
a = __file__ # type: A # E: Incompatible types in assignment (expression has type "str", variable has type "A")
257-
class A: pass
258-
[builtins fixtures/primitives.pyi]
259-
260-
[case test__package__]
261-
import typing
262-
x = __package__ # type: str
263-
a = __file__ # type: int # E: Incompatible types in assignment (expression has type "str", variable has type "int")
264-
265249

266250
-- Scoping and shadowing
267251
-- ---------------------

0 commit comments

Comments
 (0)