Add import responsible for postponed evaluation of annotations to python files
Project description
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_importshook or other hooks that may complain about it.
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file import_future_annotations-1.0.0.tar.gz.
File metadata
- Download URL: import_future_annotations-1.0.0.tar.gz
- Upload date:
- Size: 3.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
42bcc591fcf419c8f6a27b0cd08d07b59be8ac764007f031cfd4dfbf4b7ee827
|
|
| MD5 |
a9576ff8658acd92be7cc3f997557265
|
|
| BLAKE2b-256 |
6ca22b20b0c5f6f3e049746027063cf72c2eb684e0c675d6cda0aea2aca62d64
|
File details
Details for the file import_future_annotations-1.0.0-py3-none-any.whl.
File metadata
- Download URL: import_future_annotations-1.0.0-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4265e4492f7f3a6881e654870d713560a59b33e696b14e6f52c39f2052ae23d6
|
|
| MD5 |
248351f59a1120a97638960e9052feef
|
|
| BLAKE2b-256 |
2b20867f6b4eb5c755830c3fa4c3976a443b6d71b202a4202612c6d6c4754adb
|