How does type inference work?

Hi, i tried to find documentation on this, but couldn't. Are there any ways to provide extra information about the return-type of functions through docstrings or annotations, so that PyCharm will be able to correctly get it? I'm working with Python 2.5 (so no function argument-annotations available).

6 comments
Comment actions Permalink

Hello Preben,

PyCharm understands docstring syntax as used in the standard library binary

modules. For example:

""" hasattr(object o, string name) -> bool"""

The signature line must be the first non-empty line of a docstring, and can

be followed by any other text.

Hi, i tried to find documentation on this, but couldn't. Are there any

ways to provide extra information about the return-type of functions

through docstrings or annotations, so that PyCharm will be able to

correctly get it? I'm working with Python 2.5 (so no function

argument-annotations available).

--

Dmitry Jemerov

Development Lead

JetBrains, Inc.

http://www.jetbrains.com/

"Develop with Pleasure!"

0
Comment actions Permalink

Спасибо!

That's already useful. I tried to look in the issue tracker, but I'm not really good at navigating those things, so: Is there any plans to extend this functionality? (In my opinion, that would bring PyCharm up to IDEA-usefullness). I wanted to ask before I filed feature requests.

What I miss in particular is for example on:

""" hasattr(object o, string name) -> bool"""

To have autocomplete in the method for the incoming arguments. In this example "name" would be recognized as a string inside the method. As far as I can see, this is not implemented at this time.

Also, I am having problems declaring anything other than the basic types as return type. This would be practical:

""" hasattr(object o, string name) -> myapp.MyType"""

Also, collections hold the same kind of objects in most cases, so the following would be neat:

""" hasattr(object o, string name) -> list[string]"""

Regards,
Preben
0
Comment actions Permalink

That's useful for binary modules, but that's not the convention I would expect anyone to use for most of their Python code, as Python documenting tools can get argument names and default values (but not types) from the definition.  Unfortunately there isn't a universal standard on this in the Python world; epytext used to be the going thing, but now Sphinx seems to be popular and I've been recommending reST field lists: http://sphinx.pocoo.org/markup/desc.html

See also http://www.python.org/dev/peps/pep-0257/

I posted a similar question about javascript conventions, but haven't learned much since then: http://devnet.jetbrains.net/thread/292042

0
Comment actions Permalink

Hello Kevin,

We plan to support epydoc and Sphinx in future versions of PyCharm. We need

to parse the docstrings for binary modules in the standard library in any

case, so why not let people use the same convention for letting PyCharm understand

their own code better.

That's useful for binary modules, but that's not the convention I

would expect anyone to use for most of their Python code, as Python

documenting tools can get argument names and default values (but not

types) from the definition.  Unfortunately there isn't a universal

standard on this in the Python world; epytext used to be the going

thing, but now Sphinx seems to be popular and I've been recommending

reST field lists: http://sphinx.pocoo.org/markup/desc.html

--

Dmitry Jemerov

Development Lead

JetBrains, Inc.

http://www.jetbrains.com/

"Develop with Pleasure!"

0
Comment actions Permalink

Hello Preben,

I don't think we currently have YouTrack issues for any of those problems.

You're welcome to file them.

That's already useful. I tried to look in the issue tracker, but I'm

not really good at navigating those things, so: Is there any plans to

extend this functionality? (In my opinion, that would bring PyCharm up

to IDEA-usefullness). I wanted to ask before I filed feature requests.

What I miss in particular is for example on:

""" hasattr(object o, string name) -> bool"""

To have autocomplete in the method for the incoming arguments. In this

example "name" would be recognized as a string inside the method. As

far as I can see, this is not implemented at this time.

Also, I am having problems declaring anything other than the basic

types as return type. This would be practical: """ hasattr(object o,

string name) -> myapp.MyType"""

Also, collections hold the same kind of objects in most cases, so the

following would be neat: """ hasattr(object o, string name) ->

list[string]"""

--

Dmitry Jemerov

Development Lead

JetBrains, Inc.

http://www.jetbrains.com/

"Develop with Pleasure!"

0
Comment actions Permalink

Thanks for the input. Sphinx definitely looks like something I should have a deeper look at as soon as possible.

0

Please sign in to leave a comment.