Update Python typing syntax
Project description
Python typing update
Tool to update Python typing syntax. It uses token analysis and
to try and update the typing syntax the best it can.
Important
Every project uses a different formatting style,
so always check git diff
before comitting any changes!
Since this tool uses pyupgrade, it's best used for
projects that use it already.
Limitations
Due to the way the tool works, it will reorder the imports multiple times.
By default the tool tries to detect if a comment was moved
and revert all changes to the file. This can be overwritten by using --force
.
Currently, it's not possible to update aliases with a different name. In particular, these need to be updated manually:
Old typing name | New |
---|---|
Deque |
collections.deque |
DefaultDict |
collections.defaultdict |
AbstractSet |
collections.abc.Set |
ContextManager |
contextlib.AbstractContextManager |
AsyncContextMananger |
contextlib.AbstractAsyncContextManager |
How it works
- Run python-reorder-import to add
from __future__ import annotations
to each file. - Run pyupgrade to use generic aliases (PEP 585) and alternative union syntax (PEP 604) where possible.
- Run autoflake to check if any typing import is now
unused. If not, revert changes with
git restore
. - Remove unused imports with autoflake.
- Run isort to try to restore the previous formatting.
- Optional: Run black. (Requires
black
to be added asadditional_dependency
)
OR: Run ruff. (Requiresruff
to be added asadditional_dependency
) - Check
git diff
for modified comments. If one is detected, revert changes and print file name. Can be overwritten with--force
.
Setup pre-commit
Add this to the .pre-commit-config.yaml
file
repos:
- repo: https://github.com/cdce8p/python-typing-update
rev: <insert current tag here!>
hooks:
- id: python-typing-update
stages: [manual]
Run with
pre-commit run --hook-stage manual python-typing-update --all-files
Configuration
--verbose
Always print verbose logging.
--limit
Max number of files that should be changed. No performance improvements,
since the limit is only applied after all files have been processed.
--concurrent-files
Number of files to process concurrently during initial load.
--full-reorder
Use additional options from python-reorder-imports to rewrite
--py38-plus
(default): Imports frommypy_extensions
andtyping_extensions
when possible.--py39-plus
: Rewrite PEP 585 typing imports. Additionallytyping.Hashable
andtyping.Sized
will also be replaced by theircollections.abc
equivalents.
--keep-updates
Keep updates even if no import was removed. Use with caution, might result in more errors.
--black
Run black
formatting after updates.
To use it, add black
as additional_dependency
in your .pre-commit-config.yaml
.
additional_dependencies:
- black==<insert current version here!>
--ruff
Run ruff check --fix
and ruff format
after updates.
To use it, add ruff
as additional_dependency
in your .pre-commit-config.yaml
.
additional_dependencies:
- ruff==<insert current version here!>
--disable-committed-check
Don't abort with uncommitted changes. Don't use it in production!
Risk of losing uncommitted changes.
Different mode options
--check
Check if files would be modified. Return with exitcode 1
or 0
if not. Useful for CI runs.
--force
Don't revert changes if a modified comment is detected.
Check git diff
before committing!
--only-force
Only update files which are likely to require extra work.
Check git diff
before committing!
Python version options
--py37-plus
Set the minimum Python syntax version to 3.7. (Default: 3.9)
--py38-plus
Set the minimum Python syntax version to 3.8. (Default: 3.9)
--py39-plus
Set the minimum Python syntax version to 3.9. This is the default.
--py310-plus
Set the minimum Python syntax version to 3.10. (Default: 3.9)
--py311-plus
Set the minimum Python syntax version to 3.11. (Default: 3.9)
--py312-plus
Set the minimum Python syntax version to 3.12. (Default: 3.9)
--py313-plus
Set the minimum Python syntax version to 3.13. (Default: 3.9)
License
This Project is licensed under the MIT license. See LICENSE for the full license text.
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
Hashes for python_typing_update-0.7.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | c65ca0905fdc5802b4c0d1853b76aaf94a0165fb9dcc8774e264707863810fd6 |
|
MD5 | 9fc463059de31f9e55e852b427116e1a |
|
BLAKE2b-256 | 931d83cddeb65c202d2c237753d0f61b06087ef19261e5f98acd82234dfa03d9 |
Hashes for python_typing_update-0.7.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9b7bb7453a098756581944b089e510b1e12ab48aa178a71c1dcd90b529c0c899 |
|
MD5 | 0445458b02a803edc1cc86ae57188336 |
|
BLAKE2b-256 | 707866439840c75a39e5547831fff4e7f2ae3475bfb29c23f45379abe0cc82b6 |