From 03cac61bc2a33737f07fe7325ba46ea6d9bbdbfa Mon Sep 17 00:00:00 2001 From: Ilya Konstantinov Date: Sun, 29 Jan 2023 00:00:40 -0500 Subject: [PATCH 1/4] Fail-fast on missing builtins --- mypy/semanal.py | 26 +++-- mypy/semanal_namedtuple.py | 8 +- mypy/test/data.py | 5 +- test-data/unit/check-dynamic-typing.test | 2 + test-data/unit/check-generics.test | 1 + test-data/unit/check-incomplete-fixture.test | 4 +- test-data/unit/check-tuples.test | 1 + test-data/unit/cmdline.test | 2 + test-data/unit/fine-grained.test | 4 +- test-data/unit/fixtures/__init_subclass__.pyi | 1 + test-data/unit/fixtures/__new__.pyi | 1 + test-data/unit/fixtures/alias.pyi | 2 + test-data/unit/fixtures/any.pyi | 2 + test-data/unit/fixtures/attr.pyi | 2 + test-data/unit/fixtures/bool.pyi | 1 + test-data/unit/fixtures/callable.pyi | 1 + test-data/unit/fixtures/classmethod.pyi | 3 + test-data/unit/fixtures/complex.pyi | 1 + test-data/unit/fixtures/complex_tuple.pyi | 1 + test-data/unit/fixtures/divmod.pyi | 2 + test-data/unit/fixtures/exception.pyi | 2 + test-data/unit/fixtures/f_string.pyi | 2 + test-data/unit/fixtures/fine_grained.pyi | 1 + test-data/unit/fixtures/float.pyi | 2 + test-data/unit/fixtures/for.pyi | 1 + test-data/unit/fixtures/function.pyi | 1 + test-data/unit/fixtures/isinstance.pyi | 2 + .../unit/fixtures/isinstance_python3_10.pyi | 2 + test-data/unit/fixtures/list.pyi | 2 + test-data/unit/fixtures/module_all.pyi | 1 + test-data/unit/fixtures/notimplemented.pyi | 1 + test-data/unit/fixtures/object_hashable.pyi | 1 + test-data/unit/fixtures/ops.pyi | 2 + test-data/unit/fixtures/property.pyi | 1 + test-data/unit/fixtures/set.pyi | 2 + test-data/unit/fixtures/slice.pyi | 1 + test-data/unit/fixtures/staticmethod.pyi | 1 + test-data/unit/fixtures/transform.pyi | 2 + test-data/unit/fixtures/tuple-simple.pyi | 1 + test-data/unit/fixtures/tuple.pyi | 2 + test-data/unit/fixtures/union.pyi | 1 + test-data/unit/lib-stub/__builtin__.pyi | 29 ------ test-data/unit/lib-stub/builtins.pyi | 9 +- test-data/unit/merge.test | 96 +++++++++---------- test-data/unit/typexport-basic.test | 4 + 45 files changed, 132 insertions(+), 107 deletions(-) delete mode 100644 test-data/unit/lib-stub/__builtin__.pyi diff --git a/mypy/semanal.py b/mypy/semanal.py index 15566c9396c6..ca2f9bffc5e7 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -625,23 +625,21 @@ def add_implicit_module_attrs(self, file_node: MypyFile) -> None: continue # Need to construct the type ourselves, to avoid issues with __builtins__.list # not being subscriptable or typing.List not getting bound - sym = self.lookup_qualified("__builtins__.list", Context()) - if not sym: - continue - node = sym.node - if not isinstance(node, TypeInfo): - self.defer(node) + typ = self.named_type_or_none("builtins.list", [str_type]) + if typ is None: + assert not self.final_iteration, "Cannot find builtins.list to add __path__" + self.defer() return - typ = Instance(node, [str_type]) elif name == "__annotations__": - sym = self.lookup_qualified("__builtins__.dict", Context(), suppress_errors=True) - if not sym: - continue - node = sym.node - if not isinstance(node, TypeInfo): - self.defer(node) + typ = self.named_type_or_none( + "builtins.dict", [str_type, AnyType(TypeOfAny.special_form)] + ) + if typ is None: + assert ( + not self.final_iteration + ), "Cannot find builtins.dict to add __annotations__" + self.defer() return - typ = Instance(node, [str_type, AnyType(TypeOfAny.special_form)]) else: assert t is not None, f"type should be specified for {name}" typ = UnboundType(t) diff --git a/mypy/semanal_namedtuple.py b/mypy/semanal_namedtuple.py index 226c2e50326b..1194557836b1 100644 --- a/mypy/semanal_namedtuple.py +++ b/mypy/semanal_namedtuple.py @@ -481,13 +481,9 @@ def build_namedtuple_typeinfo( strtype = self.api.named_type("builtins.str") implicit_any = AnyType(TypeOfAny.special_form) basetuple_type = self.api.named_type("builtins.tuple", [implicit_any]) - dictype = self.api.named_type_or_none( - "builtins.dict", [strtype, implicit_any] - ) or self.api.named_type("builtins.object") + dictype = self.api.named_type("builtins.dict", [strtype, implicit_any]) # Actual signature should return OrderedDict[str, Union[types]] - ordereddictype = self.api.named_type_or_none( - "builtins.dict", [strtype, implicit_any] - ) or self.api.named_type("builtins.object") + ordereddictype = self.api.named_type("builtins.dict", [strtype, implicit_any]) fallback = self.api.named_type("builtins.tuple", [implicit_any]) # Note: actual signature should accept an invariant version of Iterable[UnionType[types]]. # but it can't be expressed. 'new' and 'len' should be callable types. diff --git a/mypy/test/data.py b/mypy/test/data.py index f4cb39818b4e..c6f671b2d401 100644 --- a/mypy/test/data.py +++ b/mypy/test/data.py @@ -81,13 +81,12 @@ def parse_test_case(case: DataDrivenTestCase) -> None: output_files.append((file_entry[0], re.compile(file_entry[1].rstrip(), re.S))) else: output_files.append(file_entry) - elif item.id in ("builtins", "builtins_py2"): + elif item.id == "builtins": # Use an alternative stub file for the builtins module. assert item.arg is not None mpath = join(os.path.dirname(case.file), item.arg) - fnam = "builtins.pyi" if item.id == "builtins" else "__builtin__.pyi" with open(mpath, encoding="utf8") as f: - files.append((join(base_path, fnam), f.read())) + files.append((join(base_path, "builtins.pyi"), f.read())) elif item.id == "typing": # Use an alternative stub file for the typing module. assert item.arg is not None diff --git a/test-data/unit/check-dynamic-typing.test b/test-data/unit/check-dynamic-typing.test index 7e62c0d0b0e8..dd4cc1579639 100644 --- a/test-data/unit/check-dynamic-typing.test +++ b/test-data/unit/check-dynamic-typing.test @@ -147,6 +147,7 @@ class int: pass class type: pass class function: pass class str: pass +class dict: pass [case testBinaryOperationsWithDynamicAsRightOperand] from typing import Any @@ -219,6 +220,7 @@ class int: pass class type: pass class function: pass class str: pass +class dict: pass [case testDynamicWithUnaryExpressions] from typing import Any diff --git a/test-data/unit/check-generics.test b/test-data/unit/check-generics.test index 1be3145b3b10..a62028ca94ea 100644 --- a/test-data/unit/check-generics.test +++ b/test-data/unit/check-generics.test @@ -1331,6 +1331,7 @@ class type: pass class tuple: pass class function: pass class str: pass +class dict: pass [case testMultipleAssignmentWithIterable] from typing import Iterable, TypeVar diff --git a/test-data/unit/check-incomplete-fixture.test b/test-data/unit/check-incomplete-fixture.test index f06dad293184..36eab08885a6 100644 --- a/test-data/unit/check-incomplete-fixture.test +++ b/test-data/unit/check-incomplete-fixture.test @@ -16,9 +16,7 @@ m.x # E: "object" has no attribute "x" 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 +main:2: error: "dict" expects no type arguments, but 1 given [case testSetMissingFromStubs] from typing import Set diff --git a/test-data/unit/check-tuples.test b/test-data/unit/check-tuples.test index 535a8ae5007e..266bfbf97888 100644 --- a/test-data/unit/check-tuples.test +++ b/test-data/unit/check-tuples.test @@ -774,6 +774,7 @@ class str: pass class bool: pass class type: pass class function: pass +class dict: pass -- For loop over tuple diff --git a/test-data/unit/cmdline.test b/test-data/unit/cmdline.test index 9eba9ea1e906..c2e98cdb74f9 100644 --- a/test-data/unit/cmdline.test +++ b/test-data/unit/cmdline.test @@ -1516,6 +1516,8 @@ a.py:2: note: By default the bodies of untyped functions are not checked, consid class object: pass class str(object): pass class int(object): pass +class list: pass +class dict: pass [file dir/stdlib/sys.pyi] [file dir/stdlib/types.pyi] [file dir/stdlib/typing.pyi] diff --git a/test-data/unit/fine-grained.test b/test-data/unit/fine-grained.test index d47c21283c91..9f22dc9ab7ac 100644 --- a/test-data/unit/fine-grained.test +++ b/test-data/unit/fine-grained.test @@ -1809,8 +1809,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/__init_subclass__.pyi b/test-data/unit/fixtures/__init_subclass__.pyi index c5a17f60688e..b4618c28249e 100644 --- a/test-data/unit/fixtures/__init_subclass__.pyi +++ b/test-data/unit/fixtures/__init_subclass__.pyi @@ -11,3 +11,4 @@ class int: pass class bool: pass class str: pass class function: pass +class dict: pass diff --git a/test-data/unit/fixtures/__new__.pyi b/test-data/unit/fixtures/__new__.pyi index bb4788df8fe9..401de6fb9cd1 100644 --- a/test-data/unit/fixtures/__new__.pyi +++ b/test-data/unit/fixtures/__new__.pyi @@ -16,3 +16,4 @@ class int: pass class bool: pass class str: pass class function: pass +class dict: pass diff --git a/test-data/unit/fixtures/alias.pyi b/test-data/unit/fixtures/alias.pyi index 08b145f4efd1..2ec7703f00c4 100644 --- a/test-data/unit/fixtures/alias.pyi +++ b/test-data/unit/fixtures/alias.pyi @@ -12,3 +12,5 @@ class str: pass class function: pass bytes = str + +class dict: pass diff --git a/test-data/unit/fixtures/any.pyi b/test-data/unit/fixtures/any.pyi index d6d90b7b3e98..b1f8d83bf524 100644 --- a/test-data/unit/fixtures/any.pyi +++ b/test-data/unit/fixtures/any.pyi @@ -6,3 +6,5 @@ class int: pass class str: pass def any(i: Iterable[T]) -> bool: pass + +class dict: pass diff --git a/test-data/unit/fixtures/attr.pyi b/test-data/unit/fixtures/attr.pyi index 3ac535c21108..3bd4f0ec7cbe 100644 --- a/test-data/unit/fixtures/attr.pyi +++ b/test-data/unit/fixtures/attr.pyi @@ -25,3 +25,5 @@ class complex: class str: pass class ellipsis: pass class tuple: pass +class list: pass +class dict: pass diff --git a/test-data/unit/fixtures/bool.pyi b/test-data/unit/fixtures/bool.pyi index 0f6e1a174c7b..bc58a22b952b 100644 --- a/test-data/unit/fixtures/bool.pyi +++ b/test-data/unit/fixtures/bool.pyi @@ -17,3 +17,4 @@ class str: pass class ellipsis: pass class list(Generic[T]): pass class property: pass +class dict: pass diff --git a/test-data/unit/fixtures/callable.pyi b/test-data/unit/fixtures/callable.pyi index 4ad72bee93ec..44abf0691ceb 100644 --- a/test-data/unit/fixtures/callable.pyi +++ b/test-data/unit/fixtures/callable.pyi @@ -28,3 +28,4 @@ class str: def __eq__(self, other: 'str') -> bool: pass class ellipsis: pass class list: ... +class dict: pass diff --git a/test-data/unit/fixtures/classmethod.pyi b/test-data/unit/fixtures/classmethod.pyi index 03ad803890a3..97e018b1dc1c 100644 --- a/test-data/unit/fixtures/classmethod.pyi +++ b/test-data/unit/fixtures/classmethod.pyi @@ -26,3 +26,6 @@ class bool: pass class ellipsis: pass class tuple(typing.Generic[_T]): pass + +class list: pass +class dict: pass diff --git a/test-data/unit/fixtures/complex.pyi b/test-data/unit/fixtures/complex.pyi index bcd03a2562e5..880ec3dd4d9d 100644 --- a/test-data/unit/fixtures/complex.pyi +++ b/test-data/unit/fixtures/complex.pyi @@ -10,3 +10,4 @@ class int: pass class float: pass class complex: pass class str: pass +class dict: pass diff --git a/test-data/unit/fixtures/complex_tuple.pyi b/test-data/unit/fixtures/complex_tuple.pyi index 6be46ac34573..81f1d33d1207 100644 --- a/test-data/unit/fixtures/complex_tuple.pyi +++ b/test-data/unit/fixtures/complex_tuple.pyi @@ -13,3 +13,4 @@ class float: pass class complex: pass class str: pass class ellipsis: pass +class dict: pass diff --git a/test-data/unit/fixtures/divmod.pyi b/test-data/unit/fixtures/divmod.pyi index cf41c500f49b..4d81d8fb47a2 100644 --- a/test-data/unit/fixtures/divmod.pyi +++ b/test-data/unit/fixtures/divmod.pyi @@ -19,3 +19,5 @@ class ellipsis: pass _N = TypeVar('_N', int, float) def divmod(_x: _N, _y: _N) -> Tuple[_N, _N]: ... + +class dict: pass diff --git a/test-data/unit/fixtures/exception.pyi b/test-data/unit/fixtures/exception.pyi index 70e3b19c4149..08496e4e5934 100644 --- a/test-data/unit/fixtures/exception.pyi +++ b/test-data/unit/fixtures/exception.pyi @@ -8,6 +8,8 @@ class object: class type: pass class tuple(Generic[T]): def __ge__(self, other: object) -> bool: ... +class list: pass +class dict: 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 78d39aee85b8..328c666b7ece 100644 --- a/test-data/unit/fixtures/f_string.pyi +++ b/test-data/unit/fixtures/f_string.pyi @@ -34,3 +34,5 @@ class str: def format(self, *args) -> str: pass def join(self, l: List[str]) -> str: pass + +class dict: pass diff --git a/test-data/unit/fixtures/fine_grained.pyi b/test-data/unit/fixtures/fine_grained.pyi index b2e104ccfceb..e454a27a5ebd 100644 --- a/test-data/unit/fixtures/fine_grained.pyi +++ b/test-data/unit/fixtures/fine_grained.pyi @@ -27,3 +27,4 @@ class tuple(Generic[T]): pass class function: pass class ellipsis: pass class list(Generic[T]): pass +class dict: pass diff --git a/test-data/unit/fixtures/float.pyi b/test-data/unit/fixtures/float.pyi index 880b16a2321b..5db4525849c0 100644 --- a/test-data/unit/fixtures/float.pyi +++ b/test-data/unit/fixtures/float.pyi @@ -34,3 +34,5 @@ class float: def __int__(self) -> int: ... def __mul__(self, x: float) -> float: ... def __rmul__(self, x: float) -> float: ... + +class dict: pass diff --git a/test-data/unit/fixtures/for.pyi b/test-data/unit/fixtures/for.pyi index 31f6de78d486..694f83e940b2 100644 --- a/test-data/unit/fixtures/for.pyi +++ b/test-data/unit/fixtures/for.pyi @@ -18,3 +18,4 @@ class str: pass # for convenience class list(Iterable[t], Generic[t]): def __iter__(self) -> Iterator[t]: pass +class dict: pass diff --git a/test-data/unit/fixtures/function.pyi b/test-data/unit/fixtures/function.pyi index c00a7846628a..697d0d919d98 100644 --- a/test-data/unit/fixtures/function.pyi +++ b/test-data/unit/fixtures/function.pyi @@ -5,3 +5,4 @@ class type: pass class function: pass class int: pass class str: pass +class dict: pass diff --git a/test-data/unit/fixtures/isinstance.pyi b/test-data/unit/fixtures/isinstance.pyi index aa8bfce7fbe0..c1125c24b941 100644 --- a/test-data/unit/fixtures/isinstance.pyi +++ b/test-data/unit/fixtures/isinstance.pyi @@ -25,3 +25,5 @@ class str: class ellipsis: pass NotImplemented = cast(Any, None) + +class dict: pass diff --git a/test-data/unit/fixtures/isinstance_python3_10.pyi b/test-data/unit/fixtures/isinstance_python3_10.pyi index abb37ea81c00..7c919a216bfb 100644 --- a/test-data/unit/fixtures/isinstance_python3_10.pyi +++ b/test-data/unit/fixtures/isinstance_python3_10.pyi @@ -27,3 +27,5 @@ class str: class ellipsis: pass NotImplemented = cast(Any, None) + +class dict: pass diff --git a/test-data/unit/fixtures/list.pyi b/test-data/unit/fixtures/list.pyi index 31dc333b3d4f..90fbabe8bc92 100644 --- a/test-data/unit/fixtures/list.pyi +++ b/test-data/unit/fixtures/list.pyi @@ -36,3 +36,5 @@ class str: class bool(int): pass property = object() # Dummy definition. + +class dict: pass diff --git a/test-data/unit/fixtures/module_all.pyi b/test-data/unit/fixtures/module_all.pyi index 87959fefbff5..b14152c7e98f 100644 --- a/test-data/unit/fixtures/module_all.pyi +++ b/test-data/unit/fixtures/module_all.pyi @@ -16,3 +16,4 @@ class list(Generic[_T], Sequence[_T]): def __add__(self, rhs: Sequence[_T]) -> list[_T]: pass class tuple(Generic[_T]): pass class ellipsis: pass +class dict: pass diff --git a/test-data/unit/fixtures/notimplemented.pyi b/test-data/unit/fixtures/notimplemented.pyi index e619a6c5ad85..2ca376ea0760 100644 --- a/test-data/unit/fixtures/notimplemented.pyi +++ b/test-data/unit/fixtures/notimplemented.pyi @@ -11,3 +11,4 @@ class bool: pass class int: pass class str: pass NotImplemented = cast(Any, None) +class dict: pass diff --git a/test-data/unit/fixtures/object_hashable.pyi b/test-data/unit/fixtures/object_hashable.pyi index 592cba808cbf..49b17991f01c 100644 --- a/test-data/unit/fixtures/object_hashable.pyi +++ b/test-data/unit/fixtures/object_hashable.pyi @@ -7,3 +7,4 @@ class float: ... class str: ... class ellipsis: ... class tuple: ... +class dict: pass diff --git a/test-data/unit/fixtures/ops.pyi b/test-data/unit/fixtures/ops.pyi index 2b29414448cf..9cc4d22eb0a7 100644 --- a/test-data/unit/fixtures/ops.pyi +++ b/test-data/unit/fixtures/ops.pyi @@ -72,3 +72,5 @@ def __print(a1: object = None, a2: object = None, a3: object = None, a4: object = None) -> None: pass class ellipsis: pass + +class dict: pass diff --git a/test-data/unit/fixtures/property.pyi b/test-data/unit/fixtures/property.pyi index 9dca0d50a3be..2397c05c78d5 100644 --- a/test-data/unit/fixtures/property.pyi +++ b/test-data/unit/fixtures/property.pyi @@ -13,6 +13,7 @@ class function: pass property = object() # Dummy definition class classmethod: pass +class list: pass class dict: pass class int: pass class str: pass diff --git a/test-data/unit/fixtures/set.pyi b/test-data/unit/fixtures/set.pyi index d397d4f54af2..71d3bd2eee18 100644 --- a/test-data/unit/fixtures/set.pyi +++ b/test-data/unit/fixtures/set.pyi @@ -25,3 +25,5 @@ class set(Iterable[T], Generic[T]): def add(self, x: T) -> None: pass def discard(self, x: T) -> None: pass def update(self, x: Set[T]) -> None: pass + +class dict: pass diff --git a/test-data/unit/fixtures/slice.pyi b/test-data/unit/fixtures/slice.pyi index 947d49ea09fb..b5a4549da068 100644 --- a/test-data/unit/fixtures/slice.pyi +++ b/test-data/unit/fixtures/slice.pyi @@ -14,3 +14,4 @@ class str: pass class slice: pass class ellipsis: pass +class dict: pass diff --git a/test-data/unit/fixtures/staticmethod.pyi b/test-data/unit/fixtures/staticmethod.pyi index 08fbda8ccf8f..8a87121b2a71 100644 --- a/test-data/unit/fixtures/staticmethod.pyi +++ b/test-data/unit/fixtures/staticmethod.pyi @@ -18,3 +18,4 @@ class int: class str: pass class bytes: pass class ellipsis: pass +class dict: pass diff --git a/test-data/unit/fixtures/transform.pyi b/test-data/unit/fixtures/transform.pyi index afdc2bf5b59a..7dbb8fa90dbe 100644 --- a/test-data/unit/fixtures/transform.pyi +++ b/test-data/unit/fixtures/transform.pyi @@ -28,3 +28,5 @@ def __print(a1=None, a2=None, a3=None, a4=None): # Do not use *args since this would require list and break many test # cases. pass + +class dict: pass diff --git a/test-data/unit/fixtures/tuple-simple.pyi b/test-data/unit/fixtures/tuple-simple.pyi index b195dfa59729..6c816c1c5b7a 100644 --- a/test-data/unit/fixtures/tuple-simple.pyi +++ b/test-data/unit/fixtures/tuple-simple.pyi @@ -18,3 +18,4 @@ class function: pass # We need int for indexing tuples. class int: pass class str: pass # For convenience +class dict: pass diff --git a/test-data/unit/fixtures/tuple.pyi b/test-data/unit/fixtures/tuple.pyi index 60e47dd02220..0261731304b1 100644 --- a/test-data/unit/fixtures/tuple.pyi +++ b/test-data/unit/fixtures/tuple.pyi @@ -51,3 +51,5 @@ def isinstance(x: object, t: type) -> bool: pass def sum(iterable: Iterable[T], start: Optional[T] = None) -> T: pass class BaseException: pass + +class dict: pass diff --git a/test-data/unit/fixtures/union.pyi b/test-data/unit/fixtures/union.pyi index 489e3ddb6ef9..350e145a6f8f 100644 --- a/test-data/unit/fixtures/union.pyi +++ b/test-data/unit/fixtures/union.pyi @@ -15,3 +15,4 @@ class tuple(Generic[T]): pass # We need int for indexing tuples. class int: pass class str: pass # For convenience +class dict: pass diff --git a/test-data/unit/lib-stub/__builtin__.pyi b/test-data/unit/lib-stub/__builtin__.pyi deleted file mode 100644 index f9ee7b74011d..000000000000 --- a/test-data/unit/lib-stub/__builtin__.pyi +++ /dev/null @@ -1,29 +0,0 @@ -from typing import Generic, TypeVar -_T = TypeVar('_T') - -Any = 0 - -class object: - def __init__(self): - # type: () -> None - pass - -class type: - def __init__(self, x): - # type: (Any) -> None - pass - -# These are provided here for convenience. -class int: pass -class float: pass - -class str: pass - -class tuple(Generic[_T]): pass -class function: pass - -class ellipsis: pass - -def print(*args, end=''): pass - -# Definition of None is implicit diff --git a/test-data/unit/lib-stub/builtins.pyi b/test-data/unit/lib-stub/builtins.pyi index 82e0f6135614..c2ac78c41661 100644 --- a/test-data/unit/lib-stub/builtins.pyi +++ b/test-data/unit/lib-stub/builtins.pyi @@ -21,8 +21,13 @@ class function: __name__: str class ellipsis: pass -from typing import Generic, Sequence, TypeVar +from typing import Generic, Iterator, Sequence, TypeVar _T = TypeVar('_T') -class list(Generic[_T], Sequence[_T]): pass +class list(Generic[_T], Sequence[_T]): + def __contains__(self, item: object) -> bool: pass + def __getitem__(self, key: int) -> _T: pass + def __iter__(self) -> Iterator[_T]: pass + +class dict: pass # Definition of None is implicit diff --git a/test-data/unit/merge.test b/test-data/unit/merge.test index a593a064cbb2..144a095440f2 100644 --- a/test-data/unit/merge.test +++ b/test-data/unit/merge.test @@ -669,18 +669,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>) - __match_args__<10> (Tuple[Literal['x']]) - __new__<11> - _asdict<12> - _field_defaults<13> (builtins.object<1>) - _field_types<14> (builtins.object<1>) - _fields<15> (Tuple[builtins.str<9>]) - _make<16> - _replace<17> - _source<18> (builtins.str<9>) - x<19> (target.A<0>))) + __annotations__<7> (builtins.dict[builtins.str<8>, Any]<9>) + __doc__<10> (builtins.str<8>) + __match_args__<11> (Tuple[Literal['x']]) + __new__<12> + _asdict<13> + _field_defaults<14> (builtins.dict[builtins.str<8>, Any]<9>) + _field_types<15> (builtins.dict[builtins.str<8>, Any]<9>) + _fields<16> (Tuple[builtins.str<8>]) + _make<17> + _replace<18> + _source<19> (builtins.str<8>) + x<20> (target.A<0>))) ==> TypeInfo<0>( Name(target.A) @@ -693,19 +693,19 @@ 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>) - __match_args__<10> (Tuple[Literal['x'], Literal['y']]) - __new__<11> - _asdict<12> - _field_defaults<13> (builtins.object<1>) - _field_types<14> (builtins.object<1>) - _fields<15> (Tuple[builtins.str<9>, builtins.str<9>]) - _make<16> - _replace<17> - _source<18> (builtins.str<9>) - x<19> (target.A<0>) - y<20> (target.A<0>))) + __annotations__<7> (builtins.dict[builtins.str<8>, Any]<9>) + __doc__<10> (builtins.str<8>) + __match_args__<11> (Tuple[Literal['x'], Literal['y']]) + __new__<12> + _asdict<13> + _field_defaults<14> (builtins.dict[builtins.str<8>, Any]<9>) + _field_types<15> (builtins.dict[builtins.str<8>, Any]<9>) + _fields<16> (Tuple[builtins.str<8>, builtins.str<8>]) + _make<17> + _replace<18> + _source<19> (builtins.str<8>) + x<20> (target.A<0>) + y<21> (target.A<0>))) [case testNamedTupleOldVersion_typeinfo] import target @@ -730,17 +730,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) @@ -753,18 +753,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 diff --git a/test-data/unit/typexport-basic.test b/test-data/unit/typexport-basic.test index 26caef0d6dde..cd4071eb14ee 100644 --- a/test-data/unit/typexport-basic.test +++ b/test-data/unit/typexport-basic.test @@ -139,6 +139,8 @@ class float: def __sub__(self, x: int) -> float: pass class type: pass class str: pass +class list: pass +class dict: pass [out] OpExpr(3) : builtins.int OpExpr(4) : builtins.float @@ -165,6 +167,8 @@ class bool: pass class type: pass class function: pass class str: pass +class list: pass +class dict: pass [out] ComparisonExpr(3) : builtins.bool ComparisonExpr(4) : builtins.bool From 027cc9258089b31380a7bd47571dcd4c6c0e8e32 Mon Sep 17 00:00:00 2001 From: Ilya Konstantinov Date: Sun, 29 Jan 2023 01:34:06 -0500 Subject: [PATCH 2/4] check yo'self --- mypy/semanal.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/mypy/semanal.py b/mypy/semanal.py index ca2f9bffc5e7..d25387c542bf 100644 --- a/mypy/semanal.py +++ b/mypy/semanal.py @@ -625,21 +625,23 @@ def add_implicit_module_attrs(self, file_node: MypyFile) -> None: continue # Need to construct the type ourselves, to avoid issues with __builtins__.list # not being subscriptable or typing.List not getting bound - typ = self.named_type_or_none("builtins.list", [str_type]) - if typ is None: + inst = self.named_type_or_none("builtins.list", [str_type]) + if inst is None: assert not self.final_iteration, "Cannot find builtins.list to add __path__" self.defer() return + typ = inst elif name == "__annotations__": - typ = self.named_type_or_none( + inst = self.named_type_or_none( "builtins.dict", [str_type, AnyType(TypeOfAny.special_form)] ) - if typ is None: + if inst is None: assert ( not self.final_iteration ), "Cannot find builtins.dict to add __annotations__" self.defer() return + typ = inst else: assert t is not None, f"type should be specified for {name}" typ = UnboundType(t) From 069b2e3d99ea929d2b285eac89cadd94d7668b66 Mon Sep 17 00:00:00 2001 From: Ilya Konstantinov Date: Sun, 29 Jan 2023 22:07:15 -0500 Subject: [PATCH 3/4] Remove testDictMissingFromStubs test --- test-data/unit/check-incomplete-fixture.test | 6 ------ 1 file changed, 6 deletions(-) diff --git a/test-data/unit/check-incomplete-fixture.test b/test-data/unit/check-incomplete-fixture.test index 36eab08885a6..146494df1bd6 100644 --- a/test-data/unit/check-incomplete-fixture.test +++ b/test-data/unit/check-incomplete-fixture.test @@ -12,12 +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:2: error: "dict" expects no type arguments, but 1 given - [case testSetMissingFromStubs] from typing import Set def f(x: Set[int]) -> None: pass From 07d10ae17b078b984f0afa2f5d10c21a1d64a648 Mon Sep 17 00:00:00 2001 From: Ilya Konstantinov Date: Sun, 29 Jan 2023 22:08:15 -0500 Subject: [PATCH 4/4] remove dict, list from SUGGESTED_TEST_FIXTURES --- mypy/messages.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/mypy/messages.py b/mypy/messages.py index 750dcdd42398..b529615e564e 100644 --- a/mypy/messages.py +++ b/mypy/messages.py @@ -122,8 +122,6 @@ # test-data/unit/fixtures/) that provides the definition. This is used for # generating better error messages when running mypy tests only. SUGGESTED_TEST_FIXTURES: Final = { - "builtins.list": "list.pyi", - "builtins.dict": "dict.pyi", "builtins.set": "set.pyi", "builtins.tuple": "tuple.pyi", "builtins.bool": "bool.pyi",