-1

Suppose I have such function:

def render(someobject):
 someobject. #here i expect IDE to know which class it is and autocompletion and other IDE functional will be available
 dorender()

In my case the IDE has no way to know what goes in there. How I tell it by using comments?

In case of PHP i've used something like this

/**
 * @param MyClass myobject
**/
function render($myobject){
  //by typing $myobject-> IDE already knows, that it should use $myobject as MyClass objec
  dorender()
}

Another use case is when I know, that in some case some proxy will return some object IDE has no chance to know, but I know for sure and want to specify this by comment, to help myself in future and to help IDE.

In PHP while using PhpStorm I would do following:

$myobject=some_proxy()
/**
 * @var $my_object MyClass
**/
// in following code IDE will use $myobject as MyClass

How can I achieve same behavior with PyCharm specificaly and python documentators in common?

Tigra
  • 2,611
  • 20
  • 22
  • Found an answer, here at stackoverflow :D Exactly what I need. http://stackoverflow.com/questions/6318814/how-can-i-tell-pycharm-what-type-a-parameter-is-expected-to-be – Tigra Apr 17 '12 at 17:03

2 Answers2

1

Use a docstring:

def render(someobject):
    """ This methods renders some object. """
    dorender()

More information can be found here http://en.wikipedia.org/wiki/Docstring#Python

You can use introspection to find out about an object's methods, attributes, etc:

dir(object)
help(object)

Auto completion and IDE integration based on introspection is possible, there's a more detailed information available on the Python.org website: http://wiki.python.org/moin/IntegratedDevelopmentEnvironments

To find out what someobject is, you can use type or isinstance, see the following pseudo code:

type(someobject)
if isinstance(someobject, ClassA):
    print "someobject is a ClassA variable"
cfedermann
  • 3,286
  • 19
  • 24
  • But yet again, this is tip just for me. IDE still does not know what type of object it is. For example i want to do(inside function) someobject.(start typing, want to see relevant methods) – Tigra Apr 17 '12 at 16:43
  • IDE integration with code auto completion depends on your IDE, see my updated answer regarding this. – cfedermann Apr 17 '12 at 16:47
  • And agian, help, dir - Is just for me. It does not enable introspection for IDE. Please, read the question more carefully. I know about docsrting, but maybe i am missing the point, but I dont see the way to specify to IDE (not to myself and to doc-builders, that use docstring) to which class belongs someobject. So I could do refactoring later, to use "find usages", to use autocompletion. Anything that could be done, only when MyClass is explicitly specified – Tigra Apr 17 '12 at 16:54
0

I am not sure if it will work in PyCharm (I don't use it) - but for Python 3 function annotations were added.

You can put those into the definition of a function:

def render(someobject :ObjectClass):
    dorender()

Maybe PyCharm will use this to define which methods are available on someobject.

EDIT:

After a small test I guess this should work (which is pretty awesome).

BergmannF
  • 9,727
  • 3
  • 37
  • 37
  • Thank, but it does not work with python 2.x . In PHP phpdoc is working standart, all IDE's support it's annotations. phpstorm, netbeans, eclipse and many others. Looks like python just has not such standart :( – Tigra Apr 17 '12 at 16:59
  • At least I know of no convention in docstrings that mention object-classes - I guess to allow static interpretation of the code function annotations were added in Python 3. – BergmannF Apr 17 '12 at 17:01