How to "inform" the editor from which class a parameter is (for completions purposes)

Hi,

I am defining a class, which receives another class object as a parameter in the __init__ stage.
example:

class a(object):
    def __init__(b): #b is a class
       a.att1 = b.att1
       a.att2 = b.att2
       a.att3 = b.att3


I would like to have completions for the 'b' class in the __init__ function. currently the editor does not suggest attribute when i start typing 'b.'.

Is there a way to "inform" the editor that b is an object of some class?
becuase for me, b is an object of a big class, which forces me to switch over to that class and search how its attributes are written in order to correctly use them with 'a' class objects.

Thanks!
0
5 comments
With the cursor on 'b' in the definition, hit Alt-Enter (Show Intention Actions) and do "Specify type for reference in docstring."

The type reference then is freeform text, so it can be hard to tell when you've got it set to something pycharm recognizes, but when you do, the Ctrl-Q (Quick Documentation) for that method should show b's "inferred type."

I thought there was a project setting somewhere to set to specify whether docstrings are in Restructured Text or epydoc, but I can't find it now.
0
wonder why there isnt any more robust solution, like a keybinding that opens all the classes defined in the scope and let you select the type. Anyway,Im going to try your solution, thanks!
0
If you use python 3, you can use the extended syntax:
def __init__(self, b:str):

However, this is not compatible with Python 2.7. For that version you have to add type information in docstrings.

def __init__(self, b):
    """
    :param b: some parameter, in this example a string
    :type b: str
    """


You can verify if the syntax is correct, if the type is properly shown in the quick documentation popup (CTRL-Q on windows, CTRL-J on my Mac). There are a lot more options to annotate (return values, list of objects of a certain type, ...) For details see here: http://www.jetbrains.com/pycharm/webhelp/type-hinting-in-pycharm.html

You can also check "Collect run-time types information for code insight" in Preferences > Python debugger. For details see here: http://blog.jetbrains.com/pycharm/2013/02/dynamic-runtime-type-inference-in-pycharm-2-7/
0
I hadn't seen that documentation on type syntax before, though I remember looking for it in the past. That answers a lot of questions!

Though it does raise a few others, i.e. what are "Generics" in Python?
0
LOL. Same thing I was thinking when I read that page.
0

Please sign in to leave a comment.