IntelliJ IDEA 2019.1 Help

Type hinting in IntelliJ IDEA

IntelliJ IDEA provides various means to assist inspecting and checking the types of the objects in your script. IntelliJ IDEA supports type hinting in function annotations and type comments using the typing module and the format defined by PEP 484.

Adding type hints

Although IntelliJ IDEA supports all methods for adding types supported in PEP 484, using type hints through intention actions is the most convenient way. Depending on the interpreter you use, the type is added as an annotation (Python 3) or as a comment (Python 2).

To add a type hint, follow these steps:

  1. Select a code element.

  2. Press Alt+Enter.

  3. Select Add type hint for ....

  4. Press Enter to complete the action or edit the type if appropriate.

Example

Intention Action

Resulting Code for comments (Python 2)

Resulting Code for annotations (Python 3)

Variables

example of adding a type hint for a variable
example of adding a type hint for a variable (Python 2)
example of adding a type hint for a variable (Python 3)

Functions

example of adding a type hint for a function
example of adding a type hint for a function (Python 2)
example of adding a type hint for a function (Python 3)

Class attributes

example of adding a type hint for a class attribute
example of adding a type hint for a class attribute (Python 2)
example of adding a type hint for a class attribute (Python 3)

You can also use Python stubs or comments to specify the types of variables, functions, and class fields.

Specifying types by using comments

Use a # type: comment to specify the types of local variables and attributes:

Type hinting attributes

Converting comments

For comment-based type hints, IntelliJ IDEA suggests an intention action that allows you to convert comment-based type hint to a variable annotation. This intention has the name Convert to variable annotation, and works as follows:

Before

After

from typing import List, Optional xs = [] # type: List[Optional[str]]

from typing import List, Optional xs: List[Optional[str]] = []

Type hints validation

Any time you're applying type hints, IntelliJ IDEA checks if the type is used correctly. If there is a usage error, the corresponding warning is shown and the recommended action is suggested. Below are the validation examples.

Validation error

Suggested action

Duplication of type declaration.

incorrect type hint

Remove either of the type declarations.

Number of arguments in the type declaration differs from the number of function arguments.

too many arguments

Adjust the number of the arguments.

Missing brackets.

missing bracket

Add the required brackets where appropriate.

Type comments with unpacking do not match the corresponding targets.

incorrect type for unpacked variables

Check the target format and modify the type comment accordingly.

Incorrect syntax of Callable parameters.

Incorrect Callable format

Use the suggested format and add the required brackets to wrap Callable parameters.

Python stubs

IntelliJ IDEA supports Python stub files with the .pyi extension. These files allow you to specify the type hints using Python 3 syntax for both Python 2 and 3.

The stub files are, but you must specify the extension .pyi explicitly.

IntelliJ IDEA shows an asterisk in the left gutter for those code elements that have stubs:

elements with stubs

Clicking the asterisk results in jumping to the corresponding stub:

jumping to the corresponding stub

With the PEP-561 support, you can install stubs as a package for a Python interpreter.

To install a stub package

  1. Follow the procedure described in To install a package. If needed, click Manage repositories to add a repository where your stub packages reside.

  2. Stub packages have a predefined name format, so type "-stubs" in the search field to discover them.

    install a stub package

  3. Complete installation by clicking Install Package.

Once added, the type hints recorded in the stub files become available in your code. For example, the following type hint for some_func_2:

type hints in the stub

becomes available in the following Python code

type hints

If you're using a package for which a stub analog is detected, the following message will be shown:

The stub package inspection

You can install the stub package, ignore this message and continue working with the currently installed package, or even disable the corresponding inspection in the project Settings/Preferences.

Using Typeshed

Typeshed is a set of files with type annotations for the standard Java library and various packages. Typeshed stubs provide definitions for Java classes, functions, and modules defined with type hints. IntelliJ IDEA uses this information for better code completion, inspections, and other code insight features.

IntelliJ IDEA is switching to Typeshed, the common repository for Java stubs. The Typeshed stubs bundled with IntelliJ IDEA are shown in the project view under the node External Libraries | <Python interpreter> | Typeshed Stubs. Note that IntelliJ IDEA currently uses only a few of the bundled stubs (i.e. builtins.pyi, typing.pyi, and several others).

To override the bundled Typeshed repository with your own version, follow these steps:

  1. Copy some or all the stubs into a directory in your project.

  2. Mark a directory as a source root by choosing Mark Directory as | Sources Root from the context menu of the directory.

The Python skeletons repository https://github.com/JetBrains/python-skeletons is now deprecated.

Last modified: 23 April 2019

See Also