Skip to content

Fix __annotations__ being undefined #10969

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 72 commits into from
Mar 14, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
72 commits
Select commit Hold shift + click to select a range
58ea3ed
Define __annotations__ as a dictionary
Nickedude Aug 10, 2021
1784e99
Set type of __annotations__ to a dict mapping strings to types
Nickedude Aug 10, 2021
f6834b8
Add dict to builtins stub
Nickedude Aug 11, 2021
ec99381
Add dict stubs to check-basic.test
Nickedude Aug 11, 2021
671ffbb
Add dict stubs to check-python38.test
Nickedude Aug 11, 2021
fb3b0bc
Add dict stubs to check-union-or-syntax.test
Nickedude Aug 11, 2021
3a41480
Use dict fixture instead of bool fixture for testNoneHasBool
Nickedude Aug 12, 2021
1b2a1b1
Add dict stub to isinstance fixture
Nickedude Aug 12, 2021
daddad5
Add dict stub to list fixture
Nickedude Aug 12, 2021
47c6bb5
Add dict stub to tuple fixture
Nickedude Aug 12, 2021
e683549
Add dict stub to set fixture
Nickedude Aug 12, 2021
5c07a13
Add dict stub to type fixture
Nickedude Aug 12, 2021
c8604cc
Add dict stub to isinstance_python3_10 fixture
Nickedude Aug 12, 2021
fb6baff
Add dict stub to callable fixture
Nickedude Aug 12, 2021
fac1882
Add dict stub to classmethod fixture
Nickedude Aug 12, 2021
e76637e
Add dict stub to staticmethod fixture
Nickedude Aug 12, 2021
7622856
Add dict stub to attr fixture
Nickedude Aug 12, 2021
28aa161
Use single instead of double quotes in definition of __annotations__
Nickedude Aug 13, 2021
31e2ab6
Add dict stub to __new__ fixture
Nickedude Aug 13, 2021
e7eec43
Add dict stub to for fixture
Nickedude Aug 13, 2021
c4d0750
Add dict stub to ops fixtures
Nickedude Aug 13, 2021
762991c
Add dict to complex fixture
Nickedude Aug 13, 2021
40942ee
Add dict stub to __builtin__ fixture
Nickedude Aug 13, 2021
3a7fe9f
Add __iterator__ to dict stub in tuple fixture
Nickedude Aug 13, 2021
26f34a2
Add dict stub to bool fixture
Nickedude Aug 13, 2021
5a5af45
Add dict stub to exception fixture
Nickedude Aug 13, 2021
715cfd4
Add dict stub to float fixture
Nickedude Aug 13, 2021
561b0b5
Add dict stub to testMultipleAssignmentWithListAndIndexing
Nickedude Aug 13, 2021
441ed92
Add dict stubs to tests in check-dynamic-typing.test
Nickedude Aug 13, 2021
881e479
Add __iter__ to dict stub in list fixture
Nickedude Aug 13, 2021
9efa3cd
Add dict stub to function fixture
Nickedude Aug 13, 2021
f3a2fd4
Add dict stub to __init_subclass__ fixture
Nickedude Aug 13, 2021
89b7029
Add dict stub to module_all fixture
Nickedude Aug 13, 2021
7cbc726
Add dict stub to module_all_python2
Nickedude Aug 13, 2021
3a208e7
Add __iter__ to dict stub in isinstance fixture
Nickedude Aug 13, 2021
c413c4d
Add dict stub to slice fixture
Nickedude Aug 13, 2021
6f0dfde
Add dict stub to alias fixture
Nickedude Aug 13, 2021
8066ca9
Add dict stub to notimplemented fixture
Nickedude Aug 13, 2021
9eda63e
Add __iter__ to dict stub in float fixture
Nickedude Aug 13, 2021
b556b64
Add dict stub to object_hashable fixture
Nickedude Aug 13, 2021
8e3eb36
Add dict stub to f_string fixture
Nickedude Aug 13, 2021
38cd546
Add dict stub to divmod fixture
Nickedude Aug 13, 2021
0e57252
Add dict stub to bool_py2 fixture
Nickedude Aug 13, 2021
f82b7b3
Add dict stub to property_py2 fixture
Nickedude Aug 13, 2021
727602f
Add dict stubs to tests in check-expressions.test
Nickedude Aug 13, 2021
74f5f56
Add dict stubs to tests in check-tuples.test
Nickedude Aug 13, 2021
c124ddd
Add return type to __iter__ in dict stub in list fixture
Nickedude Aug 13, 2021
8d1a139
Remove testDictMissingFromStubs
Nickedude Aug 13, 2021
4881a7f
Add dict stub to complex_tuple fixture
Nickedude Aug 13, 2021
f49c4f0
Add dict stub to fine_grained fixture
Nickedude Aug 13, 2021
7a68cc7
Add dict stub to tuple-simple fixture
Nickedude Aug 13, 2021
89ff0d5
Add dict stubs to tests in typexport-basic.test
Nickedude Aug 13, 2021
8c1c975
Set value of __annotations__ to AnyType
Nickedude Aug 13, 2021
ce035ad
Fix merge.test
Nickedude Aug 13, 2021
9498d8b
Fix testDecoratorTypeAfterReprocessing
Nickedude Aug 17, 2021
8f1380c
Merge branch 'master' into fix__annotations__is_undefined
Nickedude Sep 26, 2021
54afcee
Add unittest revealing type of __annotations__
Nickedude Sep 26, 2021
b706550
Merge branch 'master' into fix__annotations__is_undefined
Nickedude Oct 23, 2021
545abd8
Merge branch 'master' into fix__annotations__is_undefined
Nickedude Nov 7, 2021
de1f56f
Only define __annotations__ if dict is defined
niklas-zen Dec 11, 2021
2b3ab61
Revert adding dict to most fixtures
niklas-zen Dec 11, 2021
fdd7a71
Bring back testDictMissingFromStubs
niklas-zen Dec 11, 2021
ea7cc08
Minor cleanup
niklas-zen Dec 11, 2021
b0a59aa
Remove dict stub from check-dynamic-typing.test
niklas-zen Dec 11, 2021
5d15537
Remove dict stub from testMultipleAssignmentWithListAndIndexing
niklas-zen Dec 11, 2021
9dce590
Remove dict from typeexport-basic.test
niklas-zen Dec 11, 2021
ab18254
Remove dict stub from testTupleMethods
niklas-zen Dec 12, 2021
e75ab3b
Remove dict stub from testDecoratorTypeAfterReprocessing and list.pyi
niklas-zen Dec 12, 2021
fc48ca5
Remove dict stub from tuple.pyi and merge.test
niklas-zen Dec 12, 2021
b7bf39f
Merge test cases
97littleleaf11 Mar 13, 2022
d37965b
Merge branch 'master' of https://github.com/python/mypy into HEAD
97littleleaf11 Mar 13, 2022
18ac989
Fix stubtest
97littleleaf11 Mar 14, 2022
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
3 changes: 2 additions & 1 deletion mypy/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,8 @@ def get_column(self) -> int:
'__doc__': None, # depends on Python version, see semanal.py
'__path__': None, # depends on if the module is a package
'__file__': '__builtins__.str',
'__package__': '__builtins__.str'
'__package__': '__builtins__.str',
'__annotations__': None, # dict[str, Any] bounded in add_implicit_module_attrs()
}


Expand Down
7 changes: 7 additions & 0 deletions mypy/semanal.py
Original file line number Diff line number Diff line change
Expand Up @@ -447,6 +447,13 @@ def add_implicit_module_attrs(self, file_node: MypyFile) -> None:
node = sym.node
assert isinstance(node, TypeInfo)
typ = Instance(node, [self.str_type()])
elif name == '__annotations__':
sym = self.lookup_qualified("__builtins__.dict", Context(), suppress_errors=True)
if not sym:
continue
node = sym.node
assert isinstance(node, TypeInfo)
typ = Instance(node, [self.str_type(), AnyType(TypeOfAny.special_form)])
else:
assert t is not None, 'type should be specified for {}'.format(name)
typ = UnboundType(t)
Expand Down
1 change: 1 addition & 0 deletions mypy/stubtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -978,6 +978,7 @@ def verify_typealias(
"__cached__",
"__loader__",
"__spec__",
"__annotations__",
"__path__", # mypy adds __path__ to packages, but C packages don't have it
"__getattr__", # resulting behaviour might be typed explicitly
# TODO: remove the following from this list
Expand Down
28 changes: 6 additions & 22 deletions test-data/unit/check-basic.test
Original file line number Diff line number Diff line change
Expand Up @@ -236,32 +236,16 @@ from typing import Any
def f() -> Any:
yield

[case testModule__name__]
[case testModuleImplicitAttributes]
import typing
x = __name__ # type: str
a = __name__ # type: A # E: Incompatible types in assignment (expression has type "str", variable has type "A")
class A: pass
reveal_type(__name__) # N: Revealed type is "builtins.str"
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]"
[builtins fixtures/primitives.pyi]

[case testModule__doc__]
import typing
x = __doc__ # type: str
a = __doc__ # type: A # E: Incompatible types in assignment (expression has type "str", variable has type "A")
class A: pass
[builtins fixtures/primitives.pyi]

[case testModule__file__]
import typing
x = __file__ # type: str
a = __file__ # type: A # E: Incompatible types in assignment (expression has type "str", variable has type "A")
class A: pass
[builtins fixtures/primitives.pyi]

[case test__package__]
import typing
x = __package__ # type: str
a = __file__ # type: int # E: Incompatible types in assignment (expression has type "str", variable has type "int")


-- Scoping and shadowing
-- ---------------------
Expand Down