pyupgrade is an active Python tool (version 3.21.2) designed to automatically upgrade code syntax for newer language versions. It streamlines the process of adopting modern Python features, improving code readability and efficiency. It's primarily used as a command-line utility or integrated into development workflows via pre-commit hooks, with new versions released continuously to support the latest Python syntax.
pip install pyupgradeNo compatibility data collected yet for this library.
To quickly upgrade a Python file, first save your code, then run `pyupgrade` from your terminal, specifying the target Python version using a `--pyN-plus` flag. This example updates `my_module.py` to Python 3.8+ syntax, converting old string formatting, removing redundant `(object)` inheritance, and updating type hints.
Use `find . -name '*.py' -exec pyupgrade --py3N-plus {} +` for recursive application, or integrate it as a `pre-commit` hook (e.g., `pre-commit run pyupgrade --all-files`).Add `--exit-zero-even-if-changed` to your pyupgrade command or pre-commit hook configuration.
Always explicitly provide the `--pyN-plus` flag corresponding to the minimum Python version you intend to support (e.g., `--py38-plus` for Python 3.8 and newer).
Review your `ruff` configuration (e.g., in `pyproject.toml`) for `select = ['UP']` to avoid duplicate tooling. `ruff --fix` often handles these upgrades.
If you encounter issues with type checkers, use the `--keep-runtime-typing` flag to prevent these specific typing rewrites. Ensure `typing.Type` is always used with a type argument (e.g., `Type[int]`) if that's the intended meaning.
Always run your test suite and a linter (e.g., `ruff`, `flake8`) after applying `pyupgrade` to catch any regressions or new issues.
Correct the existing syntax errors in the specified file before running `pyupgrade`.
Check the `pyupgrade` documentation or run `pyupgrade --help` to verify the correct arguments and their syntax, then correct the argument.
Install `pyupgrade` using `pip install pyupgrade`. If using `pre-commit`, ensure your `.pre-commit-config.yaml` is correctly configured with `repo: https://github.com/asottile/pyupgrade`.
Ensure that the user executing `pyupgrade` has write permissions for the target file(s), or change the file permissions using `chmod`.