19

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)

ripper234
  • 222,824
  • 274
  • 634
  • 905
  • possible duplicate of [Pydev Code Completion for everything](http://stackoverflow.com/questions/6218778/pydev-code-completion-for-everything) – Jonathan Livni Jul 23 '13 at 10:32

4 Answers4

14

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>
Cyberax
  • 1,667
  • 16
  • 18
5

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
"""
yole
  • 92,896
  • 20
  • 260
  • 197
  • 1
    I tried this (see edited question), but still no auto-complete. – ripper234 Mar 15 '11 at 13:37
  • naither works for me: def get_instance_by_name(connection, name): """ get_instance_by_name(connection, name) -> boto.ec2.instance Returns instance object with specified name """ and i don't have autocmpliton avaiable for instance object – Maciek Sawicki Dec 28 '11 at 13:50
1

You can install the library via pyCharm "package manager".

Go to Settings -> Project Interpreter -> Python Interpreters

Package list

And in the Packages list, click on install and search for the library you want to install

Install package

Once installed, auto-complete will be available on editor.

Autocomple inside function

Hope this is what you are looking for.

AntonyMCs
  • 699
  • 6
  • 7
1

In order for PyCharm to recognize an instance of an object and retrieve all its methods, we have to use the following statements. But I think that both is a terrible way of wasting programming and run time.

assert isinstance(instanceX, ClassOfInstanceX)  
instanceX.{#list of method/properties appears}

Alternatively, you can also use the class name will recall the method or property everytime you want to invoke it and pass in the instance to the self parameter. But this is too verbose, for my liking, esp for nested class

ClassOfInstanceX.{#list of method/properties appears}
     # then you will have...
ClassOfInstance.method(instanceX, args...)