Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Type hinting in Eclipse with PyDev

Tags:

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 
like image 545
Andrew Avatar asked Apr 24 '10 14:04

Andrew


People also ask

What is PyDev Eclipse?

PyDev is a Python IDE for Eclipse, which may be used in Python, Jython and IronPython development. It comes with many goodies such as: Django integration. Code completion. Code completion with auto import.

Is type hinting Pythonic?

Python has always been a dynamically typed language, which means you don't have to specify data types for variables and function return values. PEP 484 introduced type hints — a way to make Python feel statically typed.

What is PyDev used for?

PyDev is a third-party plug-in for Eclipse. It is an Integrated Development Environment (IDE) used for programming in Python supporting code refactoring, graphical debugging, code analysis among other features.

How do I run code in PyDev?

Go to the menu: Alt + R + S + The number of the Run you wish (It can be Python, Jython, unit-test, etc). Note: if you were using unit-tests, you could use: Ctrl+F9 to run the unit-tests from the module (and even selecting which tests should be run -- and if Shift is pressed it's launched in debug mode).


2 Answers

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

like image 155
cryo Avatar answered Sep 25 '22 15:09

cryo


Present Python 2/3

For local scope variables and function parameters PyDev has this:

assert isinstance(obj, MyClass)
obj. # here hint will work

Though I guess it's a not documented feature. Here's PyDev's official page for type hints and couple of excerpts which illustrate Sphinx syntax.

class Example:

  def param(self, a):
    ''':type a: MyClass'''

  def var(self, iterable):
    for a in iterable: #: :type a: AnotherClass
        pass

Unfortunately, neither works for class members.

Since, PyDev 4 there's also something that can resemble PEP-484 (see below).

class LatestExample:

  def listcase(self, param):
    ''':type param: list[str]'''

  def dictcase(self, param):
    ':type param: dict[str, MyClass]'

Future Python 3

Take a look at answer by @slushy. No doubt this is the future. But for the time being PyDev supports neither function annotations, PEP-3107, nor new PEP-484 stuff @slushy demonstrates. PEP-484 is coming in Python 3.5 in some limited form, and in 3.6 in final. Here's BDFL's PyCon 2015 presentation for type hints and PEP-484.

like image 35
saaj Avatar answered Sep 23 '22 15:09

saaj