|
| 1 | +# Python typing update |
| 2 | + |
| 3 | +Small tool to update Python typing syntax. |
| 4 | +It uses |
| 5 | +- [python-reorder-imports][pri] |
| 6 | +- [pyupgrade][pyu] |
| 7 | +- [isort][isort] |
| 8 | +- [autoflake][autoflake] |
| 9 | +- [black][black] |
| 10 | +- git |
| 11 | + |
| 12 | +to try and update the typing syntax the best it can. |
| 13 | + |
| 14 | +**Important** |
| 15 | +Since every project uses a different formatting style, |
| 16 | +always check `git diff` before committing any changes! |
| 17 | +Since this tool uses [pyupgrade][pyu], it's best used for |
| 18 | +projects that use it already. |
| 19 | + |
| 20 | + |
| 21 | +## Limitations |
| 22 | +Due to the way the tool works, it will reorder the imports multiple times. |
| 23 | +By default the tool tries to detect if a comment was moved |
| 24 | +and revert all changes to the file. This can be overwritten by using `--force`. |
| 25 | + |
| 26 | + |
| 27 | +## How it works |
| 28 | +1. Run [python-reorder-import][pri] to add |
| 29 | + `from __future__ import annotations` to each file. |
| 30 | +2. Run [pyupgrade][pyu] to use generic aliases ([PEP 585][PEP585]) |
| 31 | + and alternative union syntax ([PEP 604][PEP604]) where possible. |
| 32 | +3. Run [autoflake][autoflake] to check if any typing import is now |
| 33 | + unused. If not, revert changes with `git restore`. |
| 34 | +4. Remove unused imports with [autoflake][autoflake]. |
| 35 | +5. Run [isort][isort] to try to restore the previous formatting. |
| 36 | +6. Optional: Run [black][black] |
| 37 | +7. Check `git diff` for modified comments. |
| 38 | + If one is detected, revert changes and print file name. |
| 39 | + Can be overwritten with `--force`. |
| 40 | + |
| 41 | + |
| 42 | +## Setup pre-commit |
| 43 | + |
| 44 | +Add this to the `.pre-commit-config.yaml` file |
| 45 | + |
| 46 | +```yaml |
| 47 | +repos: |
| 48 | + - repo: https://github.com/cdce8p/python-typing-update |
| 49 | + rev: <insert current tag here!> |
| 50 | + hooks: |
| 51 | + - id: python-typing-update |
| 52 | + stages: [manual] |
| 53 | +``` |
| 54 | +
|
| 55 | +Run with |
| 56 | +```bash |
| 57 | +pre-commit run --hook-stage manual python-typing-update --all-files |
| 58 | +``` |
| 59 | + |
| 60 | +## Configuration |
| 61 | + |
| 62 | +**`--verbose`** |
| 63 | +Always print verbose logging. |
| 64 | + |
| 65 | +**`--full-reorder`** |
| 66 | +Use additional options from [python-reorder-imports][pri] to rewrite |
| 67 | +- `--py38-plus` (default): Imports from `mypy_extensions` and `typing_extensions` when possible. |
| 68 | +- `--py39-plus`: Rewrite [PEP 585][PEP585] typing imports. Additionally `typing.Hashable` and `typing.Sized` will also be replace by their `collections.abc` equivalents. |
| 69 | + |
| 70 | +**`--black`** |
| 71 | +Run `black` formatting after updates. |
| 72 | + |
| 73 | +**`--check`** |
| 74 | +Check if files would be modified. Return with exitcode `1` or `0` if not. Useful for CI runs. |
| 75 | + |
| 76 | +**`--force`** |
| 77 | +Don't revert changes if a modified comment is detected. |
| 78 | +Check `git diff` before committing! |
| 79 | + |
| 80 | +**`--py39-plus`** |
| 81 | +Set the minimum Python syntax to **3.9**. (Default: **3.8**) |
| 82 | + |
| 83 | +**`--py310-plus`** |
| 84 | +Set the minimum Python syntax to **3.10**. (Default: **3.10**) |
| 85 | + |
| 86 | + |
| 87 | +## License |
| 88 | +This Project is licensed under the MIT license. |
| 89 | +See [LICENSE](LICENSE) for the full license text. |
| 90 | + |
| 91 | + |
| 92 | +[pri]: https://github.com/asottile/reorder_python_imports |
| 93 | +[pyu]: https://github.com/asottile/pyupgrade |
| 94 | +[isort]: https://github.com/PyCQA/isort |
| 95 | +[autoflake]: https://github.com/myint/autoflake |
| 96 | +[black]: https://github.com/psf/black |
| 97 | +[PEP585]: https://www.python.org/dev/peps/pep-0585/ |
| 98 | +[PEP604]: https://www.python.org/dev/peps/pep-0604/ |
0 commit comments