Skip to content

TypeVarTuple example has unused TypeVar #95337

Closed
@adriangb

Description

@adriangb

From the docs:

Ts = TypeVarTuple('Ts')

def remove_first_element(tup: tuple[Any, *Ts]) -> tuple[*Ts]:
    return tup[1:]

# Ts is bound to ()
# Return value is (), which has type tuple[()]
remove_first_element(tup=(1,))

# Ts is bound to (str,)
# Return value is ('spam',), which has type tuple[str]
remove_first_element(tup=(1, 'spam'))

# Ts is bound to (str, float)
# Return value is ('spam', 3.0), which has type tuple[str, float]
remove_first_element(tup=(1, 'spam', 3.0))

Notice that "T" is only used once in the function signature, which Pylance will highlight as an error.
In this case, Any is just as meaningful as T (and I would argue semantically better, we don't care what the first element is)
All of the examples (checked with reveal_type) are still valid.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions