pyupgrade

3.21.2 · active · verified Sat Apr 11

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.

Warnings

Install

Quickstart

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.

# my_module.py
import os
from typing import List

def old_function(name):
    print("Hello, %s!" % name)

class OldClass(object):
    def __init__(self, value):
        self.value = value

def process_items(items: List[int]) -> List[int]:
    return [x * 2 for x in items]

# Run pyupgrade from the command line:
# pyupgrade --py38-plus my_module.py
# This will update the file in place to, for example, use f-strings and remove object inheritance.

view raw JSON →