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 a command line tool
python3 -m pip install -U fix-future-annotations
fix-future-annotations my_script.py
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
Configurations
fix-future-annotations
can be configured via pyproject.toml
. Here is an example:
[tool.fix_future_annotations]
exclude_files = [ # regex patterns to exclude files
'tests/.*',
'docs/.*',
]
exclude_lines = [ # regex patterns to exclude lines
'# ffa: ignore', # if a line ends with this comment, the whole *block* will be excluded
'class .+\(BaseModel\):' # classes that inherit from `BaseModel` will be excluded
]
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
File details
Details for the file fix-future-annotations-0.5.0.tar.gz
.
File metadata
- Download URL: fix-future-annotations-0.5.0.tar.gz
- Upload date:
- Size: 11.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 666bf5fd09068f3403b34b2dc69844955a4a7bac4df28dd345183bd18172cbfd |
|
MD5 | bee17c0c6042dc2cf4a113eb6f804b7a |
|
BLAKE2b-256 | 1556d5ebafd3f1c46b43d5edd491359a870e224613ada3d1b6358a114d68d59e |
File details
Details for the file fix_future_annotations-0.5.0-py3-none-any.whl
.
File metadata
- Download URL: fix_future_annotations-0.5.0-py3-none-any.whl
- Upload date:
- Size: 10.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.8
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 45b5e73b36c514a23c2fbb2b7ff26448496399bda3b98a7b78a14439639f2e59 |
|
MD5 | 9d52567541f1d8266c1843999296a5d2 |
|
BLAKE2b-256 | 19eb7f2e259f542145d7bc4f4d664b7d9dedfa9da6ac564efeac3c5e6fa2cb33 |