Examples of Python-Specific Intention Actions
In this section:
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"}!' |
Last modified: 26 July 2017