Trick code completion with PyDev like with PDT?

Is there a way to help PyDev code completion by telling it the type of a variable?

With PDT, you can use PHPDoc-like syntax for such purpose:

/* @var $my_var MyClass */
$my_var = myFunction();
// PDT is able to figure out that $my_var is a MyClass object.

But till now, I cannot figure out how to do the same in python.


Asked by: Maddie136 | Posted: 06-12-2021






Answer 1

The assert trick does not seem to work for me with PyDev 2.2.2 ; it is still supposed to ?

However another trick I tried and that work is the following :

class Foo(object):
    def __init__(self, bar):
       self.bar = bar
       # Tricking PyDev
       if (not self.bar):
          self.bar = Bar()
          raise Exception("Bar should not be null")

In all cases, it looks pretty hacky, and I would love a cleaner way to do things (documentation, annotation, whatever)

Answered by: Gianna800 | Posted: 07-01-2022



Answer 2

Actually, you can if you do an assert isinstance()

E.g.:

a = function()
assert isinstance(a, MyClass)
a. <- would get the proper completions

Note that Pydev does analyze the return of the functions, so, it's possible that it knows that on a number of cases.

Also, that shouldn't have runtime penalties if you use python -O (which will remove the asserts)

Answered by: Arnold934 | Posted: 07-01-2022



Answer 3

Nope (see the docs). It looks like PyDev does completion of imported stuff and language keywords.

It doesn't seem like this would come up a lot though. The variable in question seems like it would only be unknown to pydev if it were passed in as a function argument with no default value.And, if you have a function operating on your own class, it seems like that should be a class member (so autocomplete would already work).

Answered by: Blake360 | Posted: 07-01-2022



Similar questions

How do I add tab completion to the Python shell?

When starting a django application using python manage.py shell, I get an InteractiveConsole shell - I can use tab completion, etc. Python 2.5.1 (r251:54863, Apr 15 2008, 22:57:26) [GCC 4.0.1 (Apple Inc. build 5465)] on darwin Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) When just starting a python interpreter using python


python - File copy completion?

In Linux, how can we know if a file has completed copying before reading it? In Windows, an OSError is raised.


emacs - Python Code Completion

After using C# for long time I finally decided to switch to Python. The question I am facing for the moment has to do about auto-complete. I guess I am spoiled by C# and especially from resharper and I was expecting something similar to exist for Python. My editor of choice is emacs and after doing some research I found autocomplete.pl, yasnippet and rope although it is not clear to me if ...


python - How to show sorted list by overall completion in Pootle

Hi I am new to Django and working on Pootle project. I would like to sort by Overall completion by default in the pootle index page. for example, http://pootle.locamotion.org/ lists the languages and sorted by names. you can click sort buttons to see sort by completion. but I would like to show this list sorted by completion whenever load a page.


Python IDE with auto completion

please advise me some good Python IDE, I was using netbeans but it does not have suitable code completion (when I press "." it gives me methods of all classes of python. It would be nice if netbeans would work as for ex. for PHP.. Thank you.


python - completion of part x using n-1 parts

I've build a small module in python which takes a list of strings/buffers in the same size, and returns a xor string in the same size. Then, using that string, together with n-1 strings, I can complete the missing one. It works great, so my question is: do you know an already made python module for that? is there a way (practically/theory) where i can complete 2 missing stri...


python - Pydev Code Completion for everything

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?


python - Create a vim script for django code completion, but fail, why?

I tried two ways to do code completion, one is OK, the other fails. OK one does like below: $&gt; cd myDjangoProject/ myDjangoProject $&gt; export PYTHONPATH="." myDjangoProject $&gt; DJANGO_SETTINGS_MODULE=settings vim urls.py Then ^x ^o can work well. But this method leads me repeatly do above when edit a file in project. So an idea comes to me, why not c...


emacs python code completion

This question already has answers here:


python code completion in Emacs

I tried make PythonIDE using emacs like in this article http://www.enigmacurry.com/2009/01/21/autocompleteel-python-code-completion-in-emacs/ But emacs said me "auto-complete-mode not enabled". It is possible use emacs for python coding?


Has anyone found a good set of python plugins for vim -- specifically module completion?

I'm looking for a suite of plugins that can help me finally switch over to vim full-time. Right now I'm using Komodo with some good success, but their vim bindings have enough little errors that I'm tired of it. What I do love in Komodo, though, is the code completion. So, here's what I'm looking for (ordered by importance). Code completion, meaning: the ability to code complete modules/functi...


How do I add tab completion to the Python shell?

When starting a django application using python manage.py shell, I get an InteractiveConsole shell - I can use tab completion, etc. Python 2.5.1 (r251:54863, Apr 15 2008, 22:57:26) [GCC 4.0.1 (Apple Inc. build 5465)] on darwin Type "help", "copyright", "credits" or "license" for more information. (InteractiveConsole) When just starting a python interpreter using python


Python tell when an ftp transfer sits on completion

I have to download some files from an FTP server. Seems prosaic enough. However, the way this server behaves is if the file is very large, the connection will just hang when the download ostensibly completes. How can I handle this gracefully using ftplib in python? Sample python code: from ftplib import FTP ... ftp = FTP(host) ftp.login(login, passwd) files=ftp.nlst() ftp.set_debuglevel(2)...


python tab completion in windows

I'm writing a cross-platform shell like program in python and I'd like to add custom tab-completion actions. On Unix systems I can use the built-in readline module and use code like the following to specify a list of possible completions when I hit the TAB key: import readline readline.parse_and_bind( 'tab: complete' ) readline.set_completer( ... ) How can I do this on Windows? I'd like to...


python - IO completion port key confusion

I'm writing an IO completion port based server (source code here) using the Windows DLL API in Python using the ctypes module. But this is a pretty direct usage of the API and this question is directed at those who have a knowledge of IOCP, not Python. As I understand the documentation for...


Getting proper code completion for Python on Vim?

I've gotten omnicompletion with Pysmell to work before, but I can't seem to do it again. I tried following some steps online, but most, if not all, of them are to vague and assume too much that you know what you are doing to some extent. Can someone post a full, step-by-step tutorial on how to get code completion working properly, for complete Vim newbies (for dummies?)?


mercurial - Tab Completion in Python Command Line Interface - how to catch Tab events

I'm writing a little CLI in Python (as an extension to Mercurial) and would like to support tab-completion. Specifically, I would like catch tabs in the prompt and show a list of matching options (just like bash). Example: Enter section name: ext*TAB* extensions extras The problem is I'm not sure how to catch the Tab events. I'm using the ui.prompt() API of Merc...


Code completion in NetBeans' python plugin does not work properly

I am asking on StackOverflow because surely I am doing something completely silly and I hope S.O. might provide me with a quick answer. I've installed the latest stable Python-plugin for NetBeans. It works great, and I tested code completion with various packages such as sys, os and so on. It works beautifully. However, it does not seem to pick up the code completion for the code in my ow...


python - Weird behaviour with optparse and bash tab completion

I am building a script for users new to Linux, so please understand why I am asking this :) My script runs like this: python script.py -f filename.txt I am using the optparse module for this. However, I noticed the following when doing tab completion. The tab completion works when I do: python script.py &lt;tab completion&gt; # Tab completion wo...


python - which inotify event signals the completion of a large file operation?

for large files or slow connections, copying files may take some time. using pyinotify, i have been watching for the IN_CREATE event code. but this seems to occur at the start of a file transfer. i need to know when a file is completely copied - it aint much use if it's only half there. when a file transfer is finished and completed, what inotify event is fired?






Still can't find your answer? Check out these communities...



PySlackers | Full Stack Python | NHS Python | Pythonist Cafe | Hacker Earth | Discord Python



top