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 git+https://github.com/frostming/fix-future-annotations.git
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.3.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5603d1d7a58546a603957bc136c4440bd3815c229c6979583c142e32bc215b94 |
|
MD5 | 888053732b97d534112d9cbf5f0367b1 |
|
BLAKE2b-256 | a47cc2b43ad18075a5f1bfd46fee71bd9ada099fdefa99de71044469c08f0c31 |
Close
Hashes for fix_future_annotations-0.3.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6fc5a16f156dfb6499a383ad884e2bc0ab0d0625a81b06817f423f34059d5525 |
|
MD5 | 6564777e258ebba84c07d3052c1a34ee |
|
BLAKE2b-256 | 7b601aec5c5ab047411e99b52c376aa6a9388fde0dbb7219ef3ea587bb9e6066 |