Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement basic *args support for variadic generics #13889

Merged
merged 2 commits into from
Oct 17, 2022

Conversation

jhance
Copy link
Collaborator

@jhance jhance commented Oct 13, 2022

This implements the most basic support for the *args feature but various edge cases are not handled in this PR because of the large volume of places that needed to be modified to support this.

In particular, we need to special handle the ARG_STAR argument in several places for the case where the type is a UnpackType. Finally when we actually check a function we need to construct a TupleType instead of a builtins.tuple.

This implements the most basic support for the *args feature but
various edge cases are not handled in this PR because of the large
volume of places that needed to be modified to support this.

In particular, we need to special handle the ARG_STAR argument in
several places for the case where the type is a UnpackType. Finally
when we actually check a function we need to construct a TupleType
instead of a builtins.tuple.
@github-actions

This comment has been minimized.

@github-actions
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@JukkaL JukkaL merged commit c810a9c into python:master Oct 17, 2022

# TODO: add less trivial tests with prefix/suffix etc.
# TODO: add tests that call with a type var tuple instead of just args.
def args_to_tuple(*args: Unpack[Ts]) -> Tuple[Unpack[Ts]]:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will this PR work for a non-typevartuple use case? I have seen people use something like this def foo(*args: Unpack[tuple[int, str]]) (but it will be probably more useful with a type alias, to re-use in different functions, similar to how we allow TypedDicts to re-use types for **kwds).

Btw currently this use case may cause a crash, see first example in #13790 (comment)

If this PR helps with this direction, could you please add a test case? Otherwise consider this a sub-feature request.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another common use case with *args which crashes currently.

from typing import Callable
from typing_extensions import TypeVarTuple, Unpack

Ts = TypeVarTuple("Ts")

def a(var: int) -> None: ...

def func(f: Callable[[Unpack[Ts]], None], *args: Unpack[Ts]) -> None:
    f(*args)

func(a, 42)

@tyralla tyralla mentioned this pull request Nov 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants