A CLI and pre-commit hook to upgrade the typing annotations syntax to PEP 585 and PEP 604
Project description
fix-future-annotations
A CLI and pre-commit hook to upgrade the typing annotations syntax to PEP 585 and PEP 604.
Upgrade Details
PEP 585 – Type Hinting Generics In Standard Collections
Old | New |
---|---|
typing.Dict[str, int]
List[str]
|
dict[str, int]
list[str]
|
PEP 604 – Allow writing union types as X | Y
Old | New |
---|---|
typing.Union[str, int]
Optional[str]
|
str | int
str | None
|
PEP 563 – Postponed Evaluation of Annotations
Old | New |
---|---|
def create() -> "Foo": pass
|
def create() -> Foo: pass
|
Import aliases handling
Old | New |
---|---|
import typing as t
from typing import Tuple as MyTuple
def foo() -> MyTuple[str, t.Optional[int]]:
pass
|
from __future__ import annotations
import typing as t
def foo() -> tuple[str, int | None]:
pass
|
Full example
Old | New |
---|---|
from typing import Union, Dict, Optional, Tuple
# non-annotation usage will be preserved
MyType = Union[str, int]
def foo() -> Tuple[Dict[str, int], Optional[str]]:
...
|
from __future__ import annotations
from typing import Union
# non-annotation usage will be preserved
MyType = Union[str, int]
def foo() -> tuple[dict[str, int], str | None]:
...
|
Unused import names will be removed, and if from __future__ import annotations
is not found in the script, it will be automatically added if the new syntax is being used.
Use as pre-commit hook
Add the following to your .pre-commit-config.yaml
:
repos:
- repo: https://github.com/frostming/fix-future-annotations
rev: x.y.z # a released version tag
hooks:
- id: fix-future-annotations
Use as command line tool
python3 -m pip install -U fix-future-annotations
fix-future-annotations my_script.py
License
This work is distributed under MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Close
Hashes for fix-future-annotations-0.4.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6df59117017b5e59f7ea024bf66006e5f2477bad623940cb171e493d0638b8d5 |
|
MD5 | 3b393b84c1729274ac5eef9814ce9a47 |
|
BLAKE2b-256 | 280764ce02d46cb177abd1c6846772ae916870bd9add36c2880cacd2bf746ff8 |
Close
Hashes for fix_future_annotations-0.4.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 341d20a93c6d9608cca645ba414bdae335d23e896e69b272df9e138adc8788cb |
|
MD5 | 7745201c8ec84c808d146ae8e5e3b4eb |
|
BLAKE2b-256 | 1f960046099cab4e6aa2ee989da8e99536ebcec2afb962bf4af32dea2252aeb3 |