Skip to main content

import-future-annotations

Tool that checks presence of import from __future__ import annotations in python files and adds it if it's missing.

Installation

Requires Python >=3.8.

pip install import-future-annotations

Usage

import-future-annotations takes filenames as positional arguments.

Additional options:

  • --check-only: Don't modify files, only check. If script is applicable to any given file, the exit status code will be 1.
  • --allow-empty: Add import to empty python files aswell.

As a pre-commit hook

See pre-commit for instructions

Sample .pre-commit-config.yaml:

-   repo: https://github.com/mbroton/import-future-annotations
    rev: v0.1.0
    hooks:
    -   id: import-future-annotations

How does it work?

Files without docstrings

file.py before:

import os
import sys

...

Run pre-commit hook or execute command:

> import-future-annotations file.py
Adding annotations import to file.py

file.py after:

+ from __future__ import annotations
import os
import sys

Files with docstrings

file.py before:

"""
This is a module docstring.
"""
import os
import sys

...

Run pre-commit hook or execute command:

> import-future-annotations file.py
Adding annotations import to file.py

file.py after:

"""
This is a module docstring.
"""
+ from __future__ import annotations
import os
import sys

The import is placed after the module docstring (if present) or at the beginning of the file (if no docstring).

Files with syntax errors

Files containing syntax errors are automatically skipped with a warning message:

> import-future-annotations broken_file.py
Skipping broken_file.py: file contains syntax errors

Notes

  • It won't add any blank lines, so I suggest to use/place it before reorder_python_imports hook or other hooks that may complain about it.

Metadata

Release files for import-future-annotations 1.0.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 import-future-annotations 1.0.0
File Size Uploaded
import_future_annotations-1.0.0.tar.gz 3.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for import-future-annotations 1.0.0
File Interpreter ABI Platform
import_future_annotations-1.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 8.5 kB

Release files / import_future_annotations-1.0.0.tar.gz

Download URL import_future_annotations-1.0.0.tar.gz
Size 3.8 kB
Tags Source
SHA-256 checksum
How to use checksums
42bcc591fcf419c8f6a27b0cd08d07b59be8ac764007f031cfd4dfbf4b7ee827
BLAKE2b-256 checksum
How to use checksums
6ca22b20b0c5f6f3e049746027063cf72c2eb684e0c675d6cda0aea2aca62d64
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.7

Release files / import_future_annotations-1.0.0-py3-none-any.whl

Download URL import_future_annotations-1.0.0-py3-none-any.whl
Size 4.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4265e4492f7f3a6881e654870d713560a59b33e696b14e6f52c39f2052ae23d6
BLAKE2b-256 checksum
How to use checksums
2b20867f6b4eb5c755830c3fa4c3976a443b6d71b202a4202612c6d6c4754adb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.12.7

Release history Release notifications | RSS feed

This release

1.0.0 This release

2 release files

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