[Python] Type hint for a function that returns only a specific set of values

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

  1. Stack

    Stack Membro Participativo

    I have a function that can only return a, b or c, all of them are of type T. I want to include this fact in the signature because of the special meaning they carry in the context of the function. How do I do that?

    Currently, I use this

    def fun(...) -> "a or b or c":
    #briefly explain the meaning of a, b and c in its docstring


    Is that the correct one?

    I know that I can do this

    def fun(...) -> T:
    # briefly explain the meaning of a, b and c in its docstring


    but as I said, I want to express in the signature that the function only returns those specific values.

    Continue reading...

Compartilhe esta Página