pythonpropertiespycharmstubpyi

python stub property "Unresolved Attribute Reference" in Class Implementation


I made a Class Interface in my pyi module (scheme.pyi):

class Catalog:

    @property
    def elements(self) -> List[Element]: ...

and in my scheme.py I implemented the class like this:

class Catalog:

    def __init__(self, element_collection):
        self.__elements = element_collection

    @property
    def elements(self):
        return self.__elements

PyCharm says "Unresolved Attribute Reference "__elements" for class Catalog"


Solution

  • I think it will work if you make Class Interface like so:

    class Catalog:
    
        def __init__(self, element_collection) -> None:
            self.__elements: List[Element]
    
        @property
        def elements(self) -> List[Element]:
    

    or you can optionally declare instance variables in the class body like so:

    class Catalog:
    
        self.__elements: List[Element]
    
        @property
        def elements(self) -> List[Element]: