PyCharm 2017.2 Help

Examples of Python-Specific Intention Actions

Converting comments

For comment-based type hints, PyCharm 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:

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

Converting formatting style

PyCharm provides an intention that converts .format() calls and printf-style formatting to f-string literals. This intention is called Convert to f-string literal, and works as follows:

BeforeAfter
var = 'hello %s!' % 'John'
var = f'hello {"John"}!'
var = 'hello {}!'.format('John')
var = f'hello {"John"}!'

Converting collections to tuples and vice versa

Python sets, lists and tuples are widely used and often mixed. PyCharm suggests few intention actions that convert between () and []:

/help/img/idea/2017.2/py_convert_tuple_intention.png
Last modified: 9 August 2017