Reputation: 1386
I'm trying to use type hinting in python 2.7 as described here.
I have a function that expects a callable (a function) with a specific signature as a parameter and I can't figure out how to annotate it.
I've tried
def set_function(self, function):
# type: ((int) -> None) -> None
But PyCharm shows an expected ')' and unexpected tokens errors
I can't seem to find any documentation for this...
Upvotes: 5
Views: 3291
Reputation: 64288
The correct way to document a callable within Pycharm (or within any other tool that understands PEP 484 type hints) is like so:
from typing import Callable
def set_function(self, function):
# type: (Callable[[int], None]) -> None
...
Since you're using Python 2, you'll need to install the typing
module from PyPi, if you haven't already. (typing
was added to Python's standard library in 3.5, the module on PyPi is a backport).
You can find more information on using the typing module in Python's documentation, and within the documentation for mypy.
(If you're not aware, mypy is a command line tool that also understands PEP 484 type hints and will also statically analyze and typecheck your code. It is an independent effort from Pycharm's built-in type checker. Since both Pycharm and mypy use PEP 484 types, mypy's documentation is often a good place to start looking to learn more about using type hints.)
Upvotes: 7