A framework for managing and running multi-language pre-commit hooks that check code before each Git commit.
asottile/pyupgrade
A command-line tool and pre-commit hook that automatically rewrites Python source to newer syntax for a target Python version.
- Stars
- 4,126
- Forks
- 229
- Open issues
- 21
- Last commit
- 3 days ago
What is it?
pyupgrade rewrites Python source files in place so they use syntax and idioms from newer Python versions. It handles changes such as converting old-style string formatting to f-strings or str.format, replacing six compatibility calls with native equivalents, and simplifying set and dict comprehensions. Version-specific rewrites are enabled with flags such as --py36-plus or --py310-plus, so the output matches the minimum Python version a project targets.
The tool can be installed with pip and run from the command line on one or more files. It can also be wired into pre-commit through a provided hook, so the rewrites run automatically on staged files before each commit. Some rewrites, such as those affecting annotations, only apply when a file imports from __future__ import annotations, and a flag keeps runtime typing unchanged when needed.
Key features
- 01Rewrites old string formatting to f-strings, str.format, and simplified literals.
- 02Removes six and py2/py3-compat code, including imports and version checks.
- 03Targets specific Python versions through flags like --py36-plus through --py315-plus.
- 04Modernizes typing annotations such as PEP 585 and PEP 604 forms when enabled.
- 05Runs as a pre-commit hook on staged files.
Use cases
- Raising the minimum Python version of an older codebase without hand-editing every file.
- Dropping Python 2 and six compatibility code after a migration.
- Enforcing modern syntax automatically through pre-commit in a team repository.
Quick start
- 1
Install pyupgrade with pip
pip install pyupgrade - 2
Or add it as a pre-commit hook in .pre-commit-config.yaml using the sample config from the README
Things to know
- Many rewrites only apply when you pass a target-version flag such as --py36-plus or --py39-plus.
In collections
Topics
FAQ
- What is pyupgrade?
- A command-line tool and pre-commit hook that automatically rewrites Python source to newer syntax for a target Python version. pyupgrade rewrites Python source files in place so they use syntax and idioms from newer Python versions. It handles changes such as converting old-style string formatting to f-strings or str.format, replacing six compatibility calls with native equivalents, and simplifying set and dict comprehensions. Version-specific rewrites are enabled with flags such as --py36-plus or --py310-plus, so the output matches the minimum Python version a project targets.
- Is pyupgrade free to use? What license does it have?
- pyupgrade is open source under the MIT license. Check the license terms before using it in commercial products.
- Is pyupgrade still maintained?
- Actively maintained. The last commit to pyupgrade was on Nov 4, 2026.
- What can I use pyupgrade for?
- Raising the minimum Python version of an older codebase without hand-editing every file. Dropping Python 2 and six compatibility code after a migration. Enforcing modern syntax automatically through pre-commit in a team repository.
- How do I install pyupgrade?
- Install pyupgrade with pip: pip install pyupgrade Or add it as a pre-commit hook in .pre-commit-config.yaml using the sample config from the README
- Where can I get pyupgrade?
- The source code and installation instructions are on GitHub: https://github.com/asottile/pyupgrade
Summary generated by AI from the project's README. GitHub stats refresh daily.
Related projects
Black is an opinionated, deterministic Python code formatter that rewrites files in place with minimal diffs.
A Python utility and library that automatically sorts and groups your import statements.
Command-line tool and flake8 plugin that enforces shared config settings across multiple projects, and can fix config files automatically.
A Python tool and pre-commit hook that automatically adds trailing commas to multi-line calls, literals, and definitions.
A CLI tool that searches, lints, and rewrites code by matching abstract syntax tree patterns instead of plain text.