Skip to content

Commit 9289a33

Browse files
authored
Speed up tests by simplifying test fixtures (#16560)
Move some definitions away from commonly used fixtures that are only needed in one or two test cases, as they will slow down many test cases. This speeds up `mypy/test/testcheck.py` by about 5% on my Linux desktop.
1 parent 50d6d0b commit 9289a33

File tree

9 files changed

+102
-35
lines changed

9 files changed

+102
-35
lines changed

test-data/unit/check-class-namedtuple.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,7 +301,7 @@ reveal_type(X._field_defaults) # N: Revealed type is "builtins.dict[builtins.st
301301
# but it's inferred as `Mapping[str, object]` here due to the fixture we're using
302302
reveal_type(X.__annotations__) # N: Revealed type is "typing.Mapping[builtins.str, builtins.object]"
303303

304-
[builtins fixtures/dict.pyi]
304+
[builtins fixtures/dict-full.pyi]
305305

306306
[case testNewNamedTupleUnit]
307307
from typing import NamedTuple

test-data/unit/check-expressions.test

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1589,8 +1589,7 @@ if str():
15891589
....a # E: "ellipsis" has no attribute "a"
15901590

15911591
class A: pass
1592-
[builtins fixtures/dict.pyi]
1593-
[out]
1592+
[builtins fixtures/dict-full.pyi]
15941593

15951594

15961595
-- Yield expression

test-data/unit/check-python310.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -931,7 +931,7 @@ match x:
931931
reveal_type(x) # N: Revealed type is "builtins.list[builtins.list[builtins.dict[builtins.int, builtins.int]]]"
932932
reveal_type(y) # N: Revealed type is "builtins.int"
933933
reveal_type(z) # N: Revealed type is "builtins.int"
934-
[builtins fixtures/dict.pyi]
934+
[builtins fixtures/dict-full.pyi]
935935

936936
[case testMatchNonFinalMatchArgs]
937937
class A:

test-data/unit/check-tuples.test

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -957,6 +957,12 @@ for x in B(), A():
957957
[builtins fixtures/for.pyi]
958958

959959
[case testTupleIterable]
960+
from typing import Iterable, Optional, TypeVar
961+
962+
T = TypeVar("T")
963+
964+
def sum(iterable: Iterable[T], start: Optional[T] = None) -> T: pass
965+
960966
y = 'a'
961967
x = sum((1,2))
962968
if int():

test-data/unit/check-type-aliases.test

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1065,4 +1065,4 @@ def eval(e: Expr) -> int:
10651065
return e[1]
10661066
elif e[0] == 456:
10671067
return -eval(e[1])
1068-
[builtins fixtures/dict.pyi]
1068+
[builtins fixtures/dict-full.pyi]

test-data/unit/check-typeddict.test

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2708,7 +2708,7 @@ class TD(TypedDict):
27082708
reveal_type(TD.__iter__) # N: Revealed type is "def (typing._TypedDict) -> typing.Iterator[builtins.str]"
27092709
reveal_type(TD.__annotations__) # N: Revealed type is "typing.Mapping[builtins.str, builtins.object]"
27102710
reveal_type(TD.values) # N: Revealed type is "def (self: typing.Mapping[T`1, T_co`2]) -> typing.Iterable[T_co`2]"
2711-
[builtins fixtures/dict.pyi]
2711+
[builtins fixtures/dict-full.pyi]
27122712
[typing fixtures/typing-typeddict.pyi]
27132713

27142714
[case testGenericTypedDictAlias]
@@ -3299,7 +3299,7 @@ main:10: error: No overload variant of "__ror__" of "dict" matches argument type
32993299
main:10: note: Possible overload variants:
33003300
main:10: note: def __ror__(self, Dict[Any, Any], /) -> Dict[Any, Any]
33013301
main:10: note: def [T, T2] __ror__(self, Dict[T, T2], /) -> Dict[Union[Any, T], Union[Any, T2]]
3302-
[builtins fixtures/dict.pyi]
3302+
[builtins fixtures/dict-full.pyi]
33033303
[typing fixtures/typing-typeddict-iror.pyi]
33043304

33053305
[case testTypedDictWith__ror__method]

test-data/unit/fixtures/dict-full.pyi

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
# Builtins stub used in dictionary-related test cases (more complete).
2+
3+
from _typeshed import SupportsKeysAndGetItem
4+
import _typeshed
5+
from typing import (
6+
TypeVar, Generic, Iterable, Iterator, Mapping, Tuple, overload, Optional, Union, Sequence,
7+
Self,
8+
)
9+
10+
T = TypeVar('T')
11+
T2 = TypeVar('T2')
12+
KT = TypeVar('KT')
13+
VT = TypeVar('VT')
14+
15+
class object:
16+
def __init__(self) -> None: pass
17+
def __init_subclass__(cls) -> None: pass
18+
def __eq__(self, other: object) -> bool: pass
19+
20+
class type:
21+
__annotations__: Mapping[str, object]
22+
23+
class dict(Mapping[KT, VT]):
24+
@overload
25+
def __init__(self, **kwargs: VT) -> None: pass
26+
@overload
27+
def __init__(self, arg: Iterable[Tuple[KT, VT]], **kwargs: VT) -> None: pass
28+
def __getitem__(self, key: KT) -> VT: pass
29+
def __setitem__(self, k: KT, v: VT) -> None: pass
30+
def __iter__(self) -> Iterator[KT]: pass
31+
def __contains__(self, item: object) -> int: pass
32+
def update(self, a: SupportsKeysAndGetItem[KT, VT]) -> None: pass
33+
@overload
34+
def get(self, k: KT) -> Optional[VT]: pass
35+
@overload
36+
def get(self, k: KT, default: Union[VT, T]) -> Union[VT, T]: pass
37+
def __len__(self) -> int: ...
38+
39+
# This was actually added in 3.9:
40+
@overload
41+
def __or__(self, __value: dict[KT, VT]) -> dict[KT, VT]: ...
42+
@overload
43+
def __or__(self, __value: dict[T, T2]) -> dict[Union[KT, T], Union[VT, T2]]: ...
44+
@overload
45+
def __ror__(self, __value: dict[KT, VT]) -> dict[KT, VT]: ...
46+
@overload
47+
def __ror__(self, __value: dict[T, T2]) -> dict[Union[KT, T], Union[VT, T2]]: ...
48+
# dict.__ior__ should be kept roughly in line with MutableMapping.update()
49+
@overload # type: ignore[misc]
50+
def __ior__(self, __value: _typeshed.SupportsKeysAndGetItem[KT, VT]) -> Self: ...
51+
@overload
52+
def __ior__(self, __value: Iterable[Tuple[KT, VT]]) -> Self: ...
53+
54+
class int: # for convenience
55+
def __add__(self, x: Union[int, complex]) -> int: pass
56+
def __radd__(self, x: int) -> int: pass
57+
def __sub__(self, x: Union[int, complex]) -> int: pass
58+
def __neg__(self) -> int: pass
59+
real: int
60+
imag: int
61+
62+
class str: pass # for keyword argument key type
63+
class bytes: pass
64+
65+
class list(Sequence[T]): # needed by some test cases
66+
def __getitem__(self, x: int) -> T: pass
67+
def __iter__(self) -> Iterator[T]: pass
68+
def __mul__(self, x: int) -> list[T]: pass
69+
def __contains__(self, item: object) -> bool: pass
70+
def append(self, item: T) -> None: pass
71+
72+
class tuple(Generic[T]): pass
73+
class function: pass
74+
class float: pass
75+
class complex: pass
76+
class bool(int): pass
77+
78+
class ellipsis:
79+
__class__: object
80+
def isinstance(x: object, t: Union[type, Tuple[type, ...]]) -> bool: pass
81+
class BaseException: pass
82+
83+
def iter(__iterable: Iterable[T]) -> Iterator[T]: pass

test-data/unit/fixtures/dict.pyi

Lines changed: 7 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
# Builtins stub used in dictionary-related test cases.
1+
# Builtins stub used in dictionary-related test cases (stripped down).
2+
#
3+
# NOTE: Use dict-full.pyi if you need more builtins instead of adding here,
4+
# if feasible.
25

36
from _typeshed import SupportsKeysAndGetItem
47
import _typeshed
@@ -14,11 +17,9 @@ VT = TypeVar('VT')
1417

1518
class object:
1619
def __init__(self) -> None: pass
17-
def __init_subclass__(cls) -> None: pass
1820
def __eq__(self, other: object) -> bool: pass
1921

20-
class type:
21-
__annotations__: Mapping[str, object]
22+
class type: pass
2223

2324
class dict(Mapping[KT, VT]):
2425
@overload
@@ -36,28 +37,10 @@ class dict(Mapping[KT, VT]):
3637
def get(self, k: KT, default: Union[VT, T]) -> Union[VT, T]: pass
3738
def __len__(self) -> int: ...
3839

39-
# This was actually added in 3.9:
40-
@overload
41-
def __or__(self, __value: dict[KT, VT]) -> dict[KT, VT]: ...
42-
@overload
43-
def __or__(self, __value: dict[T, T2]) -> dict[Union[KT, T], Union[VT, T2]]: ...
44-
@overload
45-
def __ror__(self, __value: dict[KT, VT]) -> dict[KT, VT]: ...
46-
@overload
47-
def __ror__(self, __value: dict[T, T2]) -> dict[Union[KT, T], Union[VT, T2]]: ...
48-
# dict.__ior__ should be kept roughly in line with MutableMapping.update()
49-
@overload # type: ignore[misc]
50-
def __ior__(self, __value: _typeshed.SupportsKeysAndGetItem[KT, VT]) -> Self: ...
51-
@overload
52-
def __ior__(self, __value: Iterable[Tuple[KT, VT]]) -> Self: ...
53-
5440
class int: # for convenience
5541
def __add__(self, x: Union[int, complex]) -> int: pass
5642
def __radd__(self, x: int) -> int: pass
5743
def __sub__(self, x: Union[int, complex]) -> int: pass
58-
def __neg__(self) -> int: pass
59-
real: int
60-
imag: int
6144

6245
class str: pass # for keyword argument key type
6346
class bytes: pass
@@ -74,10 +57,8 @@ class function: pass
7457
class float: pass
7558
class complex: pass
7659
class bool(int): pass
77-
78-
class ellipsis:
79-
__class__: object
80-
def isinstance(x: object, t: Union[type, Tuple[type, ...]]) -> bool: pass
60+
class ellipsis: pass
8161
class BaseException: pass
8262

63+
def isinstance(x: object, t: Union[type, Tuple[type, ...]]) -> bool: pass
8364
def iter(__iterable: Iterable[T]) -> Iterator[T]: pass

test-data/unit/fixtures/tuple.pyi

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,6 @@ class list(Sequence[T], Generic[T]):
4949

5050
def isinstance(x: object, t: type) -> bool: pass
5151

52-
def sum(iterable: Iterable[T], start: Optional[T] = None) -> T: pass
53-
5452
class BaseException: pass
5553

5654
class dict: pass

0 commit comments

Comments
 (0)