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:
Before | After |
---|---|
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:
Before | After |
---|---|
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 []
:
Last modified: 23 July 2018