Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tell PyCharm code generated fields of class

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

like image 499
deinonychusaur Avatar asked Apr 29 '15 12:04

deinonychusaur


People also ask

How do I get the details of a function in PyCharm?

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.

How do I see all references in PyCharm?

From the main menu, choose Edit | Find Usages | Find Usages in File, or press Ctrl+F7 .

Does PyCharm have code completion?

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.

What is Python stub in PyCharm?

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.


2 Answers

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.

like image 112
TomBo Avatar answered Oct 18 '22 01:10

TomBo


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__))
like image 30
Ramast Avatar answered Oct 17 '22 23:10

Ramast