Skip to content

Commit 6c24ea6

Browse files
authored
fix #16935 fix type of tuple[X,Y] expression (#17235)
implement the mypy/checkexpr.py TODO: Specialize the callable for the type arguments ... so e.g. reveal_type(tuple[int, int]) gives expected def (p0: tuple[builtins.int, builtins.int]) -> tuple[builtins.int, builtins.int] ... rather than def [_T_co] (typing.Iterable[_T_co`1] =) -> builtins.tuple[_T_co`1, ...] Fixes #16935
1 parent b207550 commit 6c24ea6

File tree

3 files changed

+57
-4
lines changed

3 files changed

+57
-4
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+
[None],
4848+
TupleType(list(args), self.chk.named_type("tuple")),
4849+
tp.fallback,
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
)
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/pythoneval.test

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

1837-
# TODO: This should produce a fixed-length tuple
18381837
reveal_type(tuple[int, str]((1, "x")))
18391838
[out]
18401839
_testTupleWithDifferentArgsPy310.py:15: note: Revealed type is "Union[builtins.str, Tuple[builtins.float, builtins.float, builtins.str]]"
@@ -1845,7 +1844,7 @@ _testTupleWithDifferentArgsPy310.py:19: note: Revealed type is "builtins.tuple[b
18451844
_testTupleWithDifferentArgsPy310.py:20: note: Revealed type is "builtins.list[Tuple[builtins.int, builtins.str]]"
18461845
_testTupleWithDifferentArgsPy310.py:26: error: Invalid type: try using Literal[1] instead?
18471846
_testTupleWithDifferentArgsPy310.py:27: error: Unexpected "..."
1848-
_testTupleWithDifferentArgsPy310.py:30: note: Revealed type is "builtins.tuple[builtins.object, ...]"
1847+
_testTupleWithDifferentArgsPy310.py:29: note: Revealed type is "Tuple[builtins.int, builtins.str]"
18491848

18501849
[case testEnumIterMetaInference]
18511850
import socket

0 commit comments

Comments
 (0)