Rewrite Optional[Type]
to Type | None
Has Fix
Description
PEP 604 recommends that Type | None
is preferred over Optional[Type]
for Python 3.10+.
This rule performs such rewriting. Note Optional[$T]
alone is interpreted as subscripting expression instead of generic type, we need to use pattern object to disambiguate it with more context code.
YAML
yaml
id: optional-to-none-union
language: python
rule:
pattern:
context: 'a: Optional[$T]'
selector: generic_type
fix: $T | None
Example
py
def a(arg: Optional[Int]): pass
Diff
py
def a(arg: Optional[Int]): pass
def a(arg: Int | None): pass