pyupgrade
A tool to automatically upgrade syntax for newer versions.
What it is and what it does
pyupgrade is a command-line tool and pre-commit hook that scans Python source files and automatically rewrites code to use modern syntax patterns. It handles a wide range of modernizations: converting old-style set and dict construction to comprehensions, replacing printf-style string formatting with `.format()` calls, removing unnecessary `u''` prefixes, simplifying `super()` calls, removing Python 2 compatibility code and six library usage, and eliminating deprecated unittest method aliases. The tool is driven by command-line flags that control which Python version's features to target, allowing gradual modernization of codebases.
It runs as a standalone tool on individual files or directories, or integrates into pre-commit hooks to enforce modernization on every commit. The single runtime dependency is tokenize-rt, which handles Python tokenization. The tool requires Python 3.10 or later to run, though it can modernize code written for older Python versions by removing version-specific compatibility patterns.
Use it for:
- Automatically upgrade a legacy codebase from Python 2 to Python 3 by removing six imports and compatibility shims
- Enforce consistent modern syntax in CI/CD pipelines via pre-commit hooks to catch outdated patterns before merge
- Batch-modernize string formatting from printf-style to `.format()` across thousands of lines of code
- Remove deprecated unittest method aliases (e.g., `failUnlessEqual` → `assertEqual`) from test suites
- Simplify class definitions by removing unnecessary `object` inheritance and rewriting metaclass declarations
Worth the install?
AI-flagged interpretation of the facts on this page — verify before relying
Automatically rewrites Python source code to use newer syntax patterns, removing deprecated constructs and modernizing code style across set literals, comprehensions, string formatting, and class definitions.
Yes. pyupgrade is actively maintained, has no known vulnerabilities, installs with minimal friction, and solves a real problem for teams modernizing Python codebases. Its permissive MIT license and broad adoption (top 5000 PyPI packages) make it a low-risk addition to development workflows, especially when integrated as a pre-commit hook to enforce syntax consistency automatically.
Install
pyupgrade on PyPI
pip
pip install pyupgradeuv
uv add pyupgradepoetry
poetry add pyupgradeInstalling pyupgrade
Before you install
Low friction install with a single runtime dependency (tokenize-rt). Active maintenance with recent releases; last commit 2026-08-13 and 4113 repository stars indicate steady use and support.
License in practice
MIT license permits unrestricted use, modification, and distribution in both open-source and commercial projects with minimal restrictions.
Quickstart
pip install pyupgrade
# Run on a single file
pyupgrade myfile.py
# Or add to .pre-commit-config.yaml:
# - repo: https://github.com/asottile/pyupgrade
# rev: v3.21.2
# hooks:
# - id: pyupgrade
Requires Python 3.10 or later to run the tool itself; the code being upgraded can target older Python versions via command-line flags.
Verify before relying
- Whether all syntax rewrites are safe to apply automatically without manual review in large codebases
- Performance characteristics when processing very large Python files or projects
- Specific Python version targets supported by command-line flags beyond those shown in examples
Package facts
| License | MIT (permissive) |
| Python support | supports the current Python release (>=3.10) |
| Install friction | low — pure-Python wheel |
| Runtime dependencies | 1 — tokenize-rt |
| Maintenance | actively maintained — 268 days since the last release |
| Last repo commit | |
| First released | |
| Downloads | 1,860,922/month — #3,483 on PyPI (30-day window, as of 2026-08-14) |
| Known vulnerabilities | none known (OSV.dev, checked 2026-08-14) |
Evidence: pyupgrade-3.21.2-py2.py3-none-any.whl
Tags
More Quality Assurance packages
Coverage.py measures which lines of Python code…
permissive · top 1,000 on PyPI
ruffRuff is a Python linter and code formatter…
permissive · top 1,000 on PyPI
pexpectPexpect spawns and controls interactive console…
permissive · top 1,000 on PyPI
blackBlack reformats Python source code to a…
permissive · top 1,000 on PyPI
pytest-xdistpytest-xdist distributes pytest tests across…
permissive · top 1,000 on PyPI
cfn-lintValidates AWS CloudFormation templates in YAML…
permissive · top 1,000 on PyPI
absolufy-importsAutomatically converts relative imports to…
permissive · top 15,000 on PyPI
reorder-python-importsAutomatically reorders Python imports into a…
permissive · top 15,000 on PyPI
to-requirements.txtAutomatically synchronizes your project's…
permissive · top 15,000 on PyPI
google-pastaParse Python source code into an AST, modify…
permissive · top 1,000 on PyPI
yesqayesqa automatically removes unnecessary `#…
permissive · top 15,000 on PyPI
add-trailing-commaAutomatically adds trailing commas to…
permissive · top 15,000 on PyPI
darkerDarker reformats Python source code in Git…
permissive · top 15,000 on PyPI
django-upgradeAutomatically modernizes Django project code by…
permissive · top 15,000 on PyPI
updateProvides a check() function to detect available…
permissive · top 15,000 on PyPI
unidiff2Parses unified diff format into structured…
permissive · top 15,000 on PyPI