[Python] Pythonic way of type-annotating function with generics that I don't care about

Discussão em 'Python' iniciado por Stack, Agosto 29, 2025.

  1. Stack

    Stack Membro Participativo

    Suppose I have a class parametrized by a type in Python 3.13:

    class Foo[T]: ...


    I want to define and type-annotate a method that should work with any Foo[T] instance, regardless of the concrete type T. I don't need the type T at all in this method.

    Some options I can come up with are:

    1. Don't use the brackets:

    def process_foo(x: Foo) -> None: ...

    1. Use an "Any":

    from typing import Any
    def process_foo(x: Foo[Any]) -> None: ...

    1. Parametrize the function too:

    def process_foo[T](x: Foo[T]) -> None: ...


    Option 1 gets a basedpyright error, so I don't like it at all. Option 2 requires an otherwise unnecessary import and gets a basedpyright warning, which can either be silenced with an inline comment (more verbosity and clutter)

    def process_foo(x: Foo[Any]) -> None: ... # pyright: ignore[reportExplicitAny]


    or project-wide, which I don't think is a good idea since the warning is there for a reason. Option 3 is a bit verbose and gets increasingly so if T is bound to some other type; and I also must repeat that bound so it increases coupling unnecessarily (I mean, the whole point is that I don't care about the type):

    class Bar: ...
    class Foo[T: Bar]: ...
    def process_foo[T: Bar](x: Foo[T]) -> None: ...


    Is there any truly pythonic way to do this? None of the above feel like so. Option 2 feels like the closest to me, but still, there must be a better way?

    Continue reading...

Compartilhe esta Página