Skip to content

[Feature Request] beartype.vale + PEP 593 typing.Annotated[...] + PEP 692 typing.Unpack[...]` = :hurtrealbad: #562

@EtaoinWu

Description

@EtaoinWu

Let's say you want to write a variadic function, and you want none of its arguments to be none:

def fn(x, *args):
    assert x is not None
    for arg in args:
        assert arg is not None
    # do something fun with x and args

If you want type-annotate this function:

def fn[T, *Ts](
        x: T, 
        *args: *Ts # or equivalently, args: Unpack[Ts]
    ):
    assert x is not None
    for arg in args:
        assert arg is not None
    # do something fun with x and args

Let's say you want to use a beartype.vale validator to make sure that your values are non-None. This is possible on x:

@beartype
def fn[T, *Ts](
        x: Annotated[T, ~IsEqual[None]], 
        *args: *Ts # or equivalently, args: Unpack[Ts]
    ):
    ...

But it breaks down on args. First, Annotated[*Ts, ...] is invalid Python annotation, with an error of

TypeError: Annotated[...] should not be used with an unpacked TypeVarTuple

(This happens at definition time for 3.13 and at get_annotations() time for 3.14.)

Second, this isn't supported by beartype:

@beartype
def fn[T, *Ts](
        x: Annotated[T, ~IsEqual[None]], 
        *args: Unpack[Annotated[Ts, ~IsEqual[None]]]
    ):
    ...

Is it possible for beartype to support this type of validators?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions