Autocompletion in PyCharm based on type - python

I'm using PyCharm for python coding. The autocompletion in PyCharm is not as good as in IntelliJ (Java). Consider the below code
a = [1,2,3,4]
a.
In this case, after I press the dot, PyCharm gives the full set of autocompletion options. Consider the below case
def func_a(a):
a.
Here, to the function func_a I'm passing a list as an argument. But when I press dot after a, PyCharm doesn't give any autocompletion options. I know this is because Python is dynamically typed language and PyCharm has no idea to determine what type a is.
But is there any way to tell PyCharm the type of a, may be in documentation comment or something like that? So, that PyCharm can give valid autocompletion options?

Yes, Python 3.5 introduces type hinting and Pycharm utilizes that.
Use
def func_name(param: type) -> return_type:
Like
def func_a(a: list):
Note that all type hints are completely optional and ignored by the interpreter. However, Pycharm can potentially help you detect type errors if you use it as a habit.

This might not answer your question but this will be helpful for those who just started using pycharm for Django application.
PyCharm does not give (It underlines some built in functions with red) auto-completion option for Django if you have started project with Pure Python. Pure Python option comes when you click on new project option from file menu or when you run pycharm to start new project. Pure Python is the default selected option on new project page. You should choose Django (the 2nd option) to get auto-completion option in PyCharm.
Hope this would be helpful for others.

Can do : (examples using list):
def func_a(a:list):
do_this()
Or manually check:
def func_a(a):
if isinstance(a,list):
do_this
else:
raise TypeError("expected type of 'list' but got type of '%s'"%type(a).__name__)

Related

How to make my IDE recognize dynamically added functions?

I'm currently developing a dynamic software system in python. It includes the use of dependency injection at many points, which is implemented with dynamic class attributes. My aim is to add a function dynamically to a class (with setattr) an this works quite fine. The only problem I have is that the IDE (in my case it's PyCharm) has no idea about those functions and marks it as "unresolved reference". Although the script runs without errors, it looks not very nice and I want the IDE to support other programmers, that don't know about those functions.
Here is an example of what I mean:
class A:
def __init__(self, func_obj):
setattr(self, 'custom_function', func_obj)
a = A(print) # Unresolved attribute reference 'custom_function' for class 'A' ...
a.custom_function('Hello World!')
As expected, this example prints "Hello World!". But I find it's ugly, that the IDE shows a warning for the last line, where I called "custom_function". And also there is no auto completion if you try to explore the script with duck typing.
So let me come to my question: Is there any way to tell the IDE that there are some dynamically added functions in this class? Maybe there is some way with meta classes or something like that... But I didn't find anything on Google and I have no idea where else to try.
I hope you can help me with that :)
I had a similar request and asked directly to PyCharm bug tracker: PY-28326. I think that their answer applies to your question too:
PyCharm at the moment doesn't support methods dynamically added to classes.

Define an object of a certain class as None

Can an object be set to None when initialized, but still belong to a class?
class Foo:
def do_stuff():
pass
bar_1 = Foo()
bar_2 = None
The specific purpose I have in mind is that I want bar_2 to be available accross different modules, but define it at a later date (for reasons that are irrelevant). It works just fine, when you run the code. My IDE, however, does not know that bar_2 is of type Foo, and hence will not show me do_stuff() as an autocomplete option the way it does for bar_1, for instance.
I understand that python is not a strongly typed language, but I reckon if there was a way to tell python that bar_2 is of type Foo, it would solve the problem of autocomplete not showing up.
If there is another way to make my IDE (PyCharm, specifically) aware of bar_2's class, I'd be happy to know.
If you're using Python 3.5, you can use type hinting. For this specific use, you'd need to put it in a type comment:
bar_2 = None # type: Foo
Earlier versions of Python don't have a builtin way of doing this, though I suppose an IDE that supports both earlier and later versions of the language might allow type comments to effect it's behavior even if the Python version you're using doesn't support the full type hinting semantics.
Try using type hints assuming the type is consistent and you're using python 3.5.
To get the intellisense completion JetBrains has a legacy type hinting system that should work for all versions of python.
when it is supposed to be an instance of foo you can do
assert isinstance(bar_2,Foo)
# anything below this and in scope will autocomplete bar_2 with Foo's methods
In general this is not a very good practice, and should be avoided but it works to enable autocomplete in most IDE's (pycharm included)

Can I update pycharm intellisense for python?

I want to know if it's possible to update Pycharm intellisense for python which doesn't care about capital or normal letters?
It's a pain in the neck to try both each time.
Currently I'm using pycharm 3.0 and python 2.7.4
An example of what I want:
For example I have a model named: 'MyModel'
I use to type first two letters and then ctrl+space to choose from the list. so if I type 'my' instead of 'My', the pycharm says: 'No suggestion'
You want to set the Case sensitive completion to None.
In PyCharm 2016.3 this is in Settings > Editor > General > Code Completion.

Emacs Extension for Python Auto Complete

I'm looking for an extension which will help in Python's auto complete feature in Python.
If I type the following code:
a = [4,5,6]
a.p
Then I expect it would give me a suggestion for pop as it is one of the method of Python list. Is this thing achievable in Emacs?
I tried installing Rope, Rope mode, Ropemacs and Pymacs. The auto-complete suggestion, I get from that aren't the methods of list. But it suggests me something like print etc. Am I doing something wrong ?
Try Jedi.el. AFAIK, it should support your example.
Because Python variables don't have types, it is difficult to know what class of object a variable would contain at a given point in your code. Imagine if your code later did a = 1. Emacs would have to know when the variable a refers to a list and when it refers to a number in order to offer the correct completion. Generally this is not possible without actually running the code.
Works for me with vimrope from https://github.com/python-rope/ropevim/ (now, the official home of all rope projects)

Python Strongly type lists

I am using eclipse for python and I am facing a problem. I have many classes with many properties and want a list of objects from one of my declared classes. The problem is: When I am accessing any item from the list, the IDE does not know its type because in python we do not declare the variable with type, so there is no auto complete and I have to go to the class to copy the attribute name.
To make idea more clear:
class AutomataBranch(object):
def __init__(selfparams):
self.Name="";
self.nodes=[];
class LanguageAutomata(object):
def __init__(selfparams):
self.cfgAutomata=[];#This has AutomaBranch Type
Now in any method in LanguageAutomata class if I wrote:
cfgAutomata. Then it wont give me the Name attribute
Is there any solution for that?
Python is strongly typed and Python lists are too. Your problem come from the fact that Python is dynamically typed. Therefor a var can contain any type, and therefor no IDE can guess what is the type of your parameter, nor give you code completion for the methods.
This is how it is, there is no clean workaround. If it's a problem, then maybe dynamics language is not you predilection tool and you should use something that fit your development style. There are tools for everybody.
8 years later and we actually have a solution in Python 3.6.
PEP484 allows you to annotate your variables primarily for IDEs and linting:
Modifying #Hani's answer:
x : AutomataBranch = self.cfgAutomata[i]
This is now picked up by any good IDE to highlight errors and allow autocomplete.
I think you mean to say "statically typed" instead of "strongly typed." Python is strongly typed. You just don't know what that type is at compile time.
With that said, you really need to abandon the idea that you're going to find any IDEs that work as well for Python as they do for Java or C#. Python's dynamic typing makes this difficult. In fact, I tend to find that powerful IDEs are more of a burden than a help.
I think I found a good managable solution. Actually it is trivial but may help (I used it now).
When I want to access the list then I assign the object which I want to access to a variable ex:
x = AutomataBranch()
x = self.cfgAutomata[i]
The first line is used only to make the IDE knows that x is from AutomatBranch type. After that when I press x then all methods and properties are visualized.
I think it is some how good.

Categories