[Python] How do I type hint a method with the type of the enclosing class?

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

  1. Stack

    Stack Membro Participativo

    I have the following code in Python 3:

    class Position:

    def __init__(self, x: int, y: int):
    self.x = x
    self.y = y

    def __add__(self, other: Position) -> Position:
    return Position(self.x + other.x, self.y + other.y)


    But my editor (PyCharm) says that the reference Position can not be resolved (in the __add__ method). How should I specify that I expect the return type to be of type Position?

    I think this is actually a PyCharm issue. It actually uses the information in its warnings, and code completion.

    [​IMG]

    But correct me if I'm wrong, and need to use some other syntax.

    Continue reading...

Compartilhe esta Página