Skip to content

Commit

Permalink
Speed up tests by simplifying test fixtures (#16560)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
JukkaL committed Nov 25, 2023
1 parent 50d6d0b commit 9289a33
Show file tree
Hide file tree
Showing 9 changed files with 102 additions and 35 deletions.
2 changes: 1 addition & 1 deletion test-data/unit/check-class-namedtuple.test
Original file line number Diff line number Diff line change
Expand Up @@ -301,7 +301,7 @@ reveal_type(X._field_defaults) # N: Revealed type is "builtins.dict[builtins.st
# but it's inferred as `Mapping[str, object]` here due to the fixture we're using
reveal_type(X.__annotations__) # N: Revealed type is "typing.Mapping[builtins.str, builtins.object]"

[builtins fixtures/dict.pyi]
[builtins fixtures/dict-full.pyi]

[case testNewNamedTupleUnit]
from typing import NamedTuple
Expand Down
3 changes: 1 addition & 2 deletions test-data/unit/check-expressions.test
Original file line number Diff line number Diff line change
Expand Up @@ -1589,8 +1589,7 @@ if str():
....a # E: "ellipsis" has no attribute "a"

class A: pass
[builtins fixtures/dict.pyi]
[out]
[builtins fixtures/dict-full.pyi]


-- Yield expression
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-python310.test
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,7 @@ match x:
reveal_type(x) # N: Revealed type is "builtins.list[builtins.list[builtins.dict[builtins.int, builtins.int]]]"
reveal_type(y) # N: Revealed type is "builtins.int"
reveal_type(z) # N: Revealed type is "builtins.int"
[builtins fixtures/dict.pyi]
[builtins fixtures/dict-full.pyi]

[case testMatchNonFinalMatchArgs]
class A:
Expand Down
6 changes: 6 additions & 0 deletions test-data/unit/check-tuples.test
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,12 @@ for x in B(), A():
[builtins fixtures/for.pyi]

[case testTupleIterable]
from typing import Iterable, Optional, TypeVar

T = TypeVar("T")

def sum(iterable: Iterable[T], start: Optional[T] = None) -> T: pass

y = 'a'
x = sum((1,2))
if int():
Expand Down
2 changes: 1 addition & 1 deletion test-data/unit/check-type-aliases.test
Original file line number Diff line number Diff line change
Expand Up @@ -1065,4 +1065,4 @@ def eval(e: Expr) -> int:
return e[1]
elif e[0] == 456:
return -eval(e[1])
[builtins fixtures/dict.pyi]
[builtins fixtures/dict-full.pyi]
4 changes: 2 additions & 2 deletions test-data/unit/check-typeddict.test
Original file line number Diff line number Diff line change
Expand Up @@ -2708,7 +2708,7 @@ class TD(TypedDict):
reveal_type(TD.__iter__) # N: Revealed type is "def (typing._TypedDict) -> typing.Iterator[builtins.str]"
reveal_type(TD.__annotations__) # N: Revealed type is "typing.Mapping[builtins.str, builtins.object]"
reveal_type(TD.values) # N: Revealed type is "def (self: typing.Mapping[T`1, T_co`2]) -> typing.Iterable[T_co`2]"
[builtins fixtures/dict.pyi]
[builtins fixtures/dict-full.pyi]
[typing fixtures/typing-typeddict.pyi]

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

[case testTypedDictWith__ror__method]
Expand Down
83 changes: 83 additions & 0 deletions test-data/unit/fixtures/dict-full.pyi
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Builtins stub used in dictionary-related test cases (more complete).

from _typeshed import SupportsKeysAndGetItem
import _typeshed
from typing import (
TypeVar, Generic, Iterable, Iterator, Mapping, Tuple, overload, Optional, Union, Sequence,
Self,
)

T = TypeVar('T')
T2 = TypeVar('T2')
KT = TypeVar('KT')
VT = TypeVar('VT')

class object:
def __init__(self) -> None: pass
def __init_subclass__(cls) -> None: pass
def __eq__(self, other: object) -> bool: pass

class type:
__annotations__: Mapping[str, object]

class dict(Mapping[KT, VT]):
@overload
def __init__(self, **kwargs: VT) -> None: pass
@overload
def __init__(self, arg: Iterable[Tuple[KT, VT]], **kwargs: VT) -> None: pass
def __getitem__(self, key: KT) -> VT: pass
def __setitem__(self, k: KT, v: VT) -> None: pass
def __iter__(self) -> Iterator[KT]: pass
def __contains__(self, item: object) -> int: pass
def update(self, a: SupportsKeysAndGetItem[KT, VT]) -> None: pass
@overload
def get(self, k: KT) -> Optional[VT]: pass
@overload
def get(self, k: KT, default: Union[VT, T]) -> Union[VT, T]: pass
def __len__(self) -> int: ...

# This was actually added in 3.9:
@overload
def __or__(self, __value: dict[KT, VT]) -> dict[KT, VT]: ...
@overload
def __or__(self, __value: dict[T, T2]) -> dict[Union[KT, T], Union[VT, T2]]: ...
@overload
def __ror__(self, __value: dict[KT, VT]) -> dict[KT, VT]: ...
@overload
def __ror__(self, __value: dict[T, T2]) -> dict[Union[KT, T], Union[VT, T2]]: ...
# dict.__ior__ should be kept roughly in line with MutableMapping.update()
@overload # type: ignore[misc]
def __ior__(self, __value: _typeshed.SupportsKeysAndGetItem[KT, VT]) -> Self: ...
@overload
def __ior__(self, __value: Iterable[Tuple[KT, VT]]) -> Self: ...

class int: # for convenience
def __add__(self, x: Union[int, complex]) -> int: pass
def __radd__(self, x: int) -> int: pass
def __sub__(self, x: Union[int, complex]) -> int: pass
def __neg__(self) -> int: pass
real: int
imag: int

class str: pass # for keyword argument key type
class bytes: pass

class list(Sequence[T]): # needed by some test cases
def __getitem__(self, x: int) -> T: pass
def __iter__(self) -> Iterator[T]: pass
def __mul__(self, x: int) -> list[T]: pass
def __contains__(self, item: object) -> bool: pass
def append(self, item: T) -> None: pass

class tuple(Generic[T]): pass
class function: pass
class float: pass
class complex: pass
class bool(int): pass

class ellipsis:
__class__: object
def isinstance(x: object, t: Union[type, Tuple[type, ...]]) -> bool: pass
class BaseException: pass

def iter(__iterable: Iterable[T]) -> Iterator[T]: pass
33 changes: 7 additions & 26 deletions test-data/unit/fixtures/dict.pyi
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# Builtins stub used in dictionary-related test cases.
# Builtins stub used in dictionary-related test cases (stripped down).
#
# NOTE: Use dict-full.pyi if you need more builtins instead of adding here,
# if feasible.

from _typeshed import SupportsKeysAndGetItem
import _typeshed
Expand All @@ -14,11 +17,9 @@ VT = TypeVar('VT')

class object:
def __init__(self) -> None: pass
def __init_subclass__(cls) -> None: pass
def __eq__(self, other: object) -> bool: pass

class type:
__annotations__: Mapping[str, object]
class type: pass

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

# This was actually added in 3.9:
@overload
def __or__(self, __value: dict[KT, VT]) -> dict[KT, VT]: ...
@overload
def __or__(self, __value: dict[T, T2]) -> dict[Union[KT, T], Union[VT, T2]]: ...
@overload
def __ror__(self, __value: dict[KT, VT]) -> dict[KT, VT]: ...
@overload
def __ror__(self, __value: dict[T, T2]) -> dict[Union[KT, T], Union[VT, T2]]: ...
# dict.__ior__ should be kept roughly in line with MutableMapping.update()
@overload # type: ignore[misc]
def __ior__(self, __value: _typeshed.SupportsKeysAndGetItem[KT, VT]) -> Self: ...
@overload
def __ior__(self, __value: Iterable[Tuple[KT, VT]]) -> Self: ...

class int: # for convenience
def __add__(self, x: Union[int, complex]) -> int: pass
def __radd__(self, x: int) -> int: pass
def __sub__(self, x: Union[int, complex]) -> int: pass
def __neg__(self) -> int: pass
real: int
imag: int

class str: pass # for keyword argument key type
class bytes: pass
Expand All @@ -74,10 +57,8 @@ class function: pass
class float: pass
class complex: pass
class bool(int): pass

class ellipsis:
__class__: object
def isinstance(x: object, t: Union[type, Tuple[type, ...]]) -> bool: pass
class ellipsis: pass
class BaseException: pass

def isinstance(x: object, t: Union[type, Tuple[type, ...]]) -> bool: pass
def iter(__iterable: Iterable[T]) -> Iterator[T]: pass
2 changes: 0 additions & 2 deletions test-data/unit/fixtures/tuple.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,6 @@ class list(Sequence[T], Generic[T]):

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

0 comments on commit 9289a33

Please sign in to comment.