[Python] (Python type hints) How to specify type being currently defined as the returned type...

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

  1. Stack

    Stack Membro Participativo

    I would like to specify (as a type hint) the currently defined type as the return type from a method.

    Here's an example:

    class Action(Enum):
    ignore = 0
    replace = 1
    delete = 2

    @classmethod
    # I would like something like
    # def idToObj(cls, elmId: int)->Action:
    # but I cannot specify Action as the return type
    # since it would generate the error
    # NameError: name 'Action' is not defined
    def idToObj(cls, elmId: int):
    if not hasattr(cls, '_idToObjDict'):
    cls._idToObjDict = {}
    for elm in list(cls):
    cls._idToObjDict[elm.value] = elm

    return cls._idToObjDict[elmId]


    Ideally I would have liked to specify something like

    def idToObj(cls, elmId: int)->Action:


    Thank you.

    Continue reading...

Compartilhe esta Página