[Python] No Pylance hints after decorating a class (Python)

Discussão em 'Python' iniciado por Stack, Setembro 26, 2024.

  1. Stack

    Stack Membro Participativo

    I have a question about Pylance hints after decorating a class. I have a useless minimal example:

    class PrimaryModel():

    def print(self):
    print("I am invisible o_O")

    def mydeco(cls: PrimaryModel):
    return cls

    if __name__ == "__main__":

    @mydeco
    class User(PrimaryModel):
    pass

    alan = User()
    alan.print() # no hints


    The problem is alan.print method seems to be unknown in IDE (VS Code). (Code works fine) Now I can fix it by explicit type declaration. Here, it hints well:

    class PrimaryModel():

    def print(self):
    print("I am invisible o_O")

    def mydeco(cls: PrimaryModel):
    return cls

    if __name__ == "__main__":

    @mydeco
    class User(PrimaryModel):
    pass

    alan: PrimaryModel = User()
    alan.print() # hints ok


    But then, if I sublclass the User, again, no hints (uncommenting the decorator enables hints):

    class PrimaryModel():

    def print(self):
    print("I am invisible o_O")

    def mydeco(cls: PrimaryModel):
    return cls

    if __name__ == "__main__":

    @mydeco
    class User(PrimaryModel):
    pass

    class Player(User):
    pass

    alan: PrimaryModel = Player()
    alan.print() # no hints :'(


    Where is the problem? Is it too complex for Pylance to solve the type? Why it does not hint even if I explicitly declare the type? Can it be solved somehow?

    I tried explicit type declaration, checking runtime type. Just did not manage to make it work.

    Continue reading...

Compartilhe esta Página