How to type hints for a genetic input tuple, with list[T] map to T #2064
Replies: 2 comments
|
I believe this is not yet supported because in some cases it can be ambiguous. Given: def func[T, *Ts1, *Ts2](values: tuple[*Ts1, list[T], *Ts2]) -> tuple[*Ts1, T, *Ts2]: ...what should type checkers reveal for the following code? y = func((5, [3], ["foo"], 3.14))
reveal_type(y)There are two different options that are compatible with
Actually, because z = func(([3], ["foo"]))
reveal_type(z) # `tuple[int, list[str]]` or `tuple[list[int], str]`?See the Python Typing specification for another example of ambiguity with |
|
Maybe introducing def split_tuple[*Ts1: int, *Ts2: str](values: tuple[*Ts1, *Ts2]) -> tuple[tuple[*Ts1], tuple[*Ts2]]: ...
class MyInt(int): ...
class MyStr(str): ...
w = split_tuple((MyInt(3), 14, MyStr("hey"), "there"))
reveal_type(w) # tuple[tuple[MyInt, int], tuple[MyStr, str]]I think this is not ambiguous, but note that I'm relying on the fact that it's impossible (at least in CPython) to construct an object that is both an instance of |
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
This is not valid as the document say:
All reactions