Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get PyCharm to auto-complete code in methods?

When I'm using a 3rd party l ibrary such as boto, PyCharm seems to be able to auto-complete quite nicely

enter image description here

However, as soon as I define a function of my own, auto-complete breaks down inside that function. I understand why, since I can't give the function any type information about its arguments, so it can't guess how to auto-complete. Is there a way around this issue?

Edit

I tried using the docstring (for Python 2), but still no auto-complete

def delete_oldest_backups(conn, backups_to_keep, backup_description):
    """
    delete_oldest_backups(EC2Connection, int, string)
    """

(Also tried boto.ec2.connection.EC2Connection instead of just EC2Connection)

like image 716
ripper234 Avatar asked Mar 15 '11 08:03

ripper234


People also ask

How do I turn on autocomplete in Python?

Enter: enter edit mode. Shift+Enter: run cell, select below. Shift+Tab: signature autocompletion.

Does PyCharm have code completion?

PyCharm provides smart code completion, code inspections, on-the-fly error highlighting and quick-fixes, along with automated code refactorings and rich navigation capabilities.

How do I turn off autofill in PyCharm?

Go to File > Settings (or Ctrl + Alt + S ) > [IDE Settings] > Editor > Code Completion. The "Autopopup code completion" setting will determine if the popup opens automatically. Below it, the "Insert selected variant by typing dot, space, etc." is likely the setting you want to turn off.


2 Answers

You can use type hints: http://www.jetbrains.com/pycharm/webhelp/type-hinting-in-pycharm.html

 def some_method(self, conn):
   """
   @type conn: EC2Connection
   """
   conn.<autocomplete>
like image 71
Cyberax Avatar answered Oct 07 '22 20:10

Cyberax


You can specify the type information about the parameters of the function using Python 3 parameter and return value annotations. If you're using Python 2, you can also specify information in the function's docstring. PyCharm understands the format used by docstrings of binary modules in the standard library, for example:

"""
foo(int, string) -> list

Returns the list of something
"""
like image 25
yole Avatar answered Oct 07 '22 21:10

yole