Another late reply. :-)
On Thu, Oct 22, 2020 at 2:17 AM Matthew Rahtz via Typing-sig < email@example.com> wrote:
Greg - sorry for the slow reply. I agree the ListVariadic is a bit jargony. I'm currently leaning towards TypeVar(variadic=True), pending some discussion in the doc about whether this might cause problems for us down the line if we want to add more features to TypeVar.
Eric - to make sure I understand what you're saying, are you thinking of something like the following?
Ts = ListVariadic('Ts') TsUnion = Union[Ts]
class Tuple(Sequence[UnionTs]): ...
I'm wondering whether there are any use-cases for this apart from making definitions of builtins cleaner - did you have any in mind?
Looks like this dropped off Eric's radar, but what I thought he meant was more the following (using the PEP-585-compliant `tuple` instead of `Tuple`).
The class `tuple` is currently defined like this: ``` class tuple(Sequence[T]): ... ``` but that really only corresponds to `tuple[T, ...]`. Other forms of tuples must be special-cased in the type checker, since they cannot be expressed in the type system directly. Nevertheless the above definition is important, because suppose we have this: ``` def first(a: Sequence[T]) -> T: return a
def foo(a: Tuple[int, str]): b = first(a) # What is the type of b? ``` Here I'd hope that b has type `Union[int, str]` and not `object` or `Any` (mypy currently says `object`).
With variadics we may have the chance of fixing this, removing (most) support for it from the type checker. We could be writing something like ``` class tuple(Sequence[Union[*Ts]], Generic[*Ts]): ... ``` ...and here I am actually not sure how to proceed. Maybe Eric has an idea? How would one spell the constructor?
On Mon, 19 Oct 2020 at 18:42, Eric Traut firstname.lastname@example.org wrote:
Has there been any thought given to a way to "collapse" a ListVariadic into a single type? This would be useful, for example, in the declaration of the built-in `tuple` class. It accepts a variadic TypeVar, but its base class `Sequence` does not. The type argument for `Sequence` needs to be the union of the types in the tuple's ListVariadic.
The reason I ask is that the pyright code contains a bunch of special-case handling for tuples because they are currently the only type that allows variadic TypeVars. It would be great to replace all of this special-case logic with generalized support for variadic generics. The current proposal comes close to enabling this.
-- Eric Traut Contributor to pyright & pylance Microsoft Corp. _______________________________________________ Typing-sig mailing list -- email@example.com To unsubscribe send an email to firstname.lastname@example.org https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: email@example.com
Typing-sig mailing list -- firstname.lastname@example.org To unsubscribe send an email to email@example.com https://mail.python.org/mailman3/lists/typing-sig.python.org/ Member address: firstname.lastname@example.org