From 58ea3ed3ca32331e0ef3ddd645b85df6724bc2c4 Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Tue, 10 Aug 2021 11:53:27 +0200 Subject: [PATCH 01/68] Define __annotations__ as a dictionary --- mypy/nodes.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/mypy/nodes.py b/mypy/nodes.py index ce0286db84f6..97f89153d312 100644 --- a/mypy/nodes.py +++ b/mypy/nodes.py @@ -103,7 +103,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__": "__builtins__.dict", } From 1784e99c20bb96bdf453810881b39310d3eb88b3 Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Tue, 10 Aug 2021 11:54:02 +0200 Subject: [PATCH 02/68] Set type of __annotations__ to a dict mapping strings to types --- mypy/semanal.py | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/mypy/semanal.py b/mypy/semanal.py index 49ec5c88f30f..18d75e6fbd28 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -438,6 +438,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()) + if not sym: + continue + node = sym.node + assert isinstance(node, TypeInfo) + typ = Instance(node, [self.str_type(), self.named_type('__builtins__.type')]) else: assert t is not None, 'type should be specified for {}'.format(name) typ = UnboundType(t) From f6834b8e01e26d0e1c7b0b3dde3326265633d8fd Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Wed, 11 Aug 2021 16:51:25 +0200 Subject: [PATCH 03/68] Add dict to builtins stub --- test-data/unit/lib-stub/builtins.pyi | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test-data/unit/lib-stub/builtins.pyi b/test-data/unit/lib-stub/builtins.pyi index 8c4f504fb2e7..e97d3853d400 100644 --- a/test-data/unit/lib-stub/builtins.pyi +++ b/test-data/unit/lib-stub/builtins.pyi @@ -20,8 +20,13 @@ class bytes: pass class function: pass class ellipsis: pass -from typing import Generic, Sequence, TypeVar +from typing import Generic, Sequence, TypeVar, Mapping + _T = TypeVar('_T') class list(Generic[_T], Sequence[_T]): pass +_KT = TypeVar('_KT') +_VT = TypeVar('_VT') +class dict(Mapping[_KT, _VT]): pass + # Definition of None is implicit From ec99381cb784464c46461c834edff2fd0f8dcd28 Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Wed, 11 Aug 2021 17:41:02 +0200 Subject: [PATCH 04/68] Add dict stubs to check-basic.test --- test-data/unit/check-basic.test | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test-data/unit/check-basic.test b/test-data/unit/check-basic.test index 184c9e58ca58..89c363b7917f 100644 --- a/test-data/unit/check-basic.test +++ b/test-data/unit/check-basic.test @@ -369,6 +369,7 @@ if isinstance(x, B): pass [file mock.py] [builtins fixtures/isinstance.pyi] +[builtins fixtures/dict.pyi] [out] [case testModuleAsTypeNoCrash2] @@ -387,6 +388,7 @@ x: Any f(x) [file mock.py] [builtins fixtures/isinstance.pyi] +[builtins fixtures/dict.pyi] [out] [case testPartialTypeComments] @@ -403,6 +405,7 @@ none = None b = none.__bool__() reveal_type(b) # N: Revealed type is "builtins.bool" [builtins fixtures/bool.pyi] +[builtins fixtures/dict.pyi] [case testAssignmentInvariantNoteForList] from typing import List @@ -412,6 +415,7 @@ y = x # E: Incompatible types in assignment (expression has type "List[int]", va # N: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \ # N: Consider using "Sequence" instead, which is covariant [builtins fixtures/list.pyi] +[builtins fixtures/dict.pyi] [case testAssignmentInvariantNoteForDict] from typing import Dict From 671ffbbbfe825bc19bea65199a7bdd346a44201c Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Wed, 11 Aug 2021 17:49:47 +0200 Subject: [PATCH 05/68] Add dict stubs to check-python38.test --- test-data/unit/check-python38.test | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test-data/unit/check-python38.test b/test-data/unit/check-python38.test index 7bcef0c498f6..001c050b95de 100644 --- a/test-data/unit/check-python38.test +++ b/test-data/unit/check-python38.test @@ -52,6 +52,7 @@ f( "IGNORE", ] [builtins fixtures/list.pyi] +[builtins fixtures/dict.pyi] [case testIgnoreScopeNested3] { @@ -78,6 +79,7 @@ def g(x: int): ... f("ERROR"), # E: Argument 1 to "f" has incompatible type "str"; expected "int" ) [builtins fixtures/tuple.pyi] +[builtins fixtures/dict.pyi] [case testIgnoreScopeNestedOverlapping] def f(x: int): ... @@ -88,6 +90,7 @@ def g(x: int): ... ), f("ERROR"), # E: Argument 1 to "f" has incompatible type "str"; expected "int" ) [builtins fixtures/tuple.pyi] +[builtins fixtures/dict.pyi] [case testIgnoreScopeUnused1] # flags: --warn-unused-ignores @@ -399,6 +402,7 @@ def check_partial_list() -> None: z.append(3) reveal_type(z) # N: Revealed type is "builtins.list[builtins.int]" [builtins fixtures/list.pyi] +[builtins fixtures/dict.pyi] [case testWalrusExpr] def func() -> None: @@ -418,3 +422,4 @@ def main(a: object) -> None: reveal_type(x) # N: Revealed type is "builtins.float" reveal_type(a) # N: Revealed type is "builtins.object" [builtins fixtures/tuple.pyi] +[builtins fixtures/dict.pyi] From fb3b0bcebd2602e7a9b81d8246a04a0593deb502 Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Wed, 11 Aug 2021 18:04:50 +0200 Subject: [PATCH 06/68] Add dict stubs to check-union-or-syntax.test --- test-data/unit/check-union-or-syntax.test | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/test-data/unit/check-union-or-syntax.test b/test-data/unit/check-union-or-syntax.test index 3e5a19b8fe61..cfb5adff08e6 100644 --- a/test-data/unit/check-union-or-syntax.test +++ b/test-data/unit/check-union-or-syntax.test @@ -10,6 +10,7 @@ def f(x: int | str) -> int | str: return x reveal_type(f) # N: Revealed type is "def (x: Union[builtins.int, builtins.str]) -> Union[builtins.int, builtins.str]" [builtins fixtures/tuple.pyi] +[builtins fixtures/dict.pyi] [case testUnionOrSyntaxWithThreeBuiltinsTypes] # flags: --python-version 3.10 @@ -48,6 +49,7 @@ reveal_type(f) # N: Revealed type is "def (x: Union[__main__.A, __main__.B, __m from typing_extensions import Literal reveal_type(Literal[4] | str) # N: Revealed type is "Any" [builtins fixtures/tuple.pyi] +[builtins fixtures/dict.pyi] [case testUnionOrSyntaxWithBadOperator] # flags: --python-version 3.10 @@ -65,6 +67,7 @@ from typing import List x: List[int | str] reveal_type(x) # N: Revealed type is "builtins.list[Union[builtins.int, builtins.str]]" [builtins fixtures/list.pyi] +[builtins fixtures/dict.pyi] [case testUnionOrSyntaxWithQuotedFunctionTypes] # flags: --python-version 3.4 @@ -102,6 +105,7 @@ X = int | str | f() b: X # E: Variable "__main__.X" is not valid as a type \ # N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases [builtins fixtures/type.pyi] +[builtins fixtures/dict.pyi] [case testUnionOrSyntaxWithinRuntimeContextNotAllowed] # flags: --python-version 3.9 @@ -114,6 +118,7 @@ class C(List[int | str]): # E: Type expected within [...] \ pass C() [builtins fixtures/tuple.pyi] +[builtins fixtures/dict.pyi] [case testUnionOrSyntaxWithinRuntimeContextNotAllowed2] # flags: --python-version 3.9 @@ -121,6 +126,7 @@ from __future__ import annotations from typing import cast cast(str | int, 'x') # E: Cast target is not a type [builtins fixtures/tuple.pyi] +[builtins fixtures/dict.pyi] [typing fixtures/typing-full.pyi] [case testUnionOrSyntaxInComment] @@ -132,6 +138,7 @@ x = 1 # type: int | str from __future__ import annotations x: int | None [builtins fixtures/tuple.pyi] +[builtins fixtures/dict.pyi] [case testUnionOrSyntaxMissingFutureImport] # flags: --python-version 3.9 @@ -155,6 +162,7 @@ def f() -> object: pass reveal_type(cast(str | None, f())) # N: Revealed type is "Union[builtins.str, None]" reveal_type(list[str | None]()) # N: Revealed type is "builtins.list[Union[builtins.str, None]]" [builtins fixtures/type.pyi] +[builtins fixtures/dict.pyi] [case testUnionOrSyntaxRuntimeContextInStubFile] import lib @@ -169,6 +177,7 @@ y: B class C(list[int | None]): pass [builtins fixtures/list.pyi] +[builtins fixtures/dict.pyi] [case testUnionOrSyntaxInIsinstance] # flags: --python-version 3.10 @@ -186,6 +195,7 @@ def g(x: int | str | tuple[int, str] | C) -> None: else: reveal_type(x) # N: Revealed type is "__main__.C" [builtins fixtures/isinstance_python3_10.pyi] +[builtins fixtures/dict.pyi] [case testUnionOrSyntaxInIsinstanceNotSupported] # flags: --python-version 3.9 @@ -196,3 +206,4 @@ def f(x: Union[int, str, None]) -> None: else: reveal_type(x) # N: Revealed type is "None" [builtins fixtures/isinstance.pyi] +[builtins fixtures/dict.pyi] From 3a41480781d12ca47defebff04ec5f8aeebf2118 Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Thu, 12 Aug 2021 14:48:12 +0200 Subject: [PATCH 07/68] Use dict fixture instead of bool fixture for testNoneHasBool --- test-data/unit/check-basic.test | 1 - 1 file changed, 1 deletion(-) diff --git a/test-data/unit/check-basic.test b/test-data/unit/check-basic.test index 89c363b7917f..32dc7a562220 100644 --- a/test-data/unit/check-basic.test +++ b/test-data/unit/check-basic.test @@ -404,7 +404,6 @@ def foo( none = None b = none.__bool__() reveal_type(b) # N: Revealed type is "builtins.bool" -[builtins fixtures/bool.pyi] [builtins fixtures/dict.pyi] [case testAssignmentInvariantNoteForList] From 1b2a1b1ab2419b3d96f07f0191d23c67326844b0 Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Thu, 12 Aug 2021 14:49:13 +0200 Subject: [PATCH 08/68] Add dict stub to isinstance fixture --- test-data/unit/check-basic.test | 2 -- test-data/unit/check-union-or-syntax.test | 1 - test-data/unit/fixtures/isinstance.pyi | 6 +++++- 3 files changed, 5 insertions(+), 4 deletions(-) diff --git a/test-data/unit/check-basic.test b/test-data/unit/check-basic.test index 32dc7a562220..5f05205fe998 100644 --- a/test-data/unit/check-basic.test +++ b/test-data/unit/check-basic.test @@ -369,7 +369,6 @@ if isinstance(x, B): pass [file mock.py] [builtins fixtures/isinstance.pyi] -[builtins fixtures/dict.pyi] [out] [case testModuleAsTypeNoCrash2] @@ -388,7 +387,6 @@ x: Any f(x) [file mock.py] [builtins fixtures/isinstance.pyi] -[builtins fixtures/dict.pyi] [out] [case testPartialTypeComments] diff --git a/test-data/unit/check-union-or-syntax.test b/test-data/unit/check-union-or-syntax.test index cfb5adff08e6..d208739d3606 100644 --- a/test-data/unit/check-union-or-syntax.test +++ b/test-data/unit/check-union-or-syntax.test @@ -206,4 +206,3 @@ def f(x: Union[int, str, None]) -> None: else: reveal_type(x) # N: Revealed type is "None" [builtins fixtures/isinstance.pyi] -[builtins fixtures/dict.pyi] diff --git a/test-data/unit/fixtures/isinstance.pyi b/test-data/unit/fixtures/isinstance.pyi index 7f7cf501b5de..de5a58e87165 100644 --- a/test-data/unit/fixtures/isinstance.pyi +++ b/test-data/unit/fixtures/isinstance.pyi @@ -1,6 +1,8 @@ -from typing import Tuple, TypeVar, Generic, Union, cast, Any, Type +from typing import Tuple, TypeVar, Generic, Union, cast, Any, Type, Mapping T = TypeVar('T') +KT = TypeVar('KT') +VT = TypeVar('VT') class object: def __init__(self) -> None: pass @@ -10,6 +12,8 @@ class type: class tuple(Generic[T]): pass +class dict(Mapping[KT, VT]): pass + class function: pass def isinstance(x: object, t: Union[Type[object], Tuple[Type[object], ...]]) -> bool: pass From daddad59759dd09fde849d89aeba2538acc47876 Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Thu, 12 Aug 2021 14:54:06 +0200 Subject: [PATCH 09/68] Add dict stub to list fixture --- test-data/unit/check-basic.test | 1 - test-data/unit/check-python38.test | 2 -- test-data/unit/check-union-or-syntax.test | 2 -- test-data/unit/fixtures/list.pyi | 5 ++++- 4 files changed, 4 insertions(+), 6 deletions(-) diff --git a/test-data/unit/check-basic.test b/test-data/unit/check-basic.test index 5f05205fe998..d575640a3154 100644 --- a/test-data/unit/check-basic.test +++ b/test-data/unit/check-basic.test @@ -412,7 +412,6 @@ y = x # E: Incompatible types in assignment (expression has type "List[int]", va # N: "List" is invariant -- see https://mypy.readthedocs.io/en/stable/common_issues.html#variance \ # N: Consider using "Sequence" instead, which is covariant [builtins fixtures/list.pyi] -[builtins fixtures/dict.pyi] [case testAssignmentInvariantNoteForDict] from typing import Dict diff --git a/test-data/unit/check-python38.test b/test-data/unit/check-python38.test index 001c050b95de..6f4721ee0b57 100644 --- a/test-data/unit/check-python38.test +++ b/test-data/unit/check-python38.test @@ -52,7 +52,6 @@ f( "IGNORE", ] [builtins fixtures/list.pyi] -[builtins fixtures/dict.pyi] [case testIgnoreScopeNested3] { @@ -402,7 +401,6 @@ def check_partial_list() -> None: z.append(3) reveal_type(z) # N: Revealed type is "builtins.list[builtins.int]" [builtins fixtures/list.pyi] -[builtins fixtures/dict.pyi] [case testWalrusExpr] def func() -> None: diff --git a/test-data/unit/check-union-or-syntax.test b/test-data/unit/check-union-or-syntax.test index d208739d3606..4abb67480bf8 100644 --- a/test-data/unit/check-union-or-syntax.test +++ b/test-data/unit/check-union-or-syntax.test @@ -67,7 +67,6 @@ from typing import List x: List[int | str] reveal_type(x) # N: Revealed type is "builtins.list[Union[builtins.int, builtins.str]]" [builtins fixtures/list.pyi] -[builtins fixtures/dict.pyi] [case testUnionOrSyntaxWithQuotedFunctionTypes] # flags: --python-version 3.4 @@ -177,7 +176,6 @@ y: B class C(list[int | None]): pass [builtins fixtures/list.pyi] -[builtins fixtures/dict.pyi] [case testUnionOrSyntaxInIsinstance] # flags: --python-version 3.10 diff --git a/test-data/unit/fixtures/list.pyi b/test-data/unit/fixtures/list.pyi index c4baf89ffc13..15fd7d82a66f 100644 --- a/test-data/unit/fixtures/list.pyi +++ b/test-data/unit/fixtures/list.pyi @@ -1,14 +1,17 @@ # Builtins stub used in list-related test cases. -from typing import TypeVar, Generic, Iterable, Iterator, Sequence, overload +from typing import TypeVar, Generic, Iterable, Iterator, Sequence, overload, Mapping T = TypeVar('T') +KT = TypeVar('KT') +VT = TypeVar('VT') class object: def __init__(self) -> None: pass class type: pass class ellipsis: pass +class dict(Mapping[KT, VT]): pass class list(Sequence[T]): @overload From 47c6bb5ad640a3cf508cb61d6797c2e1933e27c3 Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Thu, 12 Aug 2021 15:11:15 +0200 Subject: [PATCH 10/68] Add dict stub to tuple fixture --- test-data/unit/check-python38.test | 1 - test-data/unit/check-union-or-syntax.test | 5 ----- test-data/unit/fixtures/tuple.pyi | 6 +++++- 3 files changed, 5 insertions(+), 7 deletions(-) diff --git a/test-data/unit/check-python38.test b/test-data/unit/check-python38.test index 6f4721ee0b57..9c05f58c316d 100644 --- a/test-data/unit/check-python38.test +++ b/test-data/unit/check-python38.test @@ -420,4 +420,3 @@ def main(a: object) -> None: reveal_type(x) # N: Revealed type is "builtins.float" reveal_type(a) # N: Revealed type is "builtins.object" [builtins fixtures/tuple.pyi] -[builtins fixtures/dict.pyi] diff --git a/test-data/unit/check-union-or-syntax.test b/test-data/unit/check-union-or-syntax.test index 4abb67480bf8..dfb841d89279 100644 --- a/test-data/unit/check-union-or-syntax.test +++ b/test-data/unit/check-union-or-syntax.test @@ -10,7 +10,6 @@ def f(x: int | str) -> int | str: return x reveal_type(f) # N: Revealed type is "def (x: Union[builtins.int, builtins.str]) -> Union[builtins.int, builtins.str]" [builtins fixtures/tuple.pyi] -[builtins fixtures/dict.pyi] [case testUnionOrSyntaxWithThreeBuiltinsTypes] # flags: --python-version 3.10 @@ -49,7 +48,6 @@ reveal_type(f) # N: Revealed type is "def (x: Union[__main__.A, __main__.B, __m from typing_extensions import Literal reveal_type(Literal[4] | str) # N: Revealed type is "Any" [builtins fixtures/tuple.pyi] -[builtins fixtures/dict.pyi] [case testUnionOrSyntaxWithBadOperator] # flags: --python-version 3.10 @@ -117,7 +115,6 @@ class C(List[int | str]): # E: Type expected within [...] \ pass C() [builtins fixtures/tuple.pyi] -[builtins fixtures/dict.pyi] [case testUnionOrSyntaxWithinRuntimeContextNotAllowed2] # flags: --python-version 3.9 @@ -125,7 +122,6 @@ from __future__ import annotations from typing import cast cast(str | int, 'x') # E: Cast target is not a type [builtins fixtures/tuple.pyi] -[builtins fixtures/dict.pyi] [typing fixtures/typing-full.pyi] [case testUnionOrSyntaxInComment] @@ -137,7 +133,6 @@ x = 1 # type: int | str from __future__ import annotations x: int | None [builtins fixtures/tuple.pyi] -[builtins fixtures/dict.pyi] [case testUnionOrSyntaxMissingFutureImport] # flags: --python-version 3.9 diff --git a/test-data/unit/fixtures/tuple.pyi b/test-data/unit/fixtures/tuple.pyi index 36c53de619be..aaa0a8f5e40b 100644 --- a/test-data/unit/fixtures/tuple.pyi +++ b/test-data/unit/fixtures/tuple.pyi @@ -1,6 +1,6 @@ # Builtins stub used in tuple-related test cases. -from typing import Iterable, Iterator, TypeVar, Generic, Sequence, Any, overload, Tuple +from typing import Iterable, Iterator, TypeVar, Generic, Sequence, Any, overload, Tuple, Mapping Tco = TypeVar('Tco', covariant=True) @@ -32,6 +32,8 @@ class bytes: pass class unicode: pass T = TypeVar('T') +KT = TypeVar('KT') +VT = TypeVar('VT') class list(Sequence[T], Generic[T]): @overload @@ -41,6 +43,8 @@ class list(Sequence[T], Generic[T]): def __contains__(self, item: object) -> bool: ... def __iter__(self) -> Iterator[T]: ... +class dict(Mapping[KT, VT]): pass + def isinstance(x: object, t: type) -> bool: pass def sum(iterable: Iterable[T], start: T = None) -> T: pass From e683549ccbb356c77859ddcf5ac5ef9fac73db27 Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Thu, 12 Aug 2021 15:12:44 +0200 Subject: [PATCH 11/68] Add dict stub to set fixture --- test-data/unit/fixtures/set.pyi | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test-data/unit/fixtures/set.pyi b/test-data/unit/fixtures/set.pyi index c2e1f6f75237..bb50afc454ee 100644 --- a/test-data/unit/fixtures/set.pyi +++ b/test-data/unit/fixtures/set.pyi @@ -1,14 +1,17 @@ # Builtins stub used in set-related test cases. -from typing import TypeVar, Generic, Iterator, Iterable, Set +from typing import TypeVar, Generic, Iterator, Iterable, Set, Mapping T = TypeVar('T') +KT = TypeVar('KT') +VT = TypeVar('VT') class object: def __init__(self) -> None: pass class type: pass class tuple(Generic[T]): pass +class dict(Mapping[KT, VT]): pass class function: pass class int: pass From 5c07a1385bc3d743e62aff52b934531f98b66bcf Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Thu, 12 Aug 2021 15:19:33 +0200 Subject: [PATCH 12/68] Add dict stub to type fixture --- test-data/unit/check-union-or-syntax.test | 2 -- test-data/unit/fixtures/type.pyi | 5 ++++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/test-data/unit/check-union-or-syntax.test b/test-data/unit/check-union-or-syntax.test index dfb841d89279..e5aee086d34e 100644 --- a/test-data/unit/check-union-or-syntax.test +++ b/test-data/unit/check-union-or-syntax.test @@ -102,7 +102,6 @@ X = int | str | f() b: X # E: Variable "__main__.X" is not valid as a type \ # N: See https://mypy.readthedocs.io/en/stable/common_issues.html#variables-vs-type-aliases [builtins fixtures/type.pyi] -[builtins fixtures/dict.pyi] [case testUnionOrSyntaxWithinRuntimeContextNotAllowed] # flags: --python-version 3.9 @@ -156,7 +155,6 @@ def f() -> object: pass reveal_type(cast(str | None, f())) # N: Revealed type is "Union[builtins.str, None]" reveal_type(list[str | None]()) # N: Revealed type is "builtins.list[Union[builtins.str, None]]" [builtins fixtures/type.pyi] -[builtins fixtures/dict.pyi] [case testUnionOrSyntaxRuntimeContextInStubFile] import lib diff --git a/test-data/unit/fixtures/type.pyi b/test-data/unit/fixtures/type.pyi index 755b45ff0bb5..81e8e58eb564 100644 --- a/test-data/unit/fixtures/type.pyi +++ b/test-data/unit/fixtures/type.pyi @@ -1,14 +1,17 @@ # builtins stub used in type-related test cases. -from typing import Generic, TypeVar, List, Union +from typing import Generic, TypeVar, List, Union, Mapping T = TypeVar('T') +KT = TypeVar('KT') +VT = TypeVar('VT') class object: def __init__(self) -> None: pass def __str__(self) -> 'str': pass class list(Generic[T]): pass +class dict(Mapping[KT, VT]): pass class type(Generic[T]): __name__: str From c8604ccfa728722ead103586b29780552b2aefc8 Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Thu, 12 Aug 2021 15:23:22 +0200 Subject: [PATCH 13/68] Add dict stub to isinstance_python3_10 fixture --- test-data/unit/check-union-or-syntax.test | 1 - test-data/unit/fixtures/isinstance_python3_10.pyi | 6 +++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/test-data/unit/check-union-or-syntax.test b/test-data/unit/check-union-or-syntax.test index e5aee086d34e..3e5a19b8fe61 100644 --- a/test-data/unit/check-union-or-syntax.test +++ b/test-data/unit/check-union-or-syntax.test @@ -186,7 +186,6 @@ def g(x: int | str | tuple[int, str] | C) -> None: else: reveal_type(x) # N: Revealed type is "__main__.C" [builtins fixtures/isinstance_python3_10.pyi] -[builtins fixtures/dict.pyi] [case testUnionOrSyntaxInIsinstanceNotSupported] # flags: --python-version 3.9 diff --git a/test-data/unit/fixtures/isinstance_python3_10.pyi b/test-data/unit/fixtures/isinstance_python3_10.pyi index abb37ea81c00..6e466c289677 100644 --- a/test-data/unit/fixtures/isinstance_python3_10.pyi +++ b/test-data/unit/fixtures/isinstance_python3_10.pyi @@ -1,8 +1,10 @@ # For Python 3.10+ only -from typing import Tuple, TypeVar, Generic, Union, cast, Any, Type +from typing import Tuple, TypeVar, Generic, Union, cast, Any, Type, Mapping import types T = TypeVar('T') +KT = TypeVar('KT') +VT = TypeVar('VT') class object: def __init__(self) -> None: pass @@ -13,6 +15,8 @@ class type(Generic[T]): class tuple(Generic[T]): pass +class dict(Mapping[KT, VT]): pass + class function: pass def isinstance(x: object, t: Union[Type[object], Tuple[Type[object], ...], types.Union]) -> bool: pass From fb6baffa5cfcab31b3543546cc1b0a6270e75541 Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Thu, 12 Aug 2021 15:36:21 +0200 Subject: [PATCH 14/68] Add dict stub to callable fixture --- test-data/unit/fixtures/callable.pyi | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test-data/unit/fixtures/callable.pyi b/test-data/unit/fixtures/callable.pyi index 7d4757df4bf5..c345def2f046 100644 --- a/test-data/unit/fixtures/callable.pyi +++ b/test-data/unit/fixtures/callable.pyi @@ -1,6 +1,8 @@ -from typing import Generic, Tuple, TypeVar, Union +from typing import Generic, Tuple, TypeVar, Union, Mapping T = TypeVar('T') +KT = TypeVar('KT') +VT = TypeVar('VT') class object: def __init__(self) -> None: pass @@ -10,6 +12,8 @@ class type: class tuple(Generic[T]): pass +class dict(Mapping[KT, VT]): pass + class function: pass def isinstance(x: object, t: Union[type, Tuple[type, ...]]) -> bool: pass From fac1882e62059f58f1afedf43489b0b69123b841 Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Thu, 12 Aug 2021 15:39:58 +0200 Subject: [PATCH 15/68] Add dict stub to classmethod fixture --- test-data/unit/fixtures/classmethod.pyi | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test-data/unit/fixtures/classmethod.pyi b/test-data/unit/fixtures/classmethod.pyi index 03ad803890a3..078f0bae2413 100644 --- a/test-data/unit/fixtures/classmethod.pyi +++ b/test-data/unit/fixtures/classmethod.pyi @@ -1,6 +1,8 @@ import typing _T = typing.TypeVar('_T') +_KT = typing.TypeVar('_KT') +_VT = typing.TypeVar('_VT') class object: def __init__(self) -> None: pass @@ -26,3 +28,4 @@ class bool: pass class ellipsis: pass class tuple(typing.Generic[_T]): pass +class dict(typing.Mapping[_KT, _VT]): pass From e76637ec05ced178e412e89b0048972fdf318a74 Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Thu, 12 Aug 2021 15:41:34 +0200 Subject: [PATCH 16/68] Add dict stub to staticmethod fixture --- test-data/unit/fixtures/staticmethod.pyi | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test-data/unit/fixtures/staticmethod.pyi b/test-data/unit/fixtures/staticmethod.pyi index 7d5d98634e48..58fe8d5a6792 100644 --- a/test-data/unit/fixtures/staticmethod.pyi +++ b/test-data/unit/fixtures/staticmethod.pyi @@ -1,5 +1,8 @@ import typing +_KT = typing.TypeVar('_KT') +_VT = typing.TypeVar('_VT') + class object: def __init__(self) -> None: pass @@ -8,6 +11,8 @@ class type: class function: pass +class dict(typing.Mapping[_KT, _VT]): pass + staticmethod = object() # Dummy definition. property = object() # Dummy definition From 7622856f4e418f8e6bd9291883c68e8cdfbf7440 Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Thu, 12 Aug 2021 15:44:36 +0200 Subject: [PATCH 17/68] Add dict stub to attr fixture --- test-data/unit/fixtures/attr.pyi | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test-data/unit/fixtures/attr.pyi b/test-data/unit/fixtures/attr.pyi index deb1906d931e..3a4d7c996697 100644 --- a/test-data/unit/fixtures/attr.pyi +++ b/test-data/unit/fixtures/attr.pyi @@ -1,5 +1,8 @@ # Builtins stub used to support @attr.s tests. -from typing import Union, overload +from typing import Union, overload, Mapping, TypeVar + +_KT = TypeVar('_KT') +_VT = TypeVar('_VT') class object: def __init__(self) -> None: pass @@ -25,3 +28,4 @@ class complex: class str: pass class unicode: pass class ellipsis: pass +class dict(Mapping[_KT, _VT]): pass From 28aa161a360847ad13e114fb6cd53f887deb3f59 Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Fri, 13 Aug 2021 14:54:01 +0200 Subject: [PATCH 18/68] Use single instead of double quotes in definition of __annotations__ --- mypy/nodes.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy/nodes.py b/mypy/nodes.py index 97f89153d312..ecd908a939b4 100644 --- a/mypy/nodes.py +++ b/mypy/nodes.py @@ -104,7 +104,7 @@ def get_column(self) -> int: '__path__': None, # depends on if the module is a package '__file__': '__builtins__.str', '__package__': '__builtins__.str', - "__annotations__": "__builtins__.dict", + '__annotations__': '__builtins__.dict', } From 31e2ab65b245c48a137fd9a9e202f862a728166c Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Fri, 13 Aug 2021 14:59:26 +0200 Subject: [PATCH 19/68] Add dict stub to __new__ fixture --- test-data/unit/fixtures/__new__.pyi | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test-data/unit/fixtures/__new__.pyi b/test-data/unit/fixtures/__new__.pyi index bb4788df8fe9..41e4c27733b8 100644 --- a/test-data/unit/fixtures/__new__.pyi +++ b/test-data/unit/fixtures/__new__.pyi @@ -1,6 +1,9 @@ # builtins stub with object.__new__ -from typing import Any +from typing import Any, TypeVar, Mapping + +_KT = TypeVar('_KT') +_VT = TypeVar('_VT') class object: def __init__(self) -> None: pass @@ -16,3 +19,4 @@ class int: pass class bool: pass class str: pass class function: pass +class dict(Mapping[_KT, _VT]): pass From e7eec431fff7374372f76d6b055db68edaa89477 Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Fri, 13 Aug 2021 15:02:28 +0200 Subject: [PATCH 20/68] Add dict stub to for fixture --- test-data/unit/fixtures/for.pyi | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test-data/unit/fixtures/for.pyi b/test-data/unit/fixtures/for.pyi index 31f6de78d486..7b7c853f1b51 100644 --- a/test-data/unit/fixtures/for.pyi +++ b/test-data/unit/fixtures/for.pyi @@ -1,9 +1,11 @@ # builtins stub used in for statement test cases -from typing import TypeVar, Generic, Iterable, Iterator, Generator +from typing import TypeVar, Generic, Iterable, Iterator, Generator, Mapping from abc import abstractmethod, ABCMeta t = TypeVar('t') +KT = TypeVar('KT') +VT = TypeVar('VT') class object: def __init__(self) -> None: pass @@ -18,3 +20,5 @@ class str: pass # for convenience class list(Iterable[t], Generic[t]): def __iter__(self) -> Iterator[t]: pass + +class dict(Mapping[KT, VT]): pass From c4d075055a078d9ea3f985d46d3810586368e061 Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Fri, 13 Aug 2021 15:04:59 +0200 Subject: [PATCH 21/68] Add dict stub to ops fixtures --- test-data/unit/fixtures/ops.pyi | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test-data/unit/fixtures/ops.pyi b/test-data/unit/fixtures/ops.pyi index d5845aba43c6..3bcf3d7eab2f 100644 --- a/test-data/unit/fixtures/ops.pyi +++ b/test-data/unit/fixtures/ops.pyi @@ -1,6 +1,8 @@ -from typing import overload, Any, Generic, Sequence, Tuple, TypeVar, Optional +from typing import overload, Any, Generic, Sequence, Tuple, TypeVar, Optional, Mapping Tco = TypeVar('Tco', covariant=True) +KT = TypeVar('KT') +VT = TypeVar('VT') # This is an extension of transform builtins with additional operations. @@ -22,6 +24,8 @@ class tuple(Sequence[Tco]): def __gt__(self, x: Tuple[Tco, ...]) -> bool: pass def __ge__(self, x: Tuple[Tco, ...]) -> bool: pass +class dict(Mapping[KT, VT]): pass + class function: pass class bool: pass From 762991c87d48bd8dce58be617e2d23451c040ac0 Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Fri, 13 Aug 2021 15:09:33 +0200 Subject: [PATCH 22/68] Add dict to complex fixture --- test-data/unit/fixtures/complex.pyi | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/test-data/unit/fixtures/complex.pyi b/test-data/unit/fixtures/complex.pyi index bcd03a2562e5..5a9fcd0088fd 100644 --- a/test-data/unit/fixtures/complex.pyi +++ b/test-data/unit/fixtures/complex.pyi @@ -1,6 +1,11 @@ # Builtins stub used for some float/complex test cases. # Please don't add tuple to this file, it is used to test incomplete fixtures. +from typing import TypeVar, Mapping + +_KT = TypeVar('_KT') +_VT = TypeVar('_VT') + class object: def __init__(self): pass @@ -10,3 +15,4 @@ class int: pass class float: pass class complex: pass class str: pass +class dict(Mapping[_KT, _VT]): pass From 40942ee18c5f6d01c0fb9c4ebe497c473be65a9f Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Fri, 13 Aug 2021 15:21:57 +0200 Subject: [PATCH 23/68] Add dict stub to __builtin__ fixture --- test-data/unit/lib-stub/__builtin__.pyi | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test-data/unit/lib-stub/__builtin__.pyi b/test-data/unit/lib-stub/__builtin__.pyi index e7109a179aac..b6329ddbe719 100644 --- a/test-data/unit/lib-stub/__builtin__.pyi +++ b/test-data/unit/lib-stub/__builtin__.pyi @@ -1,5 +1,7 @@ -from typing import Generic, TypeVar +from typing import Generic, TypeVar, Mapping _T = TypeVar('_T') +_KT = TypeVar('_KT') +_VT = TypeVar('_VT') Any = 0 @@ -21,6 +23,7 @@ class str: pass class unicode: pass class tuple(Generic[_T]): pass +class dict(Mapping[_KT, _VT]): pass class function: pass class ellipsis: pass From 3a7fe9f9a16fe3ae2dac27a38e7804eeb0cdcd4b Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Fri, 13 Aug 2021 15:27:27 +0200 Subject: [PATCH 24/68] Add __iterator__ to dict stub in tuple fixture --- test-data/unit/fixtures/tuple.pyi | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test-data/unit/fixtures/tuple.pyi b/test-data/unit/fixtures/tuple.pyi index aaa0a8f5e40b..126f68aea30d 100644 --- a/test-data/unit/fixtures/tuple.pyi +++ b/test-data/unit/fixtures/tuple.pyi @@ -43,7 +43,9 @@ class list(Sequence[T], Generic[T]): def __contains__(self, item: object) -> bool: ... def __iter__(self) -> Iterator[T]: ... -class dict(Mapping[KT, VT]): pass +class dict(Mapping[KT, VT]): + def __iter__(self) -> Iterator[KT]: pass + def isinstance(x: object, t: type) -> bool: pass From 26f34a2867615ebc058dd454d45722b202fcd711 Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Fri, 13 Aug 2021 15:42:56 +0200 Subject: [PATCH 25/68] Add dict stub to bool fixture --- test-data/unit/check-basic.test | 2 +- test-data/unit/fixtures/bool.pyi | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/test-data/unit/check-basic.test b/test-data/unit/check-basic.test index d575640a3154..184c9e58ca58 100644 --- a/test-data/unit/check-basic.test +++ b/test-data/unit/check-basic.test @@ -402,7 +402,7 @@ def foo( none = None b = none.__bool__() reveal_type(b) # N: Revealed type is "builtins.bool" -[builtins fixtures/dict.pyi] +[builtins fixtures/bool.pyi] [case testAssignmentInvariantNoteForList] from typing import List diff --git a/test-data/unit/fixtures/bool.pyi b/test-data/unit/fixtures/bool.pyi index ca2564dabafd..ce16ce15a49a 100644 --- a/test-data/unit/fixtures/bool.pyi +++ b/test-data/unit/fixtures/bool.pyi @@ -1,6 +1,9 @@ # builtins stub used in boolean-related test cases. -from typing import Generic, TypeVar +from typing import Generic, TypeVar, Mapping, Iterator + T = TypeVar('T') +KT = TypeVar('KT') +VT = TypeVar('VT') class object: def __init__(self) -> None: pass @@ -17,3 +20,5 @@ class str: pass class unicode: pass class ellipsis: pass class list: pass +class dict(Mapping[KT, VT]): + def __iter__(self) -> Iterator[KT]: pass \ No newline at end of file From 5a5af459788e4f025903e017c51cc64cd91932a2 Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Fri, 13 Aug 2021 15:46:26 +0200 Subject: [PATCH 26/68] Add dict stub to exception fixture --- test-data/unit/fixtures/exception.pyi | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test-data/unit/fixtures/exception.pyi b/test-data/unit/fixtures/exception.pyi index bf6d21c8716e..c9fcbe8ba9d6 100644 --- a/test-data/unit/fixtures/exception.pyi +++ b/test-data/unit/fixtures/exception.pyi @@ -1,11 +1,14 @@ -from typing import Generic, TypeVar +from typing import Generic, TypeVar, Mapping T = TypeVar('T') +KT = TypeVar('KT') +VT = TypeVar('VT') class object: def __init__(self): pass class type: pass class tuple(Generic[T]): pass +class dict(Mapping[KT, VT]): pass class function: pass class int: pass class str: pass From 715cfd46caef66d692c3feceae2209b471703e47 Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Fri, 13 Aug 2021 15:48:28 +0200 Subject: [PATCH 27/68] Add dict stub to float fixture --- test-data/unit/fixtures/float.pyi | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test-data/unit/fixtures/float.pyi b/test-data/unit/fixtures/float.pyi index 880b16a2321b..689ee62658ed 100644 --- a/test-data/unit/fixtures/float.pyi +++ b/test-data/unit/fixtures/float.pyi @@ -1,5 +1,8 @@ -from typing import Generic, TypeVar +from typing import Generic, TypeVar, Mapping + T = TypeVar('T') +KT = TypeVar('KT') +VT = TypeVar('VT') Any = 0 @@ -16,6 +19,7 @@ class str: class bytes: pass class tuple(Generic[T]): pass +class dict(Mapping[KT, VT]): pass class function: pass class ellipsis: pass From 561b0b5f7f5e0ef1545b75ac7dd0a07dc9581f20 Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Fri, 13 Aug 2021 18:54:38 +0200 Subject: [PATCH 28/68] Add dict stub to testMultipleAssignmentWithListAndIndexing --- test-data/unit/check-generics.test | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test-data/unit/check-generics.test b/test-data/unit/check-generics.test index 161b5d72e772..b31f7c751722 100644 --- a/test-data/unit/check-generics.test +++ b/test-data/unit/check-generics.test @@ -1299,11 +1299,14 @@ a[1], a[2] = a class A: pass [file builtins.py] -from typing import TypeVar, Generic, Iterable +from typing import TypeVar, Generic, Iterable, Mapping T = TypeVar('T') +KT = TypeVar('KT') +VT = TypeVar('VT') class object: pass class list(Iterable[T]): def __setitem__(self, x: int, v: T) -> None: pass +class dict(Mapping[KT, VT]): pass class int: pass class type: pass class tuple: pass From 441ed9207e1af7590fa11258ffdcdc25b5b0c30d Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Fri, 13 Aug 2021 19:07:23 +0200 Subject: [PATCH 29/68] Add dict stubs to tests in check-dynamic-typing.test --- test-data/unit/check-dynamic-typing.test | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test-data/unit/check-dynamic-typing.test b/test-data/unit/check-dynamic-typing.test index 137376535b4e..77529c4c25fc 100644 --- a/test-data/unit/check-dynamic-typing.test +++ b/test-data/unit/check-dynamic-typing.test @@ -138,6 +138,9 @@ class C: def __contains__(self, a: A) -> bool: pass [file builtins.py] +from typing import TypeVar, Generic, Iterable, Mapping +KT = TypeVar('KT') +VT = TypeVar('VT') class object: def __init__(self): pass class bool: pass @@ -145,6 +148,7 @@ class int: pass class type: pass class function: pass class str: pass +class dict(Mapping[KT, VT]): pass [case testBinaryOperationsWithDynamicAsRightOperand] from typing import Any @@ -210,6 +214,9 @@ class A: class C: pass [file builtins.py] +from typing import TypeVar, Mapping +KT = TypeVar('KT') +VT = TypeVar('VT') class object: def __init__(self): pass class bool: pass @@ -217,6 +224,7 @@ class int: pass class type: pass class function: pass class str: pass +class dict(Mapping[KT, VT]): pass [case testDynamicWithUnaryExpressions] from typing import Any From 881e479d5deb87f1f43fd1d0164a677e4094becb Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Fri, 13 Aug 2021 19:10:58 +0200 Subject: [PATCH 30/68] Add __iter__ to dict stub in list fixture --- test-data/unit/fixtures/list.pyi | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test-data/unit/fixtures/list.pyi b/test-data/unit/fixtures/list.pyi index 15fd7d82a66f..282a52e8f305 100644 --- a/test-data/unit/fixtures/list.pyi +++ b/test-data/unit/fixtures/list.pyi @@ -11,7 +11,8 @@ class object: class type: pass class ellipsis: pass -class dict(Mapping[KT, VT]): pass +class dict(Mapping[KT, VT]): + def __iter__(self): pass class list(Sequence[T]): @overload From 9efa3cd67649c1af8851880db5677b31108c3ad1 Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Fri, 13 Aug 2021 19:16:53 +0200 Subject: [PATCH 31/68] Add dict stub to function fixture --- test-data/unit/fixtures/function.pyi | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/test-data/unit/fixtures/function.pyi b/test-data/unit/fixtures/function.pyi index c00a7846628a..c4265b92923e 100644 --- a/test-data/unit/fixtures/function.pyi +++ b/test-data/unit/fixtures/function.pyi @@ -1,3 +1,7 @@ +from typing import TypeVar, Mapping +KT = TypeVar('KT') +VT = TypeVar('VT') + class object: def __init__(self): pass @@ -5,3 +9,4 @@ class type: pass class function: pass class int: pass class str: pass +class dict(Mapping[KT, VT]): pass From f3a2fd4f934cd81f9ee987a3bef2872f7bd09ed5 Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Fri, 13 Aug 2021 19:23:53 +0200 Subject: [PATCH 32/68] Add dict stub to __init_subclass__ fixture --- test-data/unit/fixtures/__init_subclass__.pyi | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test-data/unit/fixtures/__init_subclass__.pyi b/test-data/unit/fixtures/__init_subclass__.pyi index 79fd04fd964e..e9853b87de18 100644 --- a/test-data/unit/fixtures/__init_subclass__.pyi +++ b/test-data/unit/fixtures/__init_subclass__.pyi @@ -1,4 +1,7 @@ # builtins stub with object.__init_subclass__ +from typing import TypeVar, Mapping +KT = TypeVar('KT') +VT = TypeVar('VT') class object: def __init_subclass__(cls) -> None: pass @@ -9,3 +12,4 @@ class int: pass class bool: pass class str: pass class function: pass +class dict(Mapping[KT, VT]): pass From 89b7029dc77a0f677c51f4b11da6eeca3f7a10cf Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Fri, 13 Aug 2021 19:29:02 +0200 Subject: [PATCH 33/68] Add dict stub to module_all fixture --- test-data/unit/fixtures/module_all.pyi | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test-data/unit/fixtures/module_all.pyi b/test-data/unit/fixtures/module_all.pyi index 87959fefbff5..4c40efe5c80f 100644 --- a/test-data/unit/fixtures/module_all.pyi +++ b/test-data/unit/fixtures/module_all.pyi @@ -1,7 +1,9 @@ -from typing import Generic, Sequence, TypeVar +from typing import Generic, Sequence, TypeVar, Mapping from types import ModuleType _T = TypeVar('_T') +_KT = TypeVar('_KT') +_VT = TypeVar('_VT') class object: def __init__(self) -> None: pass @@ -15,4 +17,5 @@ class list(Generic[_T], Sequence[_T]): def extend(self, x: Sequence[_T]): pass def __add__(self, rhs: Sequence[_T]) -> list[_T]: pass class tuple(Generic[_T]): pass +class dict(Mapping[_KT, _VT]): pass class ellipsis: pass From 7cbc7261c62aeb51820cbadeb0752a5362d58a8c Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Fri, 13 Aug 2021 19:31:13 +0200 Subject: [PATCH 34/68] Add dict stub to module_all_python2 --- test-data/unit/fixtures/module_all_python2.pyi | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test-data/unit/fixtures/module_all_python2.pyi b/test-data/unit/fixtures/module_all_python2.pyi index 989333c5f41a..c1067f43d58a 100644 --- a/test-data/unit/fixtures/module_all_python2.pyi +++ b/test-data/unit/fixtures/module_all_python2.pyi @@ -1,5 +1,7 @@ -from typing import Generic, Sequence, TypeVar +from typing import Generic, Sequence, TypeVar, Mapping _T = TypeVar('_T') +_KT = TypeVar('_KT') +_VT = TypeVar('_VT') class object: def __init__(self) -> None: pass @@ -13,3 +15,4 @@ class list(Generic[_T], Sequence[_T]): def extend(self, x: Sequence[_T]): pass def __add__(self, rhs: Sequence[_T]) -> list[_T]: pass class tuple(Generic[_T]): pass +class dict(Mapping[_KT, _VT]): pass From 3a208e79d9377ff99dc082d1527a39fb920a59ba Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Fri, 13 Aug 2021 19:37:49 +0200 Subject: [PATCH 35/68] Add __iter__ to dict stub in isinstance fixture --- test-data/unit/fixtures/isinstance.pyi | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test-data/unit/fixtures/isinstance.pyi b/test-data/unit/fixtures/isinstance.pyi index de5a58e87165..651289d3f006 100644 --- a/test-data/unit/fixtures/isinstance.pyi +++ b/test-data/unit/fixtures/isinstance.pyi @@ -12,7 +12,8 @@ class type: class tuple(Generic[T]): pass -class dict(Mapping[KT, VT]): pass +class dict(Mapping[KT, VT]): + def __iter__(self): pass class function: pass From c413c4d47cad11bc78f7cf629f33cc98be93d6df Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Fri, 13 Aug 2021 19:46:12 +0200 Subject: [PATCH 36/68] Add dict stub to slice fixture --- test-data/unit/fixtures/slice.pyi | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test-data/unit/fixtures/slice.pyi b/test-data/unit/fixtures/slice.pyi index 947d49ea09fb..f365a5e4684e 100644 --- a/test-data/unit/fixtures/slice.pyi +++ b/test-data/unit/fixtures/slice.pyi @@ -1,12 +1,15 @@ # Builtins stub used in slicing test cases. -from typing import Generic, TypeVar +from typing import Generic, TypeVar, Mapping T = TypeVar('T') +KT = TypeVar('KT') +VT = TypeVar('VT') class object: def __init__(self): pass class type: pass class tuple(Generic[T]): pass +class dict(Mapping[KT, VT]): pass class function: pass class int: pass From 6f0dfde6239345f9bdc42e672952c9e0a1d6aace Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Fri, 13 Aug 2021 19:50:04 +0200 Subject: [PATCH 37/68] Add dict stub to alias fixture --- test-data/unit/fixtures/alias.pyi | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/test-data/unit/fixtures/alias.pyi b/test-data/unit/fixtures/alias.pyi index 5909cb616794..1cb796f1e8dd 100644 --- a/test-data/unit/fixtures/alias.pyi +++ b/test-data/unit/fixtures/alias.pyi @@ -1,4 +1,7 @@ # Builtins test fixture with a type alias 'bytes' +from typing import TypeVar, Mapping +KT = TypeVar('KT') +VT = TypeVar('VT') class object: def __init__(self) -> None: pass @@ -8,5 +11,6 @@ class type: class int: pass class str: pass class function: pass +class dict(Mapping[KT, VT]): pass bytes = str From 8066ca94b8ad6817c257ef7559f673fde9163f7e Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Fri, 13 Aug 2021 19:53:51 +0200 Subject: [PATCH 38/68] Add dict stub to notimplemented fixture --- test-data/unit/fixtures/notimplemented.pyi | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/test-data/unit/fixtures/notimplemented.pyi b/test-data/unit/fixtures/notimplemented.pyi index e619a6c5ad85..f3024a369384 100644 --- a/test-data/unit/fixtures/notimplemented.pyi +++ b/test-data/unit/fixtures/notimplemented.pyi @@ -1,6 +1,7 @@ # builtins stub used in NotImplemented related cases. -from typing import Any, cast - +from typing import Any, cast, TypeVar, Mapping +KT = TypeVar('KT') +VT = TypeVar('VT') class object: def __init__(self) -> None: pass @@ -10,4 +11,5 @@ class function: pass class bool: pass class int: pass class str: pass +class dict(Mapping[KT, VT]): pass NotImplemented = cast(Any, None) From 9eda63e9685d675a87c78d525995775556c6e57f Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Fri, 13 Aug 2021 20:00:56 +0200 Subject: [PATCH 39/68] Add __iter__ to dict stub in float fixture --- test-data/unit/fixtures/float.pyi | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test-data/unit/fixtures/float.pyi b/test-data/unit/fixtures/float.pyi index 689ee62658ed..f1060f3c9c96 100644 --- a/test-data/unit/fixtures/float.pyi +++ b/test-data/unit/fixtures/float.pyi @@ -19,7 +19,8 @@ class str: class bytes: pass class tuple(Generic[T]): pass -class dict(Mapping[KT, VT]): pass +class dict(Mapping[KT, VT]): + def __iter__(self): pass class function: pass class ellipsis: pass From b556b6484c7c0e5b09f2a39799ab57c9988c8c04 Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Fri, 13 Aug 2021 20:06:16 +0200 Subject: [PATCH 40/68] Add dict stub to object_hashable fixture --- test-data/unit/fixtures/object_hashable.pyi | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test-data/unit/fixtures/object_hashable.pyi b/test-data/unit/fixtures/object_hashable.pyi index 6d7ea11d2767..59c04ca861ed 100644 --- a/test-data/unit/fixtures/object_hashable.pyi +++ b/test-data/unit/fixtures/object_hashable.pyi @@ -1,3 +1,8 @@ +from typing import TypeVar, Mapping + +KT = TypeVar('KT') +VT = TypeVar('VT') + class object: def __hash__(self) -> int: ... @@ -6,3 +11,5 @@ class int: ... class float: ... class str: ... class ellipsis: ... +class dict(Mapping[KT, VT]): + def __iter__(self): pass From 8e3eb36c0e13b95dd7b85ca09afd34e7d9b0d349 Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Fri, 13 Aug 2021 20:11:09 +0200 Subject: [PATCH 41/68] Add dict stub to f_string fixture --- test-data/unit/fixtures/f_string.pyi | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test-data/unit/fixtures/f_string.pyi b/test-data/unit/fixtures/f_string.pyi index 78d39aee85b8..e1436f370ffc 100644 --- a/test-data/unit/fixtures/f_string.pyi +++ b/test-data/unit/fixtures/f_string.pyi @@ -1,9 +1,11 @@ # Builtins stub used for format-string-related test cases. # We need str and list, and str needs join and format methods. -from typing import TypeVar, Generic, Iterable, Iterator, List, overload +from typing import TypeVar, Generic, Iterable, Iterator, List, overload, Mapping T = TypeVar('T') +KT = TypeVar('KT') +VT = TypeVar('VT') class object: def __init__(self): pass @@ -21,6 +23,7 @@ class list(Iterable[T], Generic[T]): def append(self, x: T) -> None: pass class tuple(Generic[T]): pass +class dict(Mapping[KT, VT]): pass class function: pass class int: From 38cd5469cd96234898cddeea9b84baf3e0847a74 Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Fri, 13 Aug 2021 20:13:32 +0200 Subject: [PATCH 42/68] Add dict stub to divmod fixture --- test-data/unit/fixtures/divmod.pyi | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test-data/unit/fixtures/divmod.pyi b/test-data/unit/fixtures/divmod.pyi index cf41c500f49b..f95d2b23275c 100644 --- a/test-data/unit/fixtures/divmod.pyi +++ b/test-data/unit/fixtures/divmod.pyi @@ -1,4 +1,7 @@ -from typing import TypeVar, Tuple, SupportsInt +from typing import TypeVar, Tuple, SupportsInt, Mapping +KT = TypeVar('KT') +VT = TypeVar('VT') + class object: def __init__(self): pass @@ -16,6 +19,7 @@ class function: pass class str: pass class type: pass class ellipsis: pass +class dict(Mapping[KT, VT]): pass _N = TypeVar('_N', int, float) def divmod(_x: _N, _y: _N) -> Tuple[_N, _N]: ... From 0e572523d692d56c07b79ef8e7800557f43b78de Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Fri, 13 Aug 2021 20:32:24 +0200 Subject: [PATCH 43/68] Add dict stub to bool_py2 fixture --- test-data/unit/fixtures/bool_py2.pyi | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test-data/unit/fixtures/bool_py2.pyi b/test-data/unit/fixtures/bool_py2.pyi index b2c935132d57..c91d109ed224 100644 --- a/test-data/unit/fixtures/bool_py2.pyi +++ b/test-data/unit/fixtures/bool_py2.pyi @@ -1,7 +1,9 @@ # builtins stub used in boolean-related test cases. -from typing import Generic, TypeVar +from typing import Generic, TypeVar, Mapping import sys T = TypeVar('T') +KT = TypeVar('KT') +VT = TypeVar('VT') class object: def __init__(self) -> None: pass @@ -14,3 +16,4 @@ class int: pass class str: pass class unicode: pass class ellipsis: pass +class dict(Mapping[KT, VT]): pass From f82b7b3f3b874c760738b8d5aacc5286efd3ea89 Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Fri, 13 Aug 2021 20:36:12 +0200 Subject: [PATCH 44/68] Add dict stub to property_py2 fixture --- test-data/unit/fixtures/property_py2.pyi | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test-data/unit/fixtures/property_py2.pyi b/test-data/unit/fixtures/property_py2.pyi index 3b0ab69cf43f..fa58ddd1c254 100644 --- a/test-data/unit/fixtures/property_py2.pyi +++ b/test-data/unit/fixtures/property_py2.pyi @@ -1,6 +1,8 @@ import typing _T = typing.TypeVar('_T') +_KT = typing.TypeVar('_KT') +_VT = typing.TypeVar('_VT') class object: def __init__(self) -> None: pass @@ -19,3 +21,4 @@ class bool: pass class ellipsis: pass class tuple(typing.Generic[_T]): pass +class dict(typing.Mapping[_KT, _VT]): pass From 727602f4bba9059c34a79c48a3b20d745f102359 Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Fri, 13 Aug 2021 20:36:56 +0200 Subject: [PATCH 45/68] Add dict stubs to tests in check-expressions.test --- test-data/unit/check-expressions.test | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/test-data/unit/check-expressions.test b/test-data/unit/check-expressions.test index ff3a5efde6ad..9864b0447382 100644 --- a/test-data/unit/check-expressions.test +++ b/test-data/unit/check-expressions.test @@ -64,12 +64,17 @@ if str(): class A: pass [file builtins.py] +from typing import TypeVar, Mapping +KT = TypeVar('KT') +VT = TypeVar('VT') class object: def __init__(self): pass class type: pass class function: pass class float: pass class str: pass +class int: pass +class dict(Mapping[KT, VT]): pass [case testComplexLiteral] a = 0.0j @@ -81,12 +86,17 @@ if str(): class A: pass [file builtins.py] +from typing import TypeVar, Mapping +KT = TypeVar('KT') +VT = TypeVar('VT') class object: def __init__(self): pass class type: pass class function: pass class complex: pass class str: pass +class int: pass +class dict(Mapping[KT, VT]): pass [case testBytesLiteral] b, a = None, None # type: (bytes, A) @@ -100,6 +110,9 @@ if str(): a = b'foo' # E: Incompatible types in assignment (expression has type "bytes", variable has type "A") class A: pass [file builtins.py] +from typing import TypeVar, Mapping +KT = TypeVar('KT') +VT = TypeVar('VT') class object: def __init__(self): pass class type: pass @@ -107,6 +120,8 @@ class tuple: pass class function: pass class bytes: pass class str: pass +class int: pass +class dict(Mapping[KT, VT]): pass [case testUnicodeLiteralInPython3] s = None # type: str @@ -2127,6 +2142,9 @@ if str(): class A: pass [file builtins.py] +from typing import TypeVar, Mapping +KT = TypeVar('KT') +VT = TypeVar('VT') class object: def __init__(self): pass class ellipsis: @@ -2135,6 +2153,8 @@ class ellipsis: class type: pass class function: pass class str: pass +class int: pass +class dict(Mapping[KT, VT]): pass [out] From 74f5f56ba5795a2b7a9d6cf7eabb28cf709f19eb Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Fri, 13 Aug 2021 20:37:24 +0200 Subject: [PATCH 46/68] Add dict stubs to tests in check-tuples.test --- test-data/unit/check-tuples.test | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test-data/unit/check-tuples.test b/test-data/unit/check-tuples.test index d5b35784f5e5..d2d936b8362f 100644 --- a/test-data/unit/check-tuples.test +++ b/test-data/unit/check-tuples.test @@ -793,14 +793,17 @@ if int(): b = s in t [file builtins.py] -from typing import TypeVar, Generic +from typing import TypeVar, Generic, Mapping _T = TypeVar('_T') +_KT = TypeVar('_KT') +_VT = TypeVar('_VT') class object: def __init__(self) -> None: pass class tuple(Generic[_T]): def __len__(self) -> int: pass def __str__(self) -> str: pass def __contains__(self, o: object) -> bool: pass +class dict(Mapping[_KT, _VT]): pass class int: pass class str: pass class bool: pass From c124ddd5bfa5eaa574a6701b62fd6c2ba93f5085 Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Fri, 13 Aug 2021 20:52:17 +0200 Subject: [PATCH 47/68] Add return type to __iter__ in dict stub in list fixture --- test-data/unit/fixtures/list.pyi | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test-data/unit/fixtures/list.pyi b/test-data/unit/fixtures/list.pyi index 282a52e8f305..817d397f4783 100644 --- a/test-data/unit/fixtures/list.pyi +++ b/test-data/unit/fixtures/list.pyi @@ -12,7 +12,7 @@ class object: class type: pass class ellipsis: pass class dict(Mapping[KT, VT]): - def __iter__(self): pass + def __iter__(self)-> Iterator[KT]: pass class list(Sequence[T]): @overload From 8d1a13957a61344e727cfbb7be9b2fdbd3b48ed5 Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Fri, 13 Aug 2021 20:53:09 +0200 Subject: [PATCH 48/68] Remove testDictMissingFromStubs Since dict was added to builtin stubs. --- test-data/unit/check-incomplete-fixture.test | 8 -------- 1 file changed, 8 deletions(-) diff --git a/test-data/unit/check-incomplete-fixture.test b/test-data/unit/check-incomplete-fixture.test index f06dad293184..146494df1bd6 100644 --- a/test-data/unit/check-incomplete-fixture.test +++ b/test-data/unit/check-incomplete-fixture.test @@ -12,14 +12,6 @@ import m m.x # E: "object" has no attribute "x" [file m.py] -[case testDictMissingFromStubs] -from typing import Dict -def f(x: Dict[int]) -> None: pass -[out] -main:1: error: Module "typing" has no attribute "Dict" -main:1: note: Maybe your test fixture does not define "builtins.dict"? -main:1: note: Consider adding [builtins fixtures/dict.pyi] to your test description - [case testSetMissingFromStubs] from typing import Set def f(x: Set[int]) -> None: pass From 4881a7f0d8334854987594d26523ab2464abb671 Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Fri, 13 Aug 2021 20:58:21 +0200 Subject: [PATCH 49/68] Add dict stub to complex_tuple fixture --- test-data/unit/fixtures/complex_tuple.pyi | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test-data/unit/fixtures/complex_tuple.pyi b/test-data/unit/fixtures/complex_tuple.pyi index 6be46ac34573..916b58ee1bf6 100644 --- a/test-data/unit/fixtures/complex_tuple.pyi +++ b/test-data/unit/fixtures/complex_tuple.pyi @@ -1,10 +1,13 @@ -from typing import Generic, TypeVar +from typing import Generic, TypeVar, Mapping _T = TypeVar('_T') +_KT = TypeVar('_KT') +_VT = TypeVar('_VT') class object: def __init__(self): pass class tuple(Generic[_T]): pass +class dict(Mapping[_KT, _VT]): pass class type: pass class function: pass From f49c4f072e6ed04f9ccd70ca8551165710c2ff9a Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Fri, 13 Aug 2021 21:10:30 +0200 Subject: [PATCH 50/68] Add dict stub to fine_grained fixture --- test-data/unit/fixtures/fine_grained.pyi | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test-data/unit/fixtures/fine_grained.pyi b/test-data/unit/fixtures/fine_grained.pyi index b2e104ccfceb..547a96c41c86 100644 --- a/test-data/unit/fixtures/fine_grained.pyi +++ b/test-data/unit/fixtures/fine_grained.pyi @@ -4,9 +4,11 @@ # enough to handle them. import types -from typing import TypeVar, Generic +from typing import TypeVar, Generic, Mapping T = TypeVar('T') +KT = TypeVar('KT') +VT = TypeVar('VT') class Any: pass @@ -27,3 +29,4 @@ class tuple(Generic[T]): pass class function: pass class ellipsis: pass class list(Generic[T]): pass +class dict(Mapping[KT, VT]): pass From 7a68cc72d61503958a6911a5336648c24b198215 Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Fri, 13 Aug 2021 21:19:47 +0200 Subject: [PATCH 51/68] Add dict stub to tuple-simple fixture --- test-data/unit/fixtures/tuple-simple.pyi | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/test-data/unit/fixtures/tuple-simple.pyi b/test-data/unit/fixtures/tuple-simple.pyi index b195dfa59729..f56be657a735 100644 --- a/test-data/unit/fixtures/tuple-simple.pyi +++ b/test-data/unit/fixtures/tuple-simple.pyi @@ -3,9 +3,11 @@ # This is a simpler version of tuple.py which is useful # and makes some test cases easier to write/debug. -from typing import Iterable, TypeVar, Generic +from typing import Iterable, TypeVar, Generic, Mapping T = TypeVar('T') +KT = TypeVar('KT') +VT = TypeVar('VT') class object: def __init__(self): pass @@ -13,6 +15,7 @@ class object: class type: pass class tuple(Generic[T]): def __getitem__(self, x: int) -> T: pass +class dict(Mapping[KT, VT]): pass class function: pass # We need int for indexing tuples. From 89ff0d508ec1343dc753f220b850d60bfb6b0bc1 Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Fri, 13 Aug 2021 21:22:02 +0200 Subject: [PATCH 52/68] Add dict stubs to tests in typexport-basic.test --- test-data/unit/typexport-basic.test | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test-data/unit/typexport-basic.test b/test-data/unit/typexport-basic.test index deb43f6d316f..3083b6334cdd 100644 --- a/test-data/unit/typexport-basic.test +++ b/test-data/unit/typexport-basic.test @@ -109,6 +109,9 @@ a = 1 + 2 2.2 - 3 1 / 2 [file builtins.py] +from typing import TypeVar, Mapping +KT = TypeVar('KT') +VT = TypeVar('VT') class object: def __init__(self) -> None: pass class function: pass @@ -120,6 +123,7 @@ class float: def __sub__(self, x: int) -> float: pass class type: pass class str: pass +class dict(Mapping[KT, VT]): pass [out] OpExpr(3) : builtins.int OpExpr(4) : builtins.float @@ -136,6 +140,9 @@ import typing 8 > 3 4 < 6 > 2 [file builtins.py] +from typing import TypeVar, Mapping +KT = TypeVar('KT') +VT = TypeVar('VT') class object: def __init__(self) -> None: pass class int: @@ -146,6 +153,7 @@ class bool: pass class type: pass class function: pass class str: pass +class dict(Mapping[KT, VT]): pass [out] ComparisonExpr(3) : builtins.bool ComparisonExpr(4) : builtins.bool From 8c1c975508d024e5cd940d910b98013a46899d30 Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Fri, 13 Aug 2021 22:43:26 +0200 Subject: [PATCH 53/68] Set value of __annotations__ to AnyType Since the annotations is not necessarily a type. Any expression is a valid annotation according to: https://docs.python.org/3/reference/simple_stmts.html#annotated-assignment-statements --- mypy/semanal.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mypy/semanal.py b/mypy/semanal.py index 18d75e6fbd28..e6571b5c8bee 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -444,7 +444,7 @@ def add_implicit_module_attrs(self, file_node: MypyFile) -> None: continue node = sym.node assert isinstance(node, TypeInfo) - typ = Instance(node, [self.str_type(), self.named_type('__builtins__.type')]) + 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) From ce035ad7edb435076cfb338d387a7e3b9ea3d629 Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Fri, 13 Aug 2021 22:44:26 +0200 Subject: [PATCH 54/68] Fix merge.test --- test-data/unit/merge.test | 46 +++++++++++++++++++-------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/test-data/unit/merge.test b/test-data/unit/merge.test index 836ad87857f8..b8fbfe26fa4a 100644 --- a/test-data/unit/merge.test +++ b/test-data/unit/merge.test @@ -669,17 +669,17 @@ TypeInfo<2>( Mro(target.N<2>, builtins.tuple<3>, typing.Sequence<4>, typing.Iterable<5>, builtins.object<1>) Names( _NT<6> - __annotations__<7> (builtins.object<1>) - __doc__<8> (builtins.str<9>) - __new__<10> - _asdict<11> - _field_defaults<12> (builtins.object<1>) - _field_types<13> (builtins.object<1>) - _fields<14> (Tuple[builtins.str<9>]) - _make<15> - _replace<16> - _source<17> (builtins.str<9>) - x<18> (target.A<0>))) + __annotations__<7> (builtins.dict[builtins.str<8>, Any]<9>) + __doc__<10> (builtins.str<8>) + __new__<11> + _asdict<12> + _field_defaults<13> (builtins.dict[builtins.str<8>, Any]<9>) + _field_types<14> (builtins.dict[builtins.str<8>, Any]<9>) + _fields<15> (Tuple[builtins.str<8>]) + _make<16> + _replace<17> + _source<18> (builtins.str<8>) + x<19> (target.A<0>))) ==> TypeInfo<0>( Name(target.A) @@ -692,18 +692,18 @@ TypeInfo<2>( Mro(target.N<2>, builtins.tuple<3>, typing.Sequence<4>, typing.Iterable<5>, builtins.object<1>) Names( _NT<6> - __annotations__<7> (builtins.object<1>) - __doc__<8> (builtins.str<9>) - __new__<10> - _asdict<11> - _field_defaults<12> (builtins.object<1>) - _field_types<13> (builtins.object<1>) - _fields<14> (Tuple[builtins.str<9>, builtins.str<9>]) - _make<15> - _replace<16> - _source<17> (builtins.str<9>) - x<18> (target.A<0>) - y<19> (target.A<0>))) + __annotations__<7> (builtins.dict[builtins.str<8>, Any]<9>) + __doc__<10> (builtins.str<8>) + __new__<11> + _asdict<12> + _field_defaults<13> (builtins.dict[builtins.str<8>, Any]<9>) + _field_types<14> (builtins.dict[builtins.str<8>, Any]<9>) + _fields<15> (Tuple[builtins.str<8>, builtins.str<8>]) + _make<16> + _replace<17> + _source<18> (builtins.str<8>) + x<19> (target.A<0>) + y<20> (target.A<0>))) [case testUnionType_types] import target From 9498d8b9cc6994adf217da3e4ff1218cbd1275e5 Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Tue, 17 Aug 2021 16:55:33 +0200 Subject: [PATCH 55/68] Fix testDecoratorTypeAfterReprocessing --- test-data/unit/fine-grained.test | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test-data/unit/fine-grained.test b/test-data/unit/fine-grained.test index 4963c459e8ff..4e788d3f5605 100644 --- a/test-data/unit/fine-grained.test +++ b/test-data/unit/fine-grained.test @@ -1804,8 +1804,8 @@ def f() -> Iterator[None]: [typing fixtures/typing-medium.pyi] [builtins fixtures/list.pyi] [triggered] -2: , __main__ -3: , __main__, a +2: , , __main__ +3: , , __main__, a [out] main:2: note: Revealed type is "contextlib.GeneratorContextManager[None]" == From 54afceeff30d589d6fea724d26d1eb640261a3da Mon Sep 17 00:00:00 2001 From: Niklas Gustafson Date: Sun, 26 Sep 2021 21:43:27 +0200 Subject: [PATCH 56/68] Add unittest revealing type of __annotations__ --- test-data/unit/check-basic.test | 3 +++ 1 file changed, 3 insertions(+) diff --git a/test-data/unit/check-basic.test b/test-data/unit/check-basic.test index 184c9e58ca58..f5098c7cb2b4 100644 --- a/test-data/unit/check-basic.test +++ b/test-data/unit/check-basic.test @@ -531,3 +531,6 @@ class A: [file test.py] def foo(s: str) -> None: ... + +[case testRevealAnnotations] +reveal_type(__annotations__) # N: Revealed type is "builtins.dict[builtins.str, Any]" From de1f56f81264bdff04956c373cfe11e7243b5ca7 Mon Sep 17 00:00:00 2001 From: Niklas Gustafsson Date: Sat, 11 Dec 2021 08:17:42 +0100 Subject: [PATCH 57/68] Only define __annotations__ if dict is defined --- mypy/semanal.py | 3 ++- test-data/unit/check-basic.test | 1 + test-data/unit/lib-stub/builtins.pyi | 3 --- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/mypy/semanal.py b/mypy/semanal.py index f3f34511800a..f6feefd33362 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -439,9 +439,10 @@ def add_implicit_module_attrs(self, file_node: MypyFile) -> None: assert isinstance(node, TypeInfo) typ = Instance(node, [self.str_type()]) elif name == '__annotations__': - sym = self.lookup_qualified("__builtins__.dict", Context()) + 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)]) diff --git a/test-data/unit/check-basic.test b/test-data/unit/check-basic.test index 6891830764b0..72ad3ec8b4c3 100644 --- a/test-data/unit/check-basic.test +++ b/test-data/unit/check-basic.test @@ -543,3 +543,4 @@ def foo(s: str) -> None: ... [case testRevealAnnotations] reveal_type(__annotations__) # N: Revealed type is "builtins.dict[builtins.str, Any]" +[builtins fixtures/dict.pyi] \ No newline at end of file diff --git a/test-data/unit/lib-stub/builtins.pyi b/test-data/unit/lib-stub/builtins.pyi index e97d3853d400..17f095577aad 100644 --- a/test-data/unit/lib-stub/builtins.pyi +++ b/test-data/unit/lib-stub/builtins.pyi @@ -25,8 +25,5 @@ from typing import Generic, Sequence, TypeVar, Mapping _T = TypeVar('_T') class list(Generic[_T], Sequence[_T]): pass -_KT = TypeVar('_KT') -_VT = TypeVar('_VT') -class dict(Mapping[_KT, _VT]): pass # Definition of None is implicit From 2b3ab616e16519a61f2c298033e94c6ba16ba0b1 Mon Sep 17 00:00:00 2001 From: Niklas Gustafsson Date: Sat, 11 Dec 2021 08:56:38 +0100 Subject: [PATCH 58/68] Revert adding dict to most fixtures --- test-data/unit/fixtures/__init_subclass__.pyi | 4 ---- test-data/unit/fixtures/__new__.pyi | 6 +----- test-data/unit/fixtures/alias.pyi | 4 ---- test-data/unit/fixtures/attr.pyi | 6 +----- test-data/unit/fixtures/bool.pyi | 8 ++------ test-data/unit/fixtures/bool_py2.pyi | 5 +---- test-data/unit/fixtures/callable.pyi | 5 +---- test-data/unit/fixtures/classmethod.pyi | 3 --- test-data/unit/fixtures/complex.pyi | 6 ------ test-data/unit/fixtures/complex_tuple.pyi | 5 +---- test-data/unit/fixtures/divmod.pyi | 6 +----- test-data/unit/fixtures/exception.pyi | 5 +---- test-data/unit/fixtures/f_string.pyi | 5 +---- test-data/unit/fixtures/fine_grained.pyi | 5 +---- test-data/unit/fixtures/float.pyi | 7 +------ test-data/unit/fixtures/for.pyi | 6 +----- test-data/unit/fixtures/function.pyi | 5 ----- test-data/unit/fixtures/isinstance.pyi | 7 +------ test-data/unit/fixtures/isinstance_python3_10.pyi | 6 +----- test-data/unit/fixtures/module_all.pyi | 5 +---- test-data/unit/fixtures/module_all_python2.pyi | 5 +---- test-data/unit/fixtures/notimplemented.pyi | 6 ++---- test-data/unit/fixtures/object_hashable.pyi | 7 ------- test-data/unit/fixtures/ops.pyi | 6 +----- test-data/unit/fixtures/property_py2.pyi | 3 --- test-data/unit/fixtures/set.pyi | 5 +---- test-data/unit/fixtures/slice.pyi | 5 +---- test-data/unit/fixtures/staticmethod.pyi | 5 ----- test-data/unit/fixtures/tuple-simple.pyi | 5 +---- test-data/unit/fixtures/type.pyi | 5 +---- test-data/unit/lib-stub/__builtin__.pyi | 5 +---- 31 files changed, 25 insertions(+), 141 deletions(-) diff --git a/test-data/unit/fixtures/__init_subclass__.pyi b/test-data/unit/fixtures/__init_subclass__.pyi index 5c08e2cc4996..c5a17f60688e 100644 --- a/test-data/unit/fixtures/__init_subclass__.pyi +++ b/test-data/unit/fixtures/__init_subclass__.pyi @@ -1,7 +1,4 @@ # builtins stub with object.__init_subclass__ -from typing import TypeVar, Mapping -KT = TypeVar('KT') -VT = TypeVar('VT') from typing import Mapping, Iterable # needed for ArgumentInferContext @@ -14,4 +11,3 @@ class int: pass class bool: pass class str: pass class function: pass -class dict(Mapping[KT, VT]): pass diff --git a/test-data/unit/fixtures/__new__.pyi b/test-data/unit/fixtures/__new__.pyi index 41e4c27733b8..bb4788df8fe9 100644 --- a/test-data/unit/fixtures/__new__.pyi +++ b/test-data/unit/fixtures/__new__.pyi @@ -1,9 +1,6 @@ # builtins stub with object.__new__ -from typing import Any, TypeVar, Mapping - -_KT = TypeVar('_KT') -_VT = TypeVar('_VT') +from typing import Any class object: def __init__(self) -> None: pass @@ -19,4 +16,3 @@ class int: pass class bool: pass class str: pass class function: pass -class dict(Mapping[_KT, _VT]): pass diff --git a/test-data/unit/fixtures/alias.pyi b/test-data/unit/fixtures/alias.pyi index 9310ef69fd9d..08b145f4efd1 100644 --- a/test-data/unit/fixtures/alias.pyi +++ b/test-data/unit/fixtures/alias.pyi @@ -1,7 +1,4 @@ # Builtins test fixture with a type alias 'bytes' -from typing import TypeVar, Mapping -KT = TypeVar('KT') -VT = TypeVar('VT') from typing import Mapping, Iterable # needed for `ArgumentInferContext` @@ -13,6 +10,5 @@ class type: class int: pass class str: pass class function: pass -class dict(Mapping[KT, VT]): pass bytes = str diff --git a/test-data/unit/fixtures/attr.pyi b/test-data/unit/fixtures/attr.pyi index 75a332348e16..c209abfef0d9 100644 --- a/test-data/unit/fixtures/attr.pyi +++ b/test-data/unit/fixtures/attr.pyi @@ -1,8 +1,5 @@ # Builtins stub used to support @attr.s tests. -from typing import Union, overload, Mapping, TypeVar - -_KT = TypeVar('_KT') -_VT = TypeVar('_VT') +from typing import Union, overload class object: def __init__(self) -> None: pass @@ -28,5 +25,4 @@ class complex: class str: pass class unicode: pass class ellipsis: pass -class dict(Mapping[_KT, _VT]): pass class tuple: pass diff --git a/test-data/unit/fixtures/bool.pyi b/test-data/unit/fixtures/bool.pyi index ce16ce15a49a..245526d78907 100644 --- a/test-data/unit/fixtures/bool.pyi +++ b/test-data/unit/fixtures/bool.pyi @@ -1,9 +1,6 @@ # builtins stub used in boolean-related test cases. -from typing import Generic, TypeVar, Mapping, Iterator - +from typing import Generic, TypeVar T = TypeVar('T') -KT = TypeVar('KT') -VT = TypeVar('VT') class object: def __init__(self) -> None: pass @@ -20,5 +17,4 @@ class str: pass class unicode: pass class ellipsis: pass class list: pass -class dict(Mapping[KT, VT]): - def __iter__(self) -> Iterator[KT]: pass \ No newline at end of file +class property: pass diff --git a/test-data/unit/fixtures/bool_py2.pyi b/test-data/unit/fixtures/bool_py2.pyi index c91d109ed224..b2c935132d57 100644 --- a/test-data/unit/fixtures/bool_py2.pyi +++ b/test-data/unit/fixtures/bool_py2.pyi @@ -1,9 +1,7 @@ # builtins stub used in boolean-related test cases. -from typing import Generic, TypeVar, Mapping +from typing import Generic, TypeVar import sys T = TypeVar('T') -KT = TypeVar('KT') -VT = TypeVar('VT') class object: def __init__(self) -> None: pass @@ -16,4 +14,3 @@ class int: pass class str: pass class unicode: pass class ellipsis: pass -class dict(Mapping[KT, VT]): pass diff --git a/test-data/unit/fixtures/callable.pyi b/test-data/unit/fixtures/callable.pyi index da1c25641911..4ad72bee93ec 100644 --- a/test-data/unit/fixtures/callable.pyi +++ b/test-data/unit/fixtures/callable.pyi @@ -1,8 +1,6 @@ -from typing import Generic, Tuple, TypeVar, Union, Mapping +from typing import Generic, Tuple, TypeVar, Union T = TypeVar('T') -KT = TypeVar('KT') -VT = TypeVar('VT') class object: def __init__(self) -> None: pass @@ -11,7 +9,6 @@ class type: def __init__(self, x) -> None: pass class tuple(Generic[T]): pass -class dict(Mapping[KT, VT]): pass class classmethod: pass class staticmethod: pass diff --git a/test-data/unit/fixtures/classmethod.pyi b/test-data/unit/fixtures/classmethod.pyi index 078f0bae2413..03ad803890a3 100644 --- a/test-data/unit/fixtures/classmethod.pyi +++ b/test-data/unit/fixtures/classmethod.pyi @@ -1,8 +1,6 @@ import typing _T = typing.TypeVar('_T') -_KT = typing.TypeVar('_KT') -_VT = typing.TypeVar('_VT') class object: def __init__(self) -> None: pass @@ -28,4 +26,3 @@ class bool: pass class ellipsis: pass class tuple(typing.Generic[_T]): pass -class dict(typing.Mapping[_KT, _VT]): pass diff --git a/test-data/unit/fixtures/complex.pyi b/test-data/unit/fixtures/complex.pyi index 5a9fcd0088fd..bcd03a2562e5 100644 --- a/test-data/unit/fixtures/complex.pyi +++ b/test-data/unit/fixtures/complex.pyi @@ -1,11 +1,6 @@ # Builtins stub used for some float/complex test cases. # Please don't add tuple to this file, it is used to test incomplete fixtures. -from typing import TypeVar, Mapping - -_KT = TypeVar('_KT') -_VT = TypeVar('_VT') - class object: def __init__(self): pass @@ -15,4 +10,3 @@ class int: pass class float: pass class complex: pass class str: pass -class dict(Mapping[_KT, _VT]): pass diff --git a/test-data/unit/fixtures/complex_tuple.pyi b/test-data/unit/fixtures/complex_tuple.pyi index 916b58ee1bf6..6be46ac34573 100644 --- a/test-data/unit/fixtures/complex_tuple.pyi +++ b/test-data/unit/fixtures/complex_tuple.pyi @@ -1,13 +1,10 @@ -from typing import Generic, TypeVar, Mapping +from typing import Generic, TypeVar _T = TypeVar('_T') -_KT = TypeVar('_KT') -_VT = TypeVar('_VT') class object: def __init__(self): pass class tuple(Generic[_T]): pass -class dict(Mapping[_KT, _VT]): pass class type: pass class function: pass diff --git a/test-data/unit/fixtures/divmod.pyi b/test-data/unit/fixtures/divmod.pyi index f95d2b23275c..cf41c500f49b 100644 --- a/test-data/unit/fixtures/divmod.pyi +++ b/test-data/unit/fixtures/divmod.pyi @@ -1,7 +1,4 @@ -from typing import TypeVar, Tuple, SupportsInt, Mapping -KT = TypeVar('KT') -VT = TypeVar('VT') - +from typing import TypeVar, Tuple, SupportsInt class object: def __init__(self): pass @@ -19,7 +16,6 @@ class function: pass class str: pass class type: pass class ellipsis: pass -class dict(Mapping[KT, VT]): pass _N = TypeVar('_N', int, float) def divmod(_x: _N, _y: _N) -> Tuple[_N, _N]: ... diff --git a/test-data/unit/fixtures/exception.pyi b/test-data/unit/fixtures/exception.pyi index c9fcbe8ba9d6..bf6d21c8716e 100644 --- a/test-data/unit/fixtures/exception.pyi +++ b/test-data/unit/fixtures/exception.pyi @@ -1,14 +1,11 @@ -from typing import Generic, TypeVar, Mapping +from typing import Generic, TypeVar T = TypeVar('T') -KT = TypeVar('KT') -VT = TypeVar('VT') class object: def __init__(self): pass class type: pass class tuple(Generic[T]): pass -class dict(Mapping[KT, VT]): pass class function: pass class int: pass class str: pass diff --git a/test-data/unit/fixtures/f_string.pyi b/test-data/unit/fixtures/f_string.pyi index e1436f370ffc..78d39aee85b8 100644 --- a/test-data/unit/fixtures/f_string.pyi +++ b/test-data/unit/fixtures/f_string.pyi @@ -1,11 +1,9 @@ # Builtins stub used for format-string-related test cases. # We need str and list, and str needs join and format methods. -from typing import TypeVar, Generic, Iterable, Iterator, List, overload, Mapping +from typing import TypeVar, Generic, Iterable, Iterator, List, overload T = TypeVar('T') -KT = TypeVar('KT') -VT = TypeVar('VT') class object: def __init__(self): pass @@ -23,7 +21,6 @@ class list(Iterable[T], Generic[T]): def append(self, x: T) -> None: pass class tuple(Generic[T]): pass -class dict(Mapping[KT, VT]): pass class function: pass class int: diff --git a/test-data/unit/fixtures/fine_grained.pyi b/test-data/unit/fixtures/fine_grained.pyi index 547a96c41c86..b2e104ccfceb 100644 --- a/test-data/unit/fixtures/fine_grained.pyi +++ b/test-data/unit/fixtures/fine_grained.pyi @@ -4,11 +4,9 @@ # enough to handle them. import types -from typing import TypeVar, Generic, Mapping +from typing import TypeVar, Generic T = TypeVar('T') -KT = TypeVar('KT') -VT = TypeVar('VT') class Any: pass @@ -29,4 +27,3 @@ class tuple(Generic[T]): pass class function: pass class ellipsis: pass class list(Generic[T]): pass -class dict(Mapping[KT, VT]): pass diff --git a/test-data/unit/fixtures/float.pyi b/test-data/unit/fixtures/float.pyi index f1060f3c9c96..880b16a2321b 100644 --- a/test-data/unit/fixtures/float.pyi +++ b/test-data/unit/fixtures/float.pyi @@ -1,8 +1,5 @@ -from typing import Generic, TypeVar, Mapping - +from typing import Generic, TypeVar T = TypeVar('T') -KT = TypeVar('KT') -VT = TypeVar('VT') Any = 0 @@ -19,8 +16,6 @@ class str: class bytes: pass class tuple(Generic[T]): pass -class dict(Mapping[KT, VT]): - def __iter__(self): pass class function: pass class ellipsis: pass diff --git a/test-data/unit/fixtures/for.pyi b/test-data/unit/fixtures/for.pyi index 7b7c853f1b51..31f6de78d486 100644 --- a/test-data/unit/fixtures/for.pyi +++ b/test-data/unit/fixtures/for.pyi @@ -1,11 +1,9 @@ # builtins stub used in for statement test cases -from typing import TypeVar, Generic, Iterable, Iterator, Generator, Mapping +from typing import TypeVar, Generic, Iterable, Iterator, Generator from abc import abstractmethod, ABCMeta t = TypeVar('t') -KT = TypeVar('KT') -VT = TypeVar('VT') class object: def __init__(self) -> None: pass @@ -20,5 +18,3 @@ class str: pass # for convenience class list(Iterable[t], Generic[t]): def __iter__(self) -> Iterator[t]: pass - -class dict(Mapping[KT, VT]): pass diff --git a/test-data/unit/fixtures/function.pyi b/test-data/unit/fixtures/function.pyi index c4265b92923e..c00a7846628a 100644 --- a/test-data/unit/fixtures/function.pyi +++ b/test-data/unit/fixtures/function.pyi @@ -1,7 +1,3 @@ -from typing import TypeVar, Mapping -KT = TypeVar('KT') -VT = TypeVar('VT') - class object: def __init__(self): pass @@ -9,4 +5,3 @@ class type: pass class function: pass class int: pass class str: pass -class dict(Mapping[KT, VT]): pass diff --git a/test-data/unit/fixtures/isinstance.pyi b/test-data/unit/fixtures/isinstance.pyi index 651289d3f006..7f7cf501b5de 100644 --- a/test-data/unit/fixtures/isinstance.pyi +++ b/test-data/unit/fixtures/isinstance.pyi @@ -1,8 +1,6 @@ -from typing import Tuple, TypeVar, Generic, Union, cast, Any, Type, Mapping +from typing import Tuple, TypeVar, Generic, Union, cast, Any, Type T = TypeVar('T') -KT = TypeVar('KT') -VT = TypeVar('VT') class object: def __init__(self) -> None: pass @@ -12,9 +10,6 @@ class type: class tuple(Generic[T]): pass -class dict(Mapping[KT, VT]): - def __iter__(self): pass - class function: pass def isinstance(x: object, t: Union[Type[object], Tuple[Type[object], ...]]) -> bool: pass diff --git a/test-data/unit/fixtures/isinstance_python3_10.pyi b/test-data/unit/fixtures/isinstance_python3_10.pyi index 6e466c289677..abb37ea81c00 100644 --- a/test-data/unit/fixtures/isinstance_python3_10.pyi +++ b/test-data/unit/fixtures/isinstance_python3_10.pyi @@ -1,10 +1,8 @@ # For Python 3.10+ only -from typing import Tuple, TypeVar, Generic, Union, cast, Any, Type, Mapping +from typing import Tuple, TypeVar, Generic, Union, cast, Any, Type import types T = TypeVar('T') -KT = TypeVar('KT') -VT = TypeVar('VT') class object: def __init__(self) -> None: pass @@ -15,8 +13,6 @@ class type(Generic[T]): class tuple(Generic[T]): pass -class dict(Mapping[KT, VT]): pass - class function: pass def isinstance(x: object, t: Union[Type[object], Tuple[Type[object], ...], types.Union]) -> bool: pass diff --git a/test-data/unit/fixtures/module_all.pyi b/test-data/unit/fixtures/module_all.pyi index 4c40efe5c80f..87959fefbff5 100644 --- a/test-data/unit/fixtures/module_all.pyi +++ b/test-data/unit/fixtures/module_all.pyi @@ -1,9 +1,7 @@ -from typing import Generic, Sequence, TypeVar, Mapping +from typing import Generic, Sequence, TypeVar from types import ModuleType _T = TypeVar('_T') -_KT = TypeVar('_KT') -_VT = TypeVar('_VT') class object: def __init__(self) -> None: pass @@ -17,5 +15,4 @@ class list(Generic[_T], Sequence[_T]): def extend(self, x: Sequence[_T]): pass def __add__(self, rhs: Sequence[_T]) -> list[_T]: pass class tuple(Generic[_T]): pass -class dict(Mapping[_KT, _VT]): pass class ellipsis: pass diff --git a/test-data/unit/fixtures/module_all_python2.pyi b/test-data/unit/fixtures/module_all_python2.pyi index c1067f43d58a..989333c5f41a 100644 --- a/test-data/unit/fixtures/module_all_python2.pyi +++ b/test-data/unit/fixtures/module_all_python2.pyi @@ -1,7 +1,5 @@ -from typing import Generic, Sequence, TypeVar, Mapping +from typing import Generic, Sequence, TypeVar _T = TypeVar('_T') -_KT = TypeVar('_KT') -_VT = TypeVar('_VT') class object: def __init__(self) -> None: pass @@ -15,4 +13,3 @@ class list(Generic[_T], Sequence[_T]): def extend(self, x: Sequence[_T]): pass def __add__(self, rhs: Sequence[_T]) -> list[_T]: pass class tuple(Generic[_T]): pass -class dict(Mapping[_KT, _VT]): pass diff --git a/test-data/unit/fixtures/notimplemented.pyi b/test-data/unit/fixtures/notimplemented.pyi index f3024a369384..e619a6c5ad85 100644 --- a/test-data/unit/fixtures/notimplemented.pyi +++ b/test-data/unit/fixtures/notimplemented.pyi @@ -1,7 +1,6 @@ # builtins stub used in NotImplemented related cases. -from typing import Any, cast, TypeVar, Mapping -KT = TypeVar('KT') -VT = TypeVar('VT') +from typing import Any, cast + class object: def __init__(self) -> None: pass @@ -11,5 +10,4 @@ class function: pass class bool: pass class int: pass class str: pass -class dict(Mapping[KT, VT]): pass NotImplemented = cast(Any, None) diff --git a/test-data/unit/fixtures/object_hashable.pyi b/test-data/unit/fixtures/object_hashable.pyi index 59c04ca861ed..6d7ea11d2767 100644 --- a/test-data/unit/fixtures/object_hashable.pyi +++ b/test-data/unit/fixtures/object_hashable.pyi @@ -1,8 +1,3 @@ -from typing import TypeVar, Mapping - -KT = TypeVar('KT') -VT = TypeVar('VT') - class object: def __hash__(self) -> int: ... @@ -11,5 +6,3 @@ class int: ... class float: ... class str: ... class ellipsis: ... -class dict(Mapping[KT, VT]): - def __iter__(self): pass diff --git a/test-data/unit/fixtures/ops.pyi b/test-data/unit/fixtures/ops.pyi index 3bcf3d7eab2f..d5845aba43c6 100644 --- a/test-data/unit/fixtures/ops.pyi +++ b/test-data/unit/fixtures/ops.pyi @@ -1,8 +1,6 @@ -from typing import overload, Any, Generic, Sequence, Tuple, TypeVar, Optional, Mapping +from typing import overload, Any, Generic, Sequence, Tuple, TypeVar, Optional Tco = TypeVar('Tco', covariant=True) -KT = TypeVar('KT') -VT = TypeVar('VT') # This is an extension of transform builtins with additional operations. @@ -24,8 +22,6 @@ class tuple(Sequence[Tco]): def __gt__(self, x: Tuple[Tco, ...]) -> bool: pass def __ge__(self, x: Tuple[Tco, ...]) -> bool: pass -class dict(Mapping[KT, VT]): pass - class function: pass class bool: pass diff --git a/test-data/unit/fixtures/property_py2.pyi b/test-data/unit/fixtures/property_py2.pyi index fa58ddd1c254..3b0ab69cf43f 100644 --- a/test-data/unit/fixtures/property_py2.pyi +++ b/test-data/unit/fixtures/property_py2.pyi @@ -1,8 +1,6 @@ import typing _T = typing.TypeVar('_T') -_KT = typing.TypeVar('_KT') -_VT = typing.TypeVar('_VT') class object: def __init__(self) -> None: pass @@ -21,4 +19,3 @@ class bool: pass class ellipsis: pass class tuple(typing.Generic[_T]): pass -class dict(typing.Mapping[_KT, _VT]): pass diff --git a/test-data/unit/fixtures/set.pyi b/test-data/unit/fixtures/set.pyi index 1569d31c4469..9852bbc9fcc6 100644 --- a/test-data/unit/fixtures/set.pyi +++ b/test-data/unit/fixtures/set.pyi @@ -1,17 +1,14 @@ # Builtins stub used in set-related test cases. -from typing import TypeVar, Generic, Iterator, Iterable, Set, Mapping +from typing import TypeVar, Generic, Iterator, Iterable, Set T = TypeVar('T') -KT = TypeVar('KT') -VT = TypeVar('VT') class object: def __init__(self) -> None: pass class type: pass class tuple(Generic[T]): pass -class dict(Mapping[KT, VT]): pass class function: pass class int: pass diff --git a/test-data/unit/fixtures/slice.pyi b/test-data/unit/fixtures/slice.pyi index f365a5e4684e..947d49ea09fb 100644 --- a/test-data/unit/fixtures/slice.pyi +++ b/test-data/unit/fixtures/slice.pyi @@ -1,15 +1,12 @@ # Builtins stub used in slicing test cases. -from typing import Generic, TypeVar, Mapping +from typing import Generic, TypeVar T = TypeVar('T') -KT = TypeVar('KT') -VT = TypeVar('VT') class object: def __init__(self): pass class type: pass class tuple(Generic[T]): pass -class dict(Mapping[KT, VT]): pass class function: pass class int: pass diff --git a/test-data/unit/fixtures/staticmethod.pyi b/test-data/unit/fixtures/staticmethod.pyi index 58fe8d5a6792..7d5d98634e48 100644 --- a/test-data/unit/fixtures/staticmethod.pyi +++ b/test-data/unit/fixtures/staticmethod.pyi @@ -1,8 +1,5 @@ import typing -_KT = typing.TypeVar('_KT') -_VT = typing.TypeVar('_VT') - class object: def __init__(self) -> None: pass @@ -11,8 +8,6 @@ class type: class function: pass -class dict(typing.Mapping[_KT, _VT]): pass - staticmethod = object() # Dummy definition. property = object() # Dummy definition diff --git a/test-data/unit/fixtures/tuple-simple.pyi b/test-data/unit/fixtures/tuple-simple.pyi index f56be657a735..b195dfa59729 100644 --- a/test-data/unit/fixtures/tuple-simple.pyi +++ b/test-data/unit/fixtures/tuple-simple.pyi @@ -3,11 +3,9 @@ # This is a simpler version of tuple.py which is useful # and makes some test cases easier to write/debug. -from typing import Iterable, TypeVar, Generic, Mapping +from typing import Iterable, TypeVar, Generic T = TypeVar('T') -KT = TypeVar('KT') -VT = TypeVar('VT') class object: def __init__(self): pass @@ -15,7 +13,6 @@ class object: class type: pass class tuple(Generic[T]): def __getitem__(self, x: int) -> T: pass -class dict(Mapping[KT, VT]): pass class function: pass # We need int for indexing tuples. diff --git a/test-data/unit/fixtures/type.pyi b/test-data/unit/fixtures/type.pyi index 81e8e58eb564..755b45ff0bb5 100644 --- a/test-data/unit/fixtures/type.pyi +++ b/test-data/unit/fixtures/type.pyi @@ -1,17 +1,14 @@ # builtins stub used in type-related test cases. -from typing import Generic, TypeVar, List, Union, Mapping +from typing import Generic, TypeVar, List, Union T = TypeVar('T') -KT = TypeVar('KT') -VT = TypeVar('VT') class object: def __init__(self) -> None: pass def __str__(self) -> 'str': pass class list(Generic[T]): pass -class dict(Mapping[KT, VT]): pass class type(Generic[T]): __name__: str diff --git a/test-data/unit/lib-stub/__builtin__.pyi b/test-data/unit/lib-stub/__builtin__.pyi index b6329ddbe719..e7109a179aac 100644 --- a/test-data/unit/lib-stub/__builtin__.pyi +++ b/test-data/unit/lib-stub/__builtin__.pyi @@ -1,7 +1,5 @@ -from typing import Generic, TypeVar, Mapping +from typing import Generic, TypeVar _T = TypeVar('_T') -_KT = TypeVar('_KT') -_VT = TypeVar('_VT') Any = 0 @@ -23,7 +21,6 @@ class str: pass class unicode: pass class tuple(Generic[_T]): pass -class dict(Mapping[_KT, _VT]): pass class function: pass class ellipsis: pass From fdd7a710712d45724ef5d1adfe43c8e1dd667037 Mon Sep 17 00:00:00 2001 From: Niklas Gustafsson Date: Sat, 11 Dec 2021 12:48:55 +0100 Subject: [PATCH 59/68] Bring back testDictMissingFromStubs --- test-data/unit/check-incomplete-fixture.test | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/test-data/unit/check-incomplete-fixture.test b/test-data/unit/check-incomplete-fixture.test index 146494df1bd6..f06dad293184 100644 --- a/test-data/unit/check-incomplete-fixture.test +++ b/test-data/unit/check-incomplete-fixture.test @@ -12,6 +12,14 @@ import m m.x # E: "object" has no attribute "x" [file m.py] +[case testDictMissingFromStubs] +from typing import Dict +def f(x: Dict[int]) -> None: pass +[out] +main:1: error: Module "typing" has no attribute "Dict" +main:1: note: Maybe your test fixture does not define "builtins.dict"? +main:1: note: Consider adding [builtins fixtures/dict.pyi] to your test description + [case testSetMissingFromStubs] from typing import Set def f(x: Set[int]) -> None: pass From ea7cc0801751c8fd893c77d0e97b855be58325ea Mon Sep 17 00:00:00 2001 From: Niklas Gustafsson Date: Sat, 11 Dec 2021 18:30:31 +0100 Subject: [PATCH 60/68] Minor cleanup --- test-data/unit/check-python38.test | 2 -- test-data/unit/fixtures/bool.pyi | 1 - test-data/unit/fixtures/tuple.pyi | 3 +-- test-data/unit/lib-stub/builtins.pyi | 4 +--- 4 files changed, 2 insertions(+), 8 deletions(-) diff --git a/test-data/unit/check-python38.test b/test-data/unit/check-python38.test index 7945bfbca9a8..c7289593e810 100644 --- a/test-data/unit/check-python38.test +++ b/test-data/unit/check-python38.test @@ -78,7 +78,6 @@ def g(x: int): ... f("ERROR"), # E: Argument 1 to "f" has incompatible type "str"; expected "int" ) [builtins fixtures/tuple.pyi] -[builtins fixtures/dict.pyi] [case testIgnoreScopeNestedOverlapping] def f(x: int): ... @@ -89,7 +88,6 @@ def g(x: int): ... ), f("ERROR"), # E: Argument 1 to "f" has incompatible type "str"; expected "int" ) [builtins fixtures/tuple.pyi] -[builtins fixtures/dict.pyi] [case testIgnoreScopeUnused1] # flags: --warn-unused-ignores diff --git a/test-data/unit/fixtures/bool.pyi b/test-data/unit/fixtures/bool.pyi index 245526d78907..ca2564dabafd 100644 --- a/test-data/unit/fixtures/bool.pyi +++ b/test-data/unit/fixtures/bool.pyi @@ -17,4 +17,3 @@ class str: pass class unicode: pass class ellipsis: pass class list: pass -class property: pass diff --git a/test-data/unit/fixtures/tuple.pyi b/test-data/unit/fixtures/tuple.pyi index 6e9729e872ae..d59ae9619ac0 100644 --- a/test-data/unit/fixtures/tuple.pyi +++ b/test-data/unit/fixtures/tuple.pyi @@ -1,7 +1,6 @@ # Builtins stub used in tuple-related test cases. -from typing import Iterable, Iterator, TypeVar, Generic, Sequence, Any, overload, Tuple, Mapping, Type -from typing import Iterable, Iterator, TypeVar, Generic, Sequence, Any, overload, Tuple, Type +from typing import Iterable, Iterator, TypeVar, Generic, Sequence, Any, overload, Tuple, Type, Mapping T = TypeVar("T") KT = TypeVar('KT') diff --git a/test-data/unit/lib-stub/builtins.pyi b/test-data/unit/lib-stub/builtins.pyi index 17f095577aad..8c4f504fb2e7 100644 --- a/test-data/unit/lib-stub/builtins.pyi +++ b/test-data/unit/lib-stub/builtins.pyi @@ -20,10 +20,8 @@ class bytes: pass class function: pass class ellipsis: pass -from typing import Generic, Sequence, TypeVar, Mapping - +from typing import Generic, Sequence, TypeVar _T = TypeVar('_T') class list(Generic[_T], Sequence[_T]): pass - # Definition of None is implicit From b0a59aa7614a6f17ffa0e85b636f25bbe2b1c64a Mon Sep 17 00:00:00 2001 From: Niklas Gustafsson Date: Sat, 11 Dec 2021 19:18:08 +0100 Subject: [PATCH 61/68] Remove dict stub from check-dynamic-typing.test --- test-data/unit/check-dynamic-typing.test | 8 -------- 1 file changed, 8 deletions(-) diff --git a/test-data/unit/check-dynamic-typing.test b/test-data/unit/check-dynamic-typing.test index f264f9424410..8ff82819c104 100644 --- a/test-data/unit/check-dynamic-typing.test +++ b/test-data/unit/check-dynamic-typing.test @@ -138,9 +138,6 @@ class C: def __contains__(self, a: A) -> bool: pass [file builtins.py] -from typing import TypeVar, Generic, Iterable, Mapping -KT = TypeVar('KT') -VT = TypeVar('VT') class object: def __init__(self): pass class bool: pass @@ -148,7 +145,6 @@ class int: pass class type: pass class function: pass class str: pass -class dict(Mapping[KT, VT]): pass [case testBinaryOperationsWithDynamicAsRightOperand] from typing import Any @@ -214,9 +210,6 @@ class A: class C: pass [file builtins.py] -from typing import TypeVar, Mapping -KT = TypeVar('KT') -VT = TypeVar('VT') class object: def __init__(self): pass class bool: pass @@ -224,7 +217,6 @@ class int: pass class type: pass class function: pass class str: pass -class dict(Mapping[KT, VT]): pass [case testDynamicWithUnaryExpressions] from typing import Any From 5d155378f605493dd863a216c322acf91dfe0206 Mon Sep 17 00:00:00 2001 From: Niklas Gustafsson Date: Sat, 11 Dec 2021 19:24:56 +0100 Subject: [PATCH 62/68] Remove dict stub from testMultipleAssignmentWithListAndIndexing --- test-data/unit/check-generics.test | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/test-data/unit/check-generics.test b/test-data/unit/check-generics.test index 2a7f74fd6868..2fa5a0da7e90 100644 --- a/test-data/unit/check-generics.test +++ b/test-data/unit/check-generics.test @@ -1299,14 +1299,11 @@ a[1], a[2] = a class A: pass [file builtins.py] -from typing import TypeVar, Generic, Iterable, Mapping +from typing import TypeVar, Generic, Iterable T = TypeVar('T') -KT = TypeVar('KT') -VT = TypeVar('VT') class object: pass class list(Iterable[T]): def __setitem__(self, x: int, v: T) -> None: pass -class dict(Mapping[KT, VT]): pass class int: pass class type: pass class tuple: pass From 9dce59032b4cf41995ef4c5288f2f10553e48e90 Mon Sep 17 00:00:00 2001 From: Niklas Gustafsson Date: Sat, 11 Dec 2021 19:56:24 +0100 Subject: [PATCH 63/68] Remove dict from typeexport-basic.test --- test-data/unit/typexport-basic.test | 8 -------- 1 file changed, 8 deletions(-) diff --git a/test-data/unit/typexport-basic.test b/test-data/unit/typexport-basic.test index 3083b6334cdd..deb43f6d316f 100644 --- a/test-data/unit/typexport-basic.test +++ b/test-data/unit/typexport-basic.test @@ -109,9 +109,6 @@ a = 1 + 2 2.2 - 3 1 / 2 [file builtins.py] -from typing import TypeVar, Mapping -KT = TypeVar('KT') -VT = TypeVar('VT') class object: def __init__(self) -> None: pass class function: pass @@ -123,7 +120,6 @@ class float: def __sub__(self, x: int) -> float: pass class type: pass class str: pass -class dict(Mapping[KT, VT]): pass [out] OpExpr(3) : builtins.int OpExpr(4) : builtins.float @@ -140,9 +136,6 @@ import typing 8 > 3 4 < 6 > 2 [file builtins.py] -from typing import TypeVar, Mapping -KT = TypeVar('KT') -VT = TypeVar('VT') class object: def __init__(self) -> None: pass class int: @@ -153,7 +146,6 @@ class bool: pass class type: pass class function: pass class str: pass -class dict(Mapping[KT, VT]): pass [out] ComparisonExpr(3) : builtins.bool ComparisonExpr(4) : builtins.bool From ab182546a393064c5bf47ed3146cfd1ba14a5777 Mon Sep 17 00:00:00 2001 From: Niklas Gustafsson Date: Sun, 12 Dec 2021 10:59:10 +0100 Subject: [PATCH 64/68] Remove dict stub from testTupleMethods --- test-data/unit/check-tuples.test | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/test-data/unit/check-tuples.test b/test-data/unit/check-tuples.test index d2d936b8362f..d5b35784f5e5 100644 --- a/test-data/unit/check-tuples.test +++ b/test-data/unit/check-tuples.test @@ -793,17 +793,14 @@ if int(): b = s in t [file builtins.py] -from typing import TypeVar, Generic, Mapping +from typing import TypeVar, Generic _T = TypeVar('_T') -_KT = TypeVar('_KT') -_VT = TypeVar('_VT') class object: def __init__(self) -> None: pass class tuple(Generic[_T]): def __len__(self) -> int: pass def __str__(self) -> str: pass def __contains__(self, o: object) -> bool: pass -class dict(Mapping[_KT, _VT]): pass class int: pass class str: pass class bool: pass From e75ab3ba57ffd0271c755a6b60979423a3cfc503 Mon Sep 17 00:00:00 2001 From: Niklas Gustafsson Date: Sun, 12 Dec 2021 11:13:41 +0100 Subject: [PATCH 65/68] Remove dict stub from testDecoratorTypeAfterReprocessing and list.pyi --- test-data/unit/fine-grained.test | 4 ++-- test-data/unit/fixtures/list.pyi | 6 +----- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/test-data/unit/fine-grained.test b/test-data/unit/fine-grained.test index b17971eccc18..8d41595fa04a 100644 --- a/test-data/unit/fine-grained.test +++ b/test-data/unit/fine-grained.test @@ -1804,8 +1804,8 @@ def f() -> Iterator[None]: [typing fixtures/typing-medium.pyi] [builtins fixtures/list.pyi] [triggered] -2: , , __main__ -3: , , __main__, a +2: , __main__ +3: , __main__, a [out] main:2: note: Revealed type is "contextlib.GeneratorContextManager[None]" == diff --git a/test-data/unit/fixtures/list.pyi b/test-data/unit/fixtures/list.pyi index 83b5f2c8cba4..31dc333b3d4f 100644 --- a/test-data/unit/fixtures/list.pyi +++ b/test-data/unit/fixtures/list.pyi @@ -1,18 +1,14 @@ # Builtins stub used in list-related test cases. -from typing import TypeVar, Generic, Iterable, Iterator, Sequence, overload, Mapping +from typing import TypeVar, Generic, Iterable, Iterator, Sequence, overload T = TypeVar('T') -KT = TypeVar('KT') -VT = TypeVar('VT') class object: def __init__(self) -> None: pass class type: pass class ellipsis: pass -class dict(Mapping[KT, VT]): - def __iter__(self)-> Iterator[KT]: pass class list(Sequence[T]): @overload From fc48ca5831c7f425f216d0674ae92ebbccf8ce4c Mon Sep 17 00:00:00 2001 From: Niklas Gustafsson Date: Sun, 12 Dec 2021 11:26:02 +0100 Subject: [PATCH 66/68] Remove dict stub from tuple.pyi and merge.test --- test-data/unit/fixtures/tuple.pyi | 8 +----- test-data/unit/merge.test | 46 +++++++++++++++---------------- 2 files changed, 24 insertions(+), 30 deletions(-) diff --git a/test-data/unit/fixtures/tuple.pyi b/test-data/unit/fixtures/tuple.pyi index d59ae9619ac0..5f7fb01f4b07 100644 --- a/test-data/unit/fixtures/tuple.pyi +++ b/test-data/unit/fixtures/tuple.pyi @@ -1,10 +1,8 @@ # Builtins stub used in tuple-related test cases. -from typing import Iterable, Iterator, TypeVar, Generic, Sequence, Any, overload, Tuple, Type, Mapping +from typing import Iterable, Iterator, TypeVar, Generic, Sequence, Any, overload, Tuple, Type T = TypeVar("T") -KT = TypeVar('KT') -VT = TypeVar('VT') Tco = TypeVar('Tco', covariant=True) class object: @@ -43,10 +41,6 @@ class list(Sequence[T], Generic[T]): def __contains__(self, item: object) -> bool: ... def __iter__(self) -> Iterator[T]: ... -class dict(Mapping[KT, VT]): - def __iter__(self) -> Iterator[KT]: pass - - def isinstance(x: object, t: type) -> bool: pass def sum(iterable: Iterable[T], start: T = None) -> T: pass diff --git a/test-data/unit/merge.test b/test-data/unit/merge.test index b8fbfe26fa4a..836ad87857f8 100644 --- a/test-data/unit/merge.test +++ b/test-data/unit/merge.test @@ -669,17 +669,17 @@ TypeInfo<2>( Mro(target.N<2>, builtins.tuple<3>, typing.Sequence<4>, typing.Iterable<5>, builtins.object<1>) Names( _NT<6> - __annotations__<7> (builtins.dict[builtins.str<8>, Any]<9>) - __doc__<10> (builtins.str<8>) - __new__<11> - _asdict<12> - _field_defaults<13> (builtins.dict[builtins.str<8>, Any]<9>) - _field_types<14> (builtins.dict[builtins.str<8>, Any]<9>) - _fields<15> (Tuple[builtins.str<8>]) - _make<16> - _replace<17> - _source<18> (builtins.str<8>) - x<19> (target.A<0>))) + __annotations__<7> (builtins.object<1>) + __doc__<8> (builtins.str<9>) + __new__<10> + _asdict<11> + _field_defaults<12> (builtins.object<1>) + _field_types<13> (builtins.object<1>) + _fields<14> (Tuple[builtins.str<9>]) + _make<15> + _replace<16> + _source<17> (builtins.str<9>) + x<18> (target.A<0>))) ==> TypeInfo<0>( Name(target.A) @@ -692,18 +692,18 @@ TypeInfo<2>( Mro(target.N<2>, builtins.tuple<3>, typing.Sequence<4>, typing.Iterable<5>, builtins.object<1>) Names( _NT<6> - __annotations__<7> (builtins.dict[builtins.str<8>, Any]<9>) - __doc__<10> (builtins.str<8>) - __new__<11> - _asdict<12> - _field_defaults<13> (builtins.dict[builtins.str<8>, Any]<9>) - _field_types<14> (builtins.dict[builtins.str<8>, Any]<9>) - _fields<15> (Tuple[builtins.str<8>, builtins.str<8>]) - _make<16> - _replace<17> - _source<18> (builtins.str<8>) - x<19> (target.A<0>) - y<20> (target.A<0>))) + __annotations__<7> (builtins.object<1>) + __doc__<8> (builtins.str<9>) + __new__<10> + _asdict<11> + _field_defaults<12> (builtins.object<1>) + _field_types<13> (builtins.object<1>) + _fields<14> (Tuple[builtins.str<9>, builtins.str<9>]) + _make<15> + _replace<16> + _source<17> (builtins.str<9>) + x<18> (target.A<0>) + y<19> (target.A<0>))) [case testUnionType_types] import target From b7bf39f2aab039ebd4f78a7bb79666b71bccbdac Mon Sep 17 00:00:00 2001 From: Jingchen Ye <11172084+97littleleaf11@users.noreply.github.com> Date: Mon, 14 Mar 2022 01:00:43 +0800 Subject: [PATCH 67/68] Merge test cases --- mypy/nodes.py | 2 +- mypy/semanal.py | 1 - test-data/unit/check-basic.test | 32 ++++++-------------------------- 3 files changed, 7 insertions(+), 28 deletions(-) diff --git a/mypy/nodes.py b/mypy/nodes.py index 0e29853ad8f5..1a750a6a7f91 100644 --- a/mypy/nodes.py +++ b/mypy/nodes.py @@ -104,7 +104,7 @@ def get_column(self) -> int: '__path__': None, # depends on if the module is a package '__file__': '__builtins__.str', '__package__': '__builtins__.str', - '__annotations__': '__builtins__.dict', + '__annotations__': None, # dict[str, Any] bounded in add_implicit_module_attrs() } diff --git a/mypy/semanal.py b/mypy/semanal.py index f6feefd33362..6e599dd804de 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -442,7 +442,6 @@ def add_implicit_module_attrs(self, file_node: MypyFile) -> None: 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)]) diff --git a/test-data/unit/check-basic.test b/test-data/unit/check-basic.test index 72ad3ec8b4c3..238aab3944ff 100644 --- a/test-data/unit/check-basic.test +++ b/test-data/unit/check-basic.test @@ -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 -[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 +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 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 -- --------------------- @@ -540,7 +524,3 @@ class A: [file test.py] def foo(s: str) -> None: ... - -[case testRevealAnnotations] -reveal_type(__annotations__) # N: Revealed type is "builtins.dict[builtins.str, Any]" -[builtins fixtures/dict.pyi] \ No newline at end of file From 18ac989d1744c54b3fb7f238450dde2644b86cca Mon Sep 17 00:00:00 2001 From: Jingchen Ye <11172084+97littleleaf11@users.noreply.github.com> Date: Mon, 14 Mar 2022 11:18:16 +0800 Subject: [PATCH 68/68] Fix stubtest --- mypy/stubtest.py | 1 + 1 file changed, 1 insertion(+) diff --git a/mypy/stubtest.py b/mypy/stubtest.py index b568017cfa5f..ccaf28022d4e 100644 --- a/mypy/stubtest.py +++ b/mypy/stubtest.py @@ -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