Pydev Code Completion for everything

守給你的承諾、 提交于 2019-11-27 17:33:09

问题


In many cases (such as function parameters) Pydev doesn't statically know the type of a variable. Therefore code completion (after . or when using ctrl+space) doesn't work.

In most cases, you know what type will be in run-time as you are designing the software. Is there a way to hint Pydev to code completing it correctly?

I guess this may require a specific Pydev feature, or perhaps even a new Python PIP.

This is actually seems to be a generic problem with all dynamic languages...

UPDATE:
Perhaps an example is in place for clarification:

def some_func(a_list, an_object):
    a_list.app        # Here I would not get code completion for append

An example of something that could work, if Pydev (or a PIP) would support it:

from someobj import SomeObject
def some_func(a_list, an_object):
    # typecast: a_list=list
    # typecast: an_object=SomeObject
    a_list.app        # Now code completion would show append

I'm not endorsing this specific method - it's just an example of a system that could work. Again, of course this should not be mandatory - but sometimes the lack of the possibility to hint the type is annoying.


回答1:


[Edit]

Since PyDev 2.8.0, it can use docstrings and comments to discover the type of objects.

See: http://pydev.org/manual_adv_type_hints.html for details on the supported formats.

[Before PyDev 2.8.0]

Previously, it only supported assert isinstance calls (and this still works):

assert isinstance(a_list, list)

PyDev will be able to recognize it and properly provide code-completion for it (note that you can run Python without the assertions later on if you find it's making your code slower: What does Python optimization (-O or PYTHONOPTIMIZE) do? )




回答2:


As of PyDev 2.8.0 can use Sphinx or Epydoc comments for code completion: http://pydev.org/manual_adv_type_hints.html




回答3:


If you use PyCharm, you can pick either epydoc or sphinx docstring style and specify types of parameters and function return values according to that style (further discussion)




回答4:


What I usually do to circumvent this.

def func(my_list_param):
    my_list_param = []
    my_list_param.appe # at this stage I would get code completion.

Just remember to delete the variable initialization when testing or committing. Oh and btw, the response marked as an answer doesn't seem to work for me.




回答5:


If you are using Python >= 3.5 in your code you can specify parameters type in function declarations (26.1. typing — Support for type hints), so you code could be:

from someobj import SomeObject

def some_func(a_list: list, an_object: SomeObject):
    a_list.app           # Code completion works
    an_object.a_method() # Code completion works

This type of syntax, has no effects on execution and does not make any check on the real type passed o the function, but make your code more readable and makes the code completion work.



来源:https://stackoverflow.com/questions/6218778/pydev-code-completion-for-everything

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!