[Python] How to infer return type of function by passed type[T] [closed]

Discussão em 'Python' iniciado por Stack, Julho 10, 2025.

  1. Stack

    Stack Membro Participativo

    If I have an example function like this:

    def foo[T: int | str](typ: type[T]) -> T:
    ...


    and I call it like this:

    foo(int) # return type is therefore -> int


    My type checker (Pylance) can't infer that the return type of this function is int. From what I have read I'm pretty sure this is intentional, but I'm wondering if there is a workaround.

    Continue reading...

Compartilhe esta Página