PyCharm autocomplete fails so show all options - python

I have recently gotten into python and began using PyCharm, mainly for the code completion feature, as it helps me familiarize with new packages and libraries.
While working with discord.py, I have discovered that the autocompletion feature does not show all possible options.
To show an example, this code is valid and can be executed:
However, if I attempt to use autocomplete to fill out line 11, neither user or name is offered.
Example 2
Example 3
Oddly enough, the autocomplete does work for a large portion of the package though, as shown here:
I have struggled for a long time trying to figure out why this happens. I have made sure I'm using the correct interpreter (discord.py is shown in the installed packages) and I have enabled "Collect run-time types information for code insight" under Python Debugger in Settings, as I have read that this might help.
I'll be thankful for any insight into this.

discord.Client uses __getattr__ to handle attribute lookup for the user attribute dynamically. __getattr__ is fundamentally incompatible with the kind of static analysis PyCharm uses for autocompletion.
I'd probably just live with PyCharm not being able to find this attribute. If you want to do something about it anyway, writing type stubs might help (or they might go out of sync with the implementation and cause more problems), or you could see if discord.py would accept a pull request to use properties instead of __getattr__/__setattr__ for the attribute forwarding they're doing here.

Related

What should I do to Pycharm code suggestion?

enter image description here
I'm a newbie in Python. I use Pychram to code. When I type 'pri' in Win 10, Pycharm suggests 'print'. But in Ubuntu 20, Pycharm doesn't do like that. I already set in Code Completion but it does not work.What can I do?
My English is bad, sorry for that.
#Gareth_Latty says the following in another similar post.
"
As Python is a dynamically typed language, you need to ensure it can work out what type things are, and inspect on the libraries on your system correctly. Try to make sure it's obvious what type the object is in your code.
One good way as of PyCharm 2.7 (back when versions were numbers) is to enable runtime type detection - PyCharm hooks into your program while it runs (while debugging), and checks the types of variables as they are used.
You can enable this by going to settings, going to the "Build, Execution, Deployment" section and then the "Python Debugger" subsection and enabling "Collect run-time types information for code insight"."
Please make sure to research and look at other posts. Thank You!
Make sure you check out this website and This one

What's a more efficient way to browse module functions, object methods, etc than just help(), or dir()?

As I explore new modules, I find it very inefficient to iteratively check help() and dir(), and that dir() doesn't even differentiate between classes, methods and class variables. I know that I can check the source code but that's hardly practical. Is there any Sublime plugin that allows you to see these things in a tree like view, with inline help that automatically pulls from help(), and that uses some visual language to differentiate these kinds of things?
Thanks!
For Sublime Text, check out the Anaconda plugin (not related to the Anaconda Python distribution). Make sure you read all the docs and configure it properly, but once it's going the autocomplete and code intelligence features can be really helpful.
I still highly recommend becoming familiar with the Python docs, as there is a lot in there that no IDE feature can replicate.

Pycharm autocomplete not working for some packages

I never really used pycharm before, but have used other JetBrains products and I expect the autocomplete to work.
When used on modules likes tkinter, after writing tk. I'll get the autocomplete, with methods like tk.Tk(). However, when used on another module (also included in python by default), ctypes, I don't get that kind of autocomplete.
If I start writing windll, I'll get an autocomplete for it, but won't get one after that, so if I write windll.user32, which is a perfectly valid code that runs just fine, I have no way of knowing whether user32 exists or not, ctrl+space shows nothing.
The variable is then successfully created, but using myVar. shows nothing. I'm using anaconda (but also tried on default python) and have the interpreter setup just fine.
Am I missing something?
PyCharm uses static analysis to provide completions, inspections, code insight features and so on.
Static analysis means reading project files and extracting knowledge from expected definitions of classes, function, attributes.
Due to dynamic nature of Python, some of these members could be declared dynamically via assignments, functions with side-effects, etc.
This is a possible reason why there could be no completion in some cases.

Why autocompletion options in Spyder 3.1 are not fully working in the Editor?

Running on Mac Sierra, the autocompletion in Spyder (from Anaconda distribution), seems quite erratic. When used from the Ipython console, works as expected. However, when used from the editor (which is my main way of writing), is erratic. The autocompletion works (i.e. when pressing TAB a little box appears showing options) for some modules, such as pandas or matplotlib. So writing 'pd.' and hitting TAB, gets the box with options as expected. However, this does not happen with many other objects: for example, after defining a dataframe named 'df', typing 'df.' TAB shows nothing. In the Ipython console, 'df.' TAB would show the available procedures for that dataframe, such as groupby, and also its columns, etc..
So the question is threefold. First, is there any particular configuration that should be enabled to get this to work? I don't think so, given some time spent googling, but just wanna make sure. Second, could someone state what is the official word on what works and what doesn't in terms of autocompletion (e.g. what particular modules do work from the editor, and which ones doesn't?). Finally, what are the technical aspects of the differences between the editor and the Ipython console in the performance of the autocompletion with Spyder? I read something about Jedi vs. PsychoPy modules, so got curious (however, please keep in mind that although I have scientific experience, I am relatively new to computation, so please keep it reasonably simple for an educated but not expert person).
UPDATE: As a side question, it would be great to know why is the autocompletion better in Rodeo (another IDE). It is more new, has way fewer overall options than Spyder, but the autocompletion works perfectly in the editor.
(Spyder developer here)
My answers:
is there any particular configuration that should be enabled to get this to work?
In Spyder 3.1 we added the numpydoc library to improve completions of some objects (like Matplotlib figures and NumPy arrays). If Dataframe completions are not working for you (they are for me), please open an issue in our issue tracker on Github to track and solve this problem.
could someone state what is the official word on what works and what doesn't in terms of autocompletion (e.g. what particular modules do work from the editor, and which ones doesn't?)
The most difficult part is getting completions of definitions when an object is generated by functions or methods developed in C/C++/Fortran and not in Python. I mean, things like
import numpy as np
a = np.array([])
a.<TAB>
As I said, this should be working now for arrays, figures and dataframes, but it doesn't work for all libraries (and most scientific Python libraries are created in C/C++/Fortran and wrapped in Python for speed).
The problem is that the completion libraries we use (Rope and Jedi) can't deal with this case very well because array (for example) can't be introspected in a static way (i.e. without running code involving it). So we have to resort to tricks like analyzing array's docstring to see its return type and introspect that instead.
what are the technical aspects of the differences between the editor and the Ipython console in the performance of the autocompletion with Spyder?
The most important difference is that in the IPython console you have to run your code before getting completions about it. For example, please run this in a fresh IPython console
In [1]: import pandas as pd
...: df = pd.Da<Tab>
and you will see that it won't return you any completions for Da (when it obviously should return Dataframe).
But, after evaluation, it is quite simple to get completions. You can simply run
dir(pd)
to get them (that's what IPython essentially does internally).
On the other hand, Spyder's Editor doesn't have a console to run code into, so it has to get completions by running static analysis tools in your code (like Jedi and Rope). As I said, they introspect your code without running it. While they work very well for pure Python code, they have the problems I described above for compiled libraries.
And trying to evaluate the code you have in the Editor to get completions is usually not a good idea because:
It is not necessarily valid Python code all the time. For example, suppose you left an unclosed parenthesis somewhere, but you want to get completions at some other point. That should work without problems, right?
It could involve a very costly computation (e.g. loading a huge CSV in a Dataframe), so evaluating it every time to get completions (and that's a must because your code is different every time you ask for completions) could consume all your RAM in a blink.
it would be great to know why is the autocompletion better in Rodeo (another IDE)
Last time I checked (a couple of years ago), Rodeo evaluated your code to get completions. However, we'll take a look at what they are doing now to see if we can improve our completion machinery.
Autocompletion works correctly if there are NO white spaces in the project working directory path.
Autocomplete was not working for me at all.
So, I tried Tools -> Reset Sypder to factory defaults and it worked.

Python Code Completion for C++ Libraries (Using Pycharm, VTK)

I'm trying to get back into coding for some math/physics experimentations and found VTK as a powerful tool using python. So I installed Python(x,y) and Pycharm Community edition. But I cannot get the Code Completion for VTK to work. I know this question has been posted quite a lot of times, but I couldn't find any concrete answer.
Here's what I know so far:
In order for Code Completion to work Pycharm constructs Skeletons. (Basically Python files with empty Classes/Methods that match the C++ API and can then be used like any other Python file for code completion.)
If I locate these files they don't appear to be complete and look something like this:
If this is indeed the skeleton (the file is called vtkRenderingPython.py) then shouldn't there be empty function declarations?
The result is that I get code completion for the classnames, but not the functions. For a library this huge that's rather annoying. Is there an easy way to get this working, or is this just a limitation I have to live with? Is there maybe a way to get complete Skeletons and replace the ones I have here? Am I missing the point entirely?
After another couple of hours I tried my luck with the PyDev extension for Eclipse. I didn't think that would work, but to my surprise it did! No settings necessary, it just worked out of the box.
The only drawback is that inherited methods are not shown in code completion. The base class is shown in the documentation window though so you can get the available functions by creating a temporary object of the base class and scrolling through the code completion there.

Categories