Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to document kwargs using epytext for the auto completion hinting in PyCharm?

Is is possible to get an additional hint for kwargs, which will give you examples of predefined possible keyword arguments? Maybe epytext is not supporting it?

class Person():
    def __init__(self, **kwargs):
        """
        @param name: Name
        @type name: str
        @param age: Age
        @type age: int
        @param connections: Connections to other persons
        @type connections: [Person]
        .
        .
        . # I know this is not working
        """
        self.name = kwargs[name] if name in kwargs
        self.age  = kwargs[age] if age in kwargs
        # and so on ...

Would be great if I'll get something like this in the completion hint (sorry I had to remove the pictures):

  • > self,name,age,connections

Whith a Quick Doku looking like this:

  • No image*

I really like to have global classes with common classes as parents. That makes it much easier for reuse. So here is a little snippet example:

class common():
    PERSON_DETAILS = dict( name       = ' ',
                           age        = 1,
                           connection = []  )

With a bit different defined class of Person:

class Person(common):

    def setDetail(self, **kwargs):
        """
        Set some detail information about the person.
        """
        argErrors = []
        for arg, value in kwargs.iteritems():
            if arg in self.PERSON_DETAILS:
                if type(value)==type(self.PERSON_DETAILS[arg]):
                    self.doSomething() # I don't want to go deeper here
                else:
                    raise ValueError("setDetails(%s) the type of '%s' needs to be %s, %s found" % (arg,arg,type(self.PERSON_DETAILS[arg]),type(value)))
            else:
                raise TypeError("setDetails() got an unexpected keyword argument '%s'" %arg )



person = Person()
person.setDetails()

The following (picture removed) shows what I get as completion hint (which is totally right), but it would be great to have a rolled out argument list from kwargs (like in the first example):

  • > self,**kwargs

I know that the docstring implementation for definitions and auto completion hints are limited, but maybe someone knows a different way to get what I want in PyCharm.

like image 964
MagSec Avatar asked Dec 09 '14 15:12

MagSec


People also ask

How do you document in PyCharm?

Place the caret somewhere within the function you want to document. Press Alt+Enter to show the available intention actions. PyCharm generates documentation comment stub according to docstring format, selected in the Python Integrated Tools page.

How do I automatically add a docstring in PyCharm?

Press Ctrl+Alt+S and go to Editor | General |Smart Keys. Select the Insert type placeholders checkbox in the Smart Keys page of the editor settings. Place the caret at the function name, and press Alt+Enter . In the list of intention actions that opens, choose Insert documentation string stub.

How do you write a docstring in Python?

Declaring Docstrings: The docstrings are declared using ”'triple single quotes”' or “””triple double quotes””” just below the class, method or function declaration. All functions should have a docstring.

What is Kwargs get?

kwargs. get() retrieves a keyword argument passed to a function. The first argument is the keyword and the second is the default value if there was no argument provided for the keyword.


1 Answers

According to the Epytext documentation, you will be able to achieve this by using @keyword.

@param fields should be used to document any explicit parameter (including the keyword parameter). @keywordfields should only be used for non-explicit keyword parameters:

Also, @kwarg p: ... and @kwparam p: ... are synonyms.

like image 85
Azman0101 Avatar answered Sep 27 '22 22:09

Azman0101