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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With