Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Attributes set with setattr show as unresolved references in PyCharm

Tags:

pycharm

I have a class that defines several attributes dynamically using setattr builtin. However, all uses of those attributes are shown as unresolved references by PyCharm. Is there a way (via code or PyCharm setting) to help PyCharm resolved those attributes?

like image 596
alejandro Avatar asked Apr 07 '15 17:04

alejandro


1 Answers

Unfortunately you can not.

From my point of view, you can ignore the warning doing Alt + Enter on your dynamic attribute and then selecting "Ignore unresolved reference".

Or, if you don't want do it for all class attributes, you can add @DynamicAttrs in the class docstring.

Here an example:

class YourClassWithSeveralAttributes(object):
    """@DynamicAttrs""" # <-- here

    def __init__(self):
        setattr(self, 'foo', 1)
        setattr(self, 'bar', 2)
        setattr(self, 'foo1', 11)
        setattr(self, 'bar2', 22)

Keep in mind that none of previous solutions will make PyCharm able to recognize your dynamic attributes and the autocompletition feature will be not available for those attributes. They are just way to ignore the warnings.

like image 149
Giordano Avatar answered Dec 15 '22 05:12

Giordano