[Python] Avoid PyCharm attribute reference warning from type-hinting the base class instead of...

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

  1. Stack

    Stack Membro Participativo

    I would like to avoid a PyCharm warning from unrecognized attributes. This is happening because I type-hint a base class in the function argument, however the function itself uses attributes from derived classes.

    More concretely, this is my code structure:

    @dataclass
    class Base

    @dataclass
    class Derived (Base)
    a: str = 'a'

    class Foo:
    def __init__(self, obj: Base):
    self._obj = obj
    def geta(self):
    return self._obj.a


    The last line gives me the following warning:


    Unresolved attribute reference a for class Base.
    ​

    Is there any way I can avoid this warning by using type-hinting?

    One potential solution is to import Union from the typing module and simply add the list of derived classes in the type hint, however I am expecting quite a few derived classes so I'd rather avoid that.

    Continue reading...

Compartilhe esta Página