Type declaration for variables in rst

Hello, is there any way to declare type for local, global variable or object attribute in rst?

Something like:

var = config.variable

#: :type string:

10 comments
Comment actions Permalink

Hello Fedor,

Yes.

var = config.variable

""" :type string """

Hello, is there any way to declare type for local, global variable or

object attribute in rst?

Something like:

var = config.variable

#: :type string:

--

Dmitry Jemerov

Development Lead

JetBrains, Inc.

http://www.jetbrains.com/

"Develop with Pleasure!"

0
Comment actions Permalink

Thanks for your answer but it unfortunately don't works.

In code:

class Foo(object):

    def __getattr__(self, item):

        return item

var = Foo().variable

""" :type string """

var.  # No suggestions

I use PyCharm 1.5.2 and docstring format for project settings is reStructuredText.
0
Comment actions Permalink

Hi Fedor,

there's typing error in this comment. Actually Dmitry meant:

var = Foo().variable
""":type : str """

or """@type : string """ for epytext comments.

But I'm not sure it's the right way to handle variable type.

If you check type explicitly:

if isinstance(var, str):
  var.    # <-- here PyCharm should suggest you right autocompletion

0
Comment actions Permalink

Sometimes I have to initialize the variables with None, and even if I specify the type, the autocomplete is showing just None. Is it possible to also show the type in the given example?

ac.png



Attachment(s):
ac.png
0
Comment actions Permalink

Thanks, Catherine, it great! Especially that PyCharm understands situation with isinstance.

I think that it is can be useful in some specific cases, like LocalProxy

objects in werkzeug, that can't be checked with isinstance.

But unfortunately sphinx can not understand PyCharm syntax for type declaration, i created issue PY-4084

0
Comment actions Permalink

Hi,

in your example try to type:

var = None
""" :type: str """

var.     # <-- here Pycharm'll suggest you autocompletion for string methods.

PyCharm does not really change the type of var, but adds string methods for var autocompletion list.

0
Comment actions Permalink

in your example try to type:

var = None
""" :type: str """

var.     # <-- here Pycharm'll suggest you autocompletion for string methods.

PyCharm does not really change the type of var, but adds string methods for var autocompletion list.

Then let me rephrase: is it possible to also show the type set by the user in the comment ? So I can see a list of variable names like this:

var1 None / str # or any other visual cue that the second information is set by the user

var2 None / list

And for functions like this:

def f(a, b=None):

    if b is None:

        b = []

    ...

it would be also great to allow me to hint the parameter autocompletion with the fact that b should be a list.

0
Comment actions Permalink

Thanks, I understood.

We are now discussing this feature. Possibly, it'll be added in Pycharm 2.0

Also you can create an issue for this feature here -- http://youtrack.jetbrains.net/issues/PY

0
Comment actions Permalink

I have created the feature request here: http://youtrack.jetbrains.net/issue/PY-4086

Is it possible to get a (nightly?) build with this issue fixed http://youtrack.jetbrains.net/issue/PY-4084 ?

0
Comment actions Permalink

Thanks for the issue!

> Is it possible to get a (nightly?) build with this issue fixed http://youtrack.jetbrains.net/issue/PY-4084 ?

Unfortunately you have to wait for Pycharm 1.5.3

0

Please sign in to leave a comment.