Skip to content

Commit 5e1b3ff

Browse files
committed
fix #16935 fix type of tuple[X,Y] expression
... so e.g. reveal_type(tuple[int, int]) gives expected def (p0: builtins.int, p1: builtins.int) -> tuple[builtins.int, builtins.int] ... rather than def [_T_co] (typing.Iterable[_T_co`1] =) -> builtins.tuple[_T_co`1, ...]
1 parent 35fbd2a commit 5e1b3ff

File tree

5 files changed

+66
-12
lines changed

5 files changed

+66
-12
lines changed

mypy/checkexpr.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4836,8 +4836,21 @@ def apply_type_arguments_to_callable(
48364836
len(args) < min_arg_count or len(args) > len(tp.variables)
48374837
) and not has_type_var_tuple:
48384838
if tp.is_type_obj() and tp.type_object().fullname == "builtins.tuple":
4839-
# TODO: Specialize the callable for the type arguments
4840-
return tp
4839+
# e.g. expression tuple[X, Y]
4840+
# - want the type of the expression i.e. a function with that as its return type
4841+
# - tp is type of tuple (note it won't have params as we are only called
4842+
# with generic callable type)
4843+
# - tuple[X, Y]() takes a single arg that is a tuple containing an X and a Y
4844+
return CallableType(
4845+
[TupleType(list(args), self.chk.named_type("tuple"))],
4846+
[ARG_POS],
4847+
["p1"],
4848+
TupleType(list(args), self.chk.named_type("tuple")),
4849+
self.chk.named_type("abc.ABCMeta"),
4850+
name="tuple",
4851+
definition=tp.definition,
4852+
bound_args=tp.bound_args,
4853+
)
48414854
self.msg.incompatible_type_application(
48424855
min_arg_count, len(tp.variables), len(args), ctx
48434856
)

test-data/unit/check-incremental.test

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3696,8 +3696,8 @@ cache_fine_grained = False
36963696
[file mypy.ini.2]
36973697
\[mypy]
36983698
cache_fine_grained = True
3699-
[rechecked _typeshed, a, builtins, typing]
3700-
[stale _typeshed, a, builtins, typing]
3699+
[rechecked _typeshed, a, abc, builtins, typing]
3700+
[stale _typeshed, a, abc, builtins, typing]
37013701
[builtins fixtures/tuple.pyi]
37023702

37033703
[case testIncrementalPackageNameOverload]
@@ -3748,8 +3748,8 @@ Signature: 8a477f597d28d172789f06886806bc55
37483748
[file b.py.2]
37493749
# uh
37503750
-- Every file should get reloaded, since the cache was invalidated
3751-
[stale _typeshed, a, b, builtins, typing]
3752-
[rechecked _typeshed, a, b, builtins, typing]
3751+
[stale _typeshed, a, abc, b, builtins, typing]
3752+
[rechecked _typeshed, a, abc, b, builtins, typing]
37533753
[builtins fixtures/tuple.pyi]
37543754

37553755
[case testIncrementalBustedFineGrainedCache2]
@@ -3761,8 +3761,8 @@ import b
37613761
[file b.py.2]
37623762
# uh
37633763
-- Every file should get reloaded, since the settings changed
3764-
[stale _typeshed, a, b, builtins, typing]
3765-
[rechecked _typeshed, a, b, builtins, typing]
3764+
[stale _typeshed, a, abc, b, builtins, typing]
3765+
[rechecked _typeshed, a, abc, b, builtins, typing]
37663766
[builtins fixtures/tuple.pyi]
37673767

37683768
[case testIncrementalBustedFineGrainedCache3]
@@ -3777,8 +3777,8 @@ import b
37773777
[file b.py.2]
37783778
# uh
37793779
-- Every file should get reloaded, since the cache was invalidated
3780-
[stale _typeshed, a, b, builtins, typing]
3781-
[rechecked _typeshed, a, b, builtins, typing]
3780+
[stale _typeshed, a, abc, b, builtins, typing]
3781+
[rechecked _typeshed, a, abc, b, builtins, typing]
37823782
[builtins fixtures/tuple.pyi]
37833783

37843784
[case testIncrementalWorkingFineGrainedCache]
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
[case testInferTupleType]
2+
# flags: --python-version 3.9
3+
from typing import TypeVar, Generic, Type
4+
from abc import abstractmethod
5+
6+
T = TypeVar('T')
7+
class E(Generic[T]):
8+
@abstractmethod
9+
def e(self, t: T) -> str:
10+
...
11+
12+
class F:
13+
@abstractmethod
14+
def f(self, tp: Type[T]) -> E[T]:
15+
...
16+
17+
def g(f: F):
18+
f.f(int).e(7)
19+
f.f(tuple[int,str])
20+
f.f(tuple[int,str]).e('x') # E: Argument 1 to "e" of "E" has incompatible type "str"; expected "Tuple[int, str]"
21+
f.f(tuple[int,str]).e( (7,8) ) # E: Argument 1 to "e" of "E" has incompatible type "Tuple[int, int]"; expected "Tuple[int, str]"
22+
f.f(tuple[int,str]).e( (7,'x') ) # OK
23+
reveal_type(f.f(tuple[int,str]).e) # N: Revealed type is "def (t: Tuple[builtins.int, builtins.str]) -> builtins.str"
24+
25+
def h(f: F):
26+
f.f(int).e(7)
27+
f.f(tuple)
28+
f.f(tuple).e('y') # E: Argument 1 to "e" of "E" has incompatible type "str"; expected "Tuple[Any, ...]"
29+
f.f(tuple).e( (8,'y') ) # OK
30+
reveal_type(f.f(tuple).e) # N: Revealed type is "def (t: builtins.tuple[Any, ...]) -> builtins.str"
31+
32+
def i(f: F):
33+
f.f(tuple[int,tuple[int,str]])
34+
f.f(tuple[int,tuple[int,str]]).e('z') # E: Argument 1 to "e" of "E" has incompatible type "str"; expected "Tuple[int, Tuple[int, str]]"
35+
f.f(tuple[int,tuple[int,str]]).e( (8,9) ) # E: Argument 1 to "e" of "E" has incompatible type "Tuple[int, int]"; expected "Tuple[int, Tuple[int, str]]"
36+
f.f(tuple[int,tuple[int,str]]).e( (17, (28, 29)) ) # E: Argument 1 to "e" of "E" has incompatible type "Tuple[int, Tuple[int, int]]"; expected "Tuple[int, Tuple[int, str]]"
37+
f.f(tuple[int,tuple[int,str]]).e( (27,(28,'z')) ) # OK
38+
reveal_type(f.f(tuple[int,tuple[int,str]]).e) # N: Revealed type is "def (t: Tuple[builtins.int, Tuple[builtins.int, builtins.str]]) -> builtins.str"
39+
40+
x = tuple[int,str][str] # E: The type "Type[Tuple[Any, ...]]" is not generic and not indexable
41+
[builtins fixtures/tuple.pyi]

test-data/unit/fixtures/tuple.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import _typeshed
44
from typing import Iterable, Iterator, TypeVar, Generic, Sequence, Optional, overload, Tuple, Type
5+
from abc import ABCMeta
56

67
T = TypeVar("T")
78
Tco = TypeVar('Tco', covariant=True)

test-data/unit/pythoneval.test

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,7 +1828,6 @@ RHSAlias3: type = tuple[int, ...]
18281828
WrongTypeElement = str | tuple[float, 1] # Error
18291829
WrongEllipsis = tuple[float, float, ...] | str # Error
18301830

1831-
# TODO: This should produce a fixed-length tuple
18321831
reveal_type(tuple[int, str]((1, "x")))
18331832
[out]
18341833
_testTupleWithDifferentArgsPy310.py:15: note: Revealed type is "Union[builtins.str, Tuple[builtins.float, builtins.float, builtins.str]]"
@@ -1839,7 +1838,7 @@ _testTupleWithDifferentArgsPy310.py:19: note: Revealed type is "builtins.tuple[b
18391838
_testTupleWithDifferentArgsPy310.py:20: note: Revealed type is "builtins.list[Tuple[builtins.int, builtins.str]]"
18401839
_testTupleWithDifferentArgsPy310.py:26: error: Invalid type: try using Literal[1] instead?
18411840
_testTupleWithDifferentArgsPy310.py:27: error: Unexpected "..."
1842-
_testTupleWithDifferentArgsPy310.py:30: note: Revealed type is "builtins.tuple[builtins.object, ...]"
1841+
_testTupleWithDifferentArgsPy310.py:29: note: Revealed type is "Tuple[builtins.int, builtins.str]"
18431842

18441843
[case testEnumIterMetaInference]
18451844
import socket

0 commit comments

Comments
 (0)