skillfed

pyupgrade

A tool to automatically upgrade syntax for newer versions.

pyupgrade v3.21.2 1.9M downloads/30d#3,483 on PyPI4,113
Permissive license MIT Active released

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 pyupgrade

uv

uv add pyupgrade

poetry

poetry add pyupgrade

Installing 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

Programming Language :: Python :: 3Programming Language :: Python :: 3 :: OnlyProgramming Language :: Python :: Implementation :: CPythonProgramming Language :: Python :: Implementation :: PyPy

Tags

python syntax modernizerautomatic code upgraderdeprecated syntax fixerpython modernization toolcode style updaterpython linter upgradesyntax rewriter
code-modernizationpre-commit-hooksyntax-rewriter

More Quality Assurance packages