Skip to content

Commit f30f676

Browse files
authored
Speed up test cases by simplifying test stubs (#4873)
This makes the set of pytest tests that I normally run locally about 20% faster.
1 parent 41641a0 commit f30f676

10 files changed

+23
-36
lines changed

test-data/unit/check-classes.test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4142,6 +4142,7 @@ class MyDestr(Destroyable):
41424142
reveal_type(Arc[MyDestr]()) # E: Revealed type is '__main__.Arc[__main__.MyDestr*]'
41434143
reveal_type(Arc1[MyDestr]()) # E: Revealed type is '__main__.Arc1[__main__.MyDestr*]'
41444144
[builtins fixtures/bool.pyi]
4145+
[typing fixtures/typing-full.pyi]
41454146

41464147
[case testSixMetaclassErrors]
41474148
import six

test-data/unit/check-expressions.test

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,6 +748,7 @@ divmod('foo', f) # E: Unsupported operand types for divmod ("str" and "float")
748748
divmod('foo', d) # E: Unsupported operand types for divmod ("str" and "Decimal")
749749

750750
[builtins fixtures/divmod.pyi]
751+
[typing fixtures/typing-full.pyi]
751752

752753

753754
-- Unary operators
@@ -1774,6 +1775,7 @@ d = {**a, **b, 'c': 3}
17741775
e = {1: 'a', **a} # E: Argument 1 to "update" of "dict" has incompatible type "Dict[str, int]"; expected "Mapping[int, str]"
17751776
f = {**b} # type: Dict[int, int] # E: List item 0 has incompatible type "Dict[str, int]"; expected "Mapping[int, int]"
17761777
[builtins fixtures/dict.pyi]
1778+
[typing fixtures/typing-full.pyi]
17771779

17781780
[case testDictIncompatibleTypeErrorMessage]
17791781
from typing import Dict, Callable

test-data/unit/check-generics.test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1686,6 +1686,7 @@ from typing import TypeVar, Container
16861686
T = TypeVar('T')
16871687
def f(x: Container[T]) -> T: ...
16881688
reveal_type(f((1, 2))) # E: Revealed type is 'builtins.int*'
1689+
[typing fixtures/typing-full.pyi]
16891690

16901691
[case testClassMethodInGenericClassWithGenericConstructorArg]
16911692
from typing import TypeVar, Generic

test-data/unit/check-protocols.test

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -504,6 +504,7 @@ reveal_type(close(arg)) # E: Revealed type is 'builtins.int*'
504504
reveal_type(close_all([F()])) # E: Revealed type is 'builtins.int*'
505505
reveal_type(close_all([arg])) # E: Revealed type is 'builtins.int*'
506506
[builtins fixtures/isinstancelist.pyi]
507+
[typing fixtures/typing-full.pyi]
507508

508509
[case testProtocolGenericInference2]
509510
from typing import Generic, TypeVar, Protocol
@@ -767,6 +768,7 @@ t: Traversable
767768
t = D[int]() # OK
768769
t = C() # E: Incompatible types in assignment (expression has type "C", variable has type "Traversable")
769770
[builtins fixtures/list.pyi]
771+
[typing fixtures/typing-full.pyi]
770772

771773
[case testRecursiveProtocols2]
772774
from typing import Protocol, TypeVar
@@ -822,6 +824,7 @@ t = A() # OK
822824
t = B() # E: Incompatible types in assignment (expression has type "B", variable has type "P1")
823825
t = C() # E: Incompatible types in assignment (expression has type "C", variable has type "P1")
824826
[builtins fixtures/list.pyi]
827+
[typing fixtures/typing-full.pyi]
825828

826829
[case testMutuallyRecursiveProtocolsTypesWithSubteMismatch]
827830
from typing import Protocol, Sequence, List
@@ -1737,6 +1740,7 @@ bar((1, 2))
17371740
bar(1) # E: Argument 1 to "bar" has incompatible type "int"; expected "Sized"
17381741

17391742
[builtins fixtures/isinstancelist.pyi]
1743+
[typing fixtures/typing-full.pyi]
17401744

17411745
[case testBasicSupportsIntProtocol]
17421746
from typing import SupportsInt
@@ -1752,6 +1756,7 @@ foo(Bar())
17521756
foo('no way') # E: Argument 1 to "foo" has incompatible type "str"; expected "SupportsInt"
17531757

17541758
[builtins fixtures/isinstancelist.pyi]
1759+
[typing fixtures/typing-full.pyi]
17551760

17561761
-- Additional tests and corner cases for protocols
17571762
-- ----------------------------------------------

test-data/unit/check-tuples.test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -834,6 +834,7 @@ fb(aa) # E: Argument 1 to "fb" has incompatible type "Tuple[A, A]"; expected "Tu
834834
from typing import Container
835835
a = None # type: Container[str]
836836
a = ()
837+
[typing fixtures/typing-full.pyi]
837838

838839
[case testSubtypingTupleIsSized]
839840
from typing import Sized

test-data/unit/check-typeddict.test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ def as_mutable_mapping(p: Point) -> MutableMapping[str, int]:
347347
# N: 'Point' is missing following 'MutableMapping' protocol member: \
348348
# N: __setitem__
349349
[builtins fixtures/dict.pyi]
350+
[typing fixtures/typing-full.pyi]
350351

351352
[case testCanConvertTypedDictToAny]
352353
from mypy_extensions import TypedDict

test-data/unit/check-varargs.test

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,7 @@ g(d) # E: Argument 1 to "g" has incompatible type "Dict[str, int]"; expected "Di
614614
h(c) # E: Argument 1 to "h" has incompatible type "Dict[str, float]"; expected "Dict[str, int]"
615615
h(d)
616616
[builtins fixtures/dict.pyi]
617+
[typing fixtures/typing-full.pyi]
617618

618619
[case testInvariantListArgNote]
619620
from typing import List, Union

test-data/unit/fixtures/dict.pyi

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Builtins stub used in dictionary-related test cases.
22

3-
from typing import TypeVar, Generic, Iterable, Iterator, Mapping, Tuple, overload, Optional, Union
3+
from typing import (
4+
TypeVar, Generic, Iterable, Iterator, Mapping, Tuple, overload, Optional, Union, Sequence
5+
)
46

57
T = TypeVar('T')
68
KT = TypeVar('KT')
@@ -11,15 +13,15 @@ class object:
1113

1214
class type: pass
1315

14-
class dict(Generic[KT, VT]):
16+
class dict(Mapping[KT, VT]):
1517
@overload
1618
def __init__(self, **kwargs: VT) -> None: pass
1719
@overload
1820
def __init__(self, arg: Iterable[Tuple[KT, VT]], **kwargs: VT) -> None: pass
1921
def __getitem__(self, key: KT) -> VT: pass
2022
def __setitem__(self, k: KT, v: VT) -> None: pass
2123
def __iter__(self) -> Iterator[KT]: pass
22-
def __contains__(self, item: object) -> bool: pass
24+
def __contains__(self, item: object) -> int: pass
2325
def update(self, a: Mapping[KT, VT]) -> None: pass
2426
@overload
2527
def get(self, k: KT) -> Optional[VT]: pass
@@ -33,7 +35,7 @@ class int: # for convenience
3335
class str: pass # for keyword argument key type
3436
class unicode: pass # needed for py2 docstrings
3537

36-
class list(Generic[T]): # needed by some test cases
38+
class list(Sequence[T]): # needed by some test cases
3739
def __getitem__(self, x: int) -> T: pass
3840
def __iter__(self) -> Iterator[T]: pass
3941
def __mul__(self, x: int) -> list[T]: pass

test-data/unit/fixtures/list.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Builtins stub used in list-related test cases.
22

3-
from typing import TypeVar, Generic, Iterable, Iterator, overload
3+
from typing import TypeVar, Generic, Iterable, Iterator, Sequence, overload
44

55
T = TypeVar('T')
66

@@ -10,7 +10,7 @@ class object:
1010
class type: pass
1111
class ellipsis: pass
1212

13-
class list(Generic[T]):
13+
class list(Sequence[T]):
1414
@overload
1515
def __init__(self) -> None: pass
1616
@overload

test-data/unit/lib-stub/typing.pyi

Lines changed: 3 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
# Stub for typing module. Many of the definitions have special handling in
22
# the type checker, so they can just be initialized to anything.
33

4-
from abc import abstractmethod
5-
6-
class GenericMeta(type): pass
7-
84
cast = 0
95
overload = 0
106
Any = 0
@@ -38,48 +34,25 @@ S = TypeVar('S')
3834
# Note: definitions below are different from typeshed, variances are declared
3935
# to silence the protocol variance checks. Maybe it is better to use type: ignore?
4036

41-
@runtime
42-
class Container(Protocol[T_contra]):
43-
@abstractmethod
44-
# Use int because bool isn't in the default test builtins
45-
def __contains__(self, arg: T_contra) -> int: pass
46-
47-
@runtime
4837
class Sized(Protocol):
49-
@abstractmethod
5038
def __len__(self) -> int: pass
5139

5240
@runtime
5341
class Iterable(Protocol[T_co]):
54-
@abstractmethod
5542
def __iter__(self) -> 'Iterator[T_co]': pass
5643

57-
@runtime
5844
class Iterator(Iterable[T_co], Protocol):
59-
@abstractmethod
6045
def __next__(self) -> T_co: pass
6146

6247
class Generator(Iterator[T], Generic[T, U, V]):
63-
@abstractmethod
6448
def __iter__(self) -> 'Generator[T, U, V]': pass
6549

66-
@runtime
67-
class Sequence(Iterable[T_co], Protocol):
68-
@abstractmethod
50+
class Sequence(Iterable[T_co]):
6951
def __getitem__(self, n: Any) -> T_co: pass
7052

71-
@runtime
72-
class Mapping(Protocol[T_contra, T_co]):
53+
class Mapping(Generic[T_contra, T_co]):
7354
def __getitem__(self, key: T_contra) -> T_co: pass
7455

75-
@runtime
76-
class MutableMapping(Mapping[T_contra, U], Protocol):
77-
def __setitem__(self, k: T_contra, v: U) -> None: pass
78-
79-
class SupportsInt(Protocol):
80-
def __int__(self) -> int: pass
81-
82-
def runtime(cls: T) -> T:
83-
return cls
56+
def runtime(cls: type) -> type: pass
8457

8558
TYPE_CHECKING = 1

0 commit comments

Comments
 (0)