Type hinting in Eclipse with PyDev

Andrew picture Andrew · Apr 24, 2010 · Viewed 13.9k times · Source

I'm studying Python, after a lot of PHP experience, and it would be handy to have type-hinting in Python. Looks like Eclipse with PyDev doesn't support this. Any suggestions?

For example, I want my IDE to show function docstrings and types, when I use it, like:

def f(x: int) -> int:
    r"""Adds 3 to x"""
    return x + 3

f(# and now IDE shows everything about types 

Answer

cryo picture cryo · Apr 24, 2010

Python is a dynamically-typed language, where variable types don't need to be declared. You can add information about the expected types intended to be passed to the function in docstrings though, e.g.

def f(x):
    """
    @x: int
    Adds 3 to x
    returns an int
    """
    return x + 3

But in this case, the function is so simple it doesn't need any type info in my opinion and just documenting what it does is often preferred in python over documenting strict types.

pydev does support docstring (but not types) completion and catches many errors, so long as you're opening the python files as part of a project and not opening them separately by drag-dropping them into Eclipse.

You need to add the folders containing python files by right clicking on the project root, selecting the Properties menu item and selecting PyDev - PYTHONPATH on the list on the left, and clicking Add source folder for all the folders with python files. Note that pydev can usually find modules in any subdirectories if there's a __init__.py in them, so you often only need to add the root python source folder.

After that, you access the tooltips by typing ctrl+space before typing the (, and auto-fill out suggested function arguments by typing ctrl+space after typing (.

See also the pydev manual at http://pydev.org/manual_101_root.html