Skip to content

Commit

Permalink
fix python#16935 fix type of tuple[X,Y] expression
Browse files Browse the repository at this point in the history
... 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, ...]
  • Loading branch information
urnest committed May 12, 2024
1 parent 82ebd86 commit 849d23d
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 12 deletions.
17 changes: 15 additions & 2 deletions mypy/checkexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -4836,8 +4836,21 @@ def apply_type_arguments_to_callable(
len(args) < min_arg_count or len(args) > len(tp.variables)
) and not has_type_var_tuple:
if tp.is_type_obj() and tp.type_object().fullname == "builtins.tuple":
# TODO: Specialize the callable for the type arguments
return tp
# e.g. expression tuple[X, Y]
# - want the type of the expression i.e. a function with that as its return type
# - tp is type of tuple (note it won't have params as we are only called
# with generic callable type)
# - tuple[X, Y]() takes a single arg that is a tuple containing an X and a Y
return CallableType(
[TupleType(list(args), self.chk.named_type("tuple"))],
[ARG_POS],
["p1"],
TupleType(list(args), self.chk.named_type("tuple")),
self.chk.named_type("abc.ABCMeta"),
name="tuple",
definition=tp.definition,
bound_args=tp.bound_args,
)
self.msg.incompatible_type_application(
min_arg_count, len(tp.variables), len(args), ctx
)
Expand Down
16 changes: 8 additions & 8 deletions test-data/unit/check-incremental.test
Original file line number Diff line number Diff line change
Expand Up @@ -3696,8 +3696,8 @@ cache_fine_grained = False
[file mypy.ini.2]
\[mypy]
cache_fine_grained = True
[rechecked _typeshed, a, builtins, typing]
[stale _typeshed, a, builtins, typing]
[rechecked _typeshed, a, abc, builtins, typing]
[stale _typeshed, a, abc, builtins, typing]
[builtins fixtures/tuple.pyi]

[case testIncrementalPackageNameOverload]
Expand Down Expand Up @@ -3748,8 +3748,8 @@ Signature: 8a477f597d28d172789f06886806bc55
[file b.py.2]
# uh
-- Every file should get reloaded, since the cache was invalidated
[stale _typeshed, a, b, builtins, typing]
[rechecked _typeshed, a, b, builtins, typing]
[stale _typeshed, a, abc, b, builtins, typing]
[rechecked _typeshed, a, abc, b, builtins, typing]
[builtins fixtures/tuple.pyi]

[case testIncrementalBustedFineGrainedCache2]
Expand All @@ -3761,8 +3761,8 @@ import b
[file b.py.2]
# uh
-- Every file should get reloaded, since the settings changed
[stale _typeshed, a, b, builtins, typing]
[rechecked _typeshed, a, b, builtins, typing]
[stale _typeshed, a, abc, b, builtins, typing]
[rechecked _typeshed, a, abc, b, builtins, typing]
[builtins fixtures/tuple.pyi]

[case testIncrementalBustedFineGrainedCache3]
Expand All @@ -3777,8 +3777,8 @@ import b
[file b.py.2]
# uh
-- Every file should get reloaded, since the cache was invalidated
[stale _typeshed, a, b, builtins, typing]
[rechecked _typeshed, a, b, builtins, typing]
[stale _typeshed, a, abc, b, builtins, typing]
[rechecked _typeshed, a, abc, b, builtins, typing]
[builtins fixtures/tuple.pyi]

[case testIncrementalWorkingFineGrainedCache]
Expand Down
41 changes: 41 additions & 0 deletions test-data/unit/check-type-object-type-inference.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
[case testInferTupleType]
# flags: --python-version 3.9
from typing import TypeVar, Generic, Type
from abc import abstractmethod

T = TypeVar('T')
class E(Generic[T]):
@abstractmethod
def e(self, t: T) -> str:
...

class F:
@abstractmethod
def f(self, tp: Type[T]) -> E[T]:
...

def g(f: F):
f.f(int).e(7)
f.f(tuple[int,str])
f.f(tuple[int,str]).e('x') # E: Argument 1 to "e" of "E" has incompatible type "str"; expected "Tuple[int, str]"
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]"
f.f(tuple[int,str]).e( (7,'x') ) # OK
reveal_type(f.f(tuple[int,str]).e) # N: Revealed type is "def (t: Tuple[builtins.int, builtins.str]) -> builtins.str"

def h(f: F):
f.f(int).e(7)
f.f(tuple)
f.f(tuple).e('y') # E: Argument 1 to "e" of "E" has incompatible type "str"; expected "Tuple[Any, ...]"
f.f(tuple).e( (8,'y') ) # OK
reveal_type(f.f(tuple).e) # N: Revealed type is "def (t: builtins.tuple[Any, ...]) -> builtins.str"

def i(f: F):
f.f(tuple[int,tuple[int,str]])
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]]"
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]]"
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]]"
f.f(tuple[int,tuple[int,str]]).e( (27,(28,'z')) ) # OK
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"

x = tuple[int,str][str] # E: The type "Type[Tuple[Any, ...]]" is not generic and not indexable
[builtins fixtures/tuple.pyi]
1 change: 1 addition & 0 deletions test-data/unit/fixtures/tuple.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import _typeshed
from typing import Iterable, Iterator, TypeVar, Generic, Sequence, Optional, overload, Tuple, Type
from abc import ABCMeta

T = TypeVar("T")
Tco = TypeVar('Tco', covariant=True)
Expand Down
3 changes: 1 addition & 2 deletions test-data/unit/pythoneval.test
Original file line number Diff line number Diff line change
Expand Up @@ -1828,7 +1828,6 @@ RHSAlias3: type = tuple[int, ...]
WrongTypeElement = str | tuple[float, 1] # Error
WrongEllipsis = tuple[float, float, ...] | str # Error

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

[case testEnumIterMetaInference]
import socket
Expand Down

0 comments on commit 849d23d

Please sign in to comment.