As a minimal case I have a class Example
that works like in abstract capacity for a range of other classes.
class Example(object):
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
class Test(Example):
def __init__(self, foo='bar'):
super(Test, self).__init__(foo=foo)
In the real case, Example
does more things.
Is there a way to on Test
inform PyCharm that Test
will have one field Test.foo
and even better, let it know that foo
is expected to be a string?
To be clear, consider delegating the setting of fields from Example
to Test
not possible.
The closest I've gotten is the @ivar
of Epydoc, but I can't get it to work
View Quick Documentation in a popupPlace the caret at the symbol and press Ctrl+Q (View | Quick Documentation). Press Ctrl+Q again to open this documentation in the Documentation tool window.
From the main menu, choose Edit | Find Usages | Find Usages in File, or press Ctrl+F7 .
Machine-learning-assisted code completionPyCharm allows you to prioritize completion suggestions based on choices that other users made in similar situations. The ML completion mechanism doesn't add any new elements but orders the elements retrieved from code.
Stubs Last modified: 17 March 2022. PyCharm supports Python stub files with the . pyi extension. These files allow you to specify the type hints using Python 3 syntax for both Python 2 and 3.
As others have mentioned, you can't. However, you can tell PyCharm to accept missing attributes with @DynamicAttrs
:
class Example(object):
"""
@DynamicAttrs
"""
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
Update: If Python3.5 is an option, see this question about using type hints for dynamic attributes.
I had exactly same problem you are facing.
I didn't want code suggestion I just wanted PyCharm to stop warning me about undefined attributes ( foo is not attribute of class Test
)
I couldn't fix the code hinting problem but I overcame the warning by implementing Example
class like that
class Example(object):
def __init__(self, **kwargs):
for key, value in kwargs.items():
setattr(self, key, value)
def __getattr__(self, name):
"""
Does absolutely nothing, only for pycharm to stop complaining about massing attributes
This function is *only* called when python fail to find certain attribute, so it always raises exception
"""
raise AttributeError("Attribute %s is not part of %s class" % (name, self.__class__.__name__))
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