Skip to content

Commit 2e75cba

Browse files
authored
Make some stubtest tests less verbose (#14961)
I was working on adding some extra checks around type aliases to stubtest, and realised that some tests for type aliases that I added previously could be made much less verbose and much easier to read. (Consolidate the imports into a single case at the top.)
1 parent c517b86 commit 2e75cba

File tree

1 file changed

+28
-49
lines changed

1 file changed

+28
-49
lines changed

mypy/test/teststubtest.py

+28-49
Original file line numberDiff line numberDiff line change
@@ -765,6 +765,20 @@ def read_write_attr(self, val): self._val = val
765765

766766
@collect_cases
767767
def test_type_alias(self) -> Iterator[Case]:
768+
yield Case(
769+
stub="""
770+
import collections.abc
771+
import re
772+
import typing
773+
from typing import Callable, Dict, Generic, Iterable, List, Match, Tuple, TypeVar, Union
774+
""",
775+
runtime="""
776+
import collections.abc
777+
import re
778+
from typing import Callable, Dict, Generic, Iterable, List, Match, Tuple, TypeVar, Union
779+
""",
780+
error=None,
781+
)
768782
yield Case(
769783
stub="""
770784
class X:
@@ -778,27 +792,18 @@ class Y: ...
778792
""",
779793
error="Y.f",
780794
)
781-
yield Case(
782-
stub="""
783-
from typing import Tuple
784-
A = Tuple[int, str]
785-
""",
786-
runtime="A = (int, str)",
787-
error="A",
788-
)
795+
yield Case(stub="A = Tuple[int, str]", runtime="A = (int, str)", error="A")
789796
# Error if an alias isn't present at runtime...
790797
yield Case(stub="B = str", runtime="", error="B")
791798
# ... but only if the alias isn't private
792799
yield Case(stub="_C = int", runtime="", error=None)
793800
yield Case(
794801
stub="""
795-
from typing import Tuple
796802
D = tuple[str, str]
797803
E = Tuple[int, int, int]
798804
F = Tuple[str, int]
799805
""",
800806
runtime="""
801-
from typing import List, Tuple
802807
D = Tuple[str, str]
803808
E = Tuple[int, int, int]
804809
F = List[str]
@@ -807,13 +812,11 @@ class Y: ...
807812
)
808813
yield Case(
809814
stub="""
810-
from typing import Union
811815
G = str | int
812816
H = Union[str, bool]
813817
I = str | int
814818
""",
815819
runtime="""
816-
from typing import Union
817820
G = Union[str, int]
818821
H = Union[str, bool]
819822
I = str
@@ -822,16 +825,12 @@ class Y: ...
822825
)
823826
yield Case(
824827
stub="""
825-
import typing
826-
from collections.abc import Iterable
827-
from typing import Dict
828828
K = dict[str, str]
829829
L = Dict[int, int]
830-
KK = Iterable[str]
830+
KK = collections.abc.Iterable[str]
831831
LL = typing.Iterable[str]
832832
""",
833833
runtime="""
834-
from typing import Iterable, Dict
835834
K = Dict[str, str]
836835
L = Dict[int, int]
837836
KK = Iterable[str]
@@ -841,14 +840,12 @@ class Y: ...
841840
)
842841
yield Case(
843842
stub="""
844-
from typing import Generic, TypeVar
845843
_T = TypeVar("_T")
846844
class _Spam(Generic[_T]):
847845
def foo(self) -> None: ...
848846
IntFood = _Spam[int]
849847
""",
850848
runtime="""
851-
from typing import Generic, TypeVar
852849
_T = TypeVar("_T")
853850
class _Bacon(Generic[_T]):
854851
def foo(self, arg): pass
@@ -859,14 +856,11 @@ def foo(self, arg): pass
859856
yield Case(stub="StrList = list[str]", runtime="StrList = ['foo', 'bar']", error="StrList")
860857
yield Case(
861858
stub="""
862-
import collections.abc
863-
from typing import Callable
864-
N = Callable[[str], bool]
859+
N = typing.Callable[[str], bool]
865860
O = collections.abc.Callable[[int], str]
866-
P = Callable[[str], bool]
861+
P = typing.Callable[[str], bool]
867862
""",
868863
runtime="""
869-
from typing import Callable
870864
N = Callable[[str], bool]
871865
O = Callable[[int], str]
872866
P = int
@@ -897,17 +891,7 @@ class Bar: pass
897891
""",
898892
error=None,
899893
)
900-
yield Case(
901-
stub="""
902-
from typing import Match
903-
M = Match[str]
904-
""",
905-
runtime="""
906-
from typing import Match
907-
M = Match[str]
908-
""",
909-
error=None,
910-
)
894+
yield Case(stub="M = Match[str]", runtime="M = Match[str]", error=None)
911895
yield Case(
912896
stub="""
913897
class Baz:
@@ -940,37 +924,32 @@ def fizz(self): pass
940924
if sys.version_info >= (3, 10):
941925
yield Case(
942926
stub="""
943-
import collections.abc
944-
import re
945-
from typing import Callable, Dict, Match, Iterable, Tuple, Union
946927
Q = Dict[str, str]
947928
R = dict[int, int]
948929
S = Tuple[int, int]
949930
T = tuple[str, str]
950931
U = int | str
951932
V = Union[int, str]
952-
W = Callable[[str], bool]
933+
W = typing.Callable[[str], bool]
953934
Z = collections.abc.Callable[[str], bool]
954-
QQ = Iterable[str]
935+
QQ = typing.Iterable[str]
955936
RR = collections.abc.Iterable[str]
956-
MM = Match[str]
937+
MM = typing.Match[str]
957938
MMM = re.Match[str]
958939
""",
959940
runtime="""
960-
from collections.abc import Callable, Iterable
961-
from re import Match
962941
Q = dict[str, str]
963942
R = dict[int, int]
964943
S = tuple[int, int]
965944
T = tuple[str, str]
966945
U = int | str
967946
V = int | str
968-
W = Callable[[str], bool]
969-
Z = Callable[[str], bool]
970-
QQ = Iterable[str]
971-
RR = Iterable[str]
972-
MM = Match[str]
973-
MMM = Match[str]
947+
W = collections.abc.Callable[[str], bool]
948+
Z = collections.abc.Callable[[str], bool]
949+
QQ = collections.abc.Iterable[str]
950+
RR = collections.abc.Iterable[str]
951+
MM = re.Match[str]
952+
MMM = re.Match[str]
974953
""",
975954
error=None,
976955
)

0 commit comments

Comments
 (0)