Skip to main content

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

OldNew
typing.Dict[str, int]
List[str]
dict[str, int]
list[str]

PEP 604 – Allow writing union types as X | Y

OldNew
typing.Union[str, int]
Optional[str]
str | int
str | None

PEP 563 – Postponed Evaluation of Annotations

OldNew
def create() -> "Foo": pass
def create() -> Foo: pass

Import aliases handling

OldNew
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

OldNew
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.

Release files for fix-future-annotations 0.5.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for fix-future-annotations 0.5.0
File Size Uploaded
fix-future-annotations-0.5.0.tar.gz 11.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for fix-future-annotations 0.5.0
File Interpreter ABI Platform
fix_future_annotations-0.5.0-py3-none-any.whl Python 3 none any Details

Total release size: 21.4 kB

Release files / fix-future-annotations-0.5.0.tar.gz

Download URL fix-future-annotations-0.5.0.tar.gz
Size 11.3 kB
Tags Source
SHA-256 checksum
How to use checksums
666bf5fd09068f3403b34b2dc69844955a4a7bac4df28dd345183bd18172cbfd
BLAKE2b-256 checksum
How to use checksums
1556d5ebafd3f1c46b43d5edd491359a870e224613ada3d1b6358a114d68d59e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.8

Release files / fix_future_annotations-0.5.0-py3-none-any.whl

Download URL fix_future_annotations-0.5.0-py3-none-any.whl
Size 10.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
45b5e73b36c514a23c2fbb2b7ff26448496399bda3b98a7b78a14439639f2e59
BLAKE2b-256 checksum
How to use checksums
19eb7f2e259f542145d7bc4f4d664b7d9dedfa9da6ac564efeac3c5e6fa2cb33
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.8

Release history Release notifications | RSS feed

This release

0.5.0 This release

2 release files

0.4.0

2 release files

0.3.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page