[Python] Can you annotate return type when value is instance of cls?

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

  1. Stack

    Stack Membro Participativo

    Given a class with a helper method for initialization:

    class TrivialClass:
    def __init__(self, str_arg: str):
    self.string_attribute = str_arg

    @classmethod
    def from_int(cls, int_arg: int) -> ?:
    str_arg = str(int_arg)
    return cls(str_arg)


    Is it possible to annotate the return type of the from_int method?

    I'v tried both cls and TrivialClass but PyCharm flags them as unresolved references which sounds reasonable at that point in time.

    Continue reading...

Compartilhe esta Página