Type declaration for variables in rst Permanently deleted user 创建于 2011年07月04日 08:49 Hello, is there any way to declare type for local, global variable or object attribute in rst?Something like:var = config.variable#: :type string:
Hello Fedor,
Yes.
var = config.variable
""" :type string """
--
Dmitry Jemerov
Development Lead
JetBrains, Inc.
http://www.jetbrains.com/
"Develop with Pleasure!"
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
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
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?
Attachment(s):
ac.png
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
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.
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.
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
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 ?
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