Skip to content
| Marketplace
Sign in
Visual Studio Code>Programming Languages>PyrefactNew to Visual Studio Code? Get it now.
Pyrefact

Pyrefact

Olle Lindgren

|
7,299 installs
| (0) | Free
Refactoring python code with Pyrefact
Installation
Launch VS Code Quick Open (Ctrl+P), paste the following command, and press enter.
Copied to clipboard
More Info

Pyrefact

This extension adds Pyrefact as a formatter in VSCode.

Pyrefact finds and solves many common anti-patterns in python code.

Supported commands

Pyrefact supports three commands:

  • Format Document With... -> Pyrefact
  • Format Selection With... -> Pyrefact
  • Pyrefact: Restart Server

Pyrefact can also be configured as the default formatter and used with the regular hotkeys, described in the VSCode formatting documentation.

Examples

Below follows a number of examples to illustrate the sort of things pyrefact can do.

Converting loops into constant expressions

q = 3
w = list()
for a in range(-1, 10):
    for k in range(-1, 1):
        w.append(a ** 2 + q + k ** 2)
y = sum(w)
print(y)
q = 3
y = 22 * q + 583
print(y)

Replacing a loop filling up a set with a set comprehension

x = set()
for i in range(100):
    if i > 3:
        if i % 8 == 0:
            x.add(i ** 2)
x = {i**2 for i in range(100) if i > 3 and i % 8 == 0}

Replacing a dict with a defaultdict where appropriate

d = {}
for x in range(10):
    if x in d:
        d[x].append(f(x))
    else:
        d[x] = [f(x)]
d = collections.defaultdict(list)
for x in range(10):
    d[x].append(f(x))

Converting math loops into np.matmul()

import numpy as np

i, j, k = 10, 11, 12

a = np.random.random((i, j))
b = np.random.random((j, k))

u = np.array(
    [
        [
            np.sum(
                a__ * b__
                for a__, b__ in zip(a_, b_)
            )
            for a_ in a
        ]
        for b_ in b.T
    ]
).T

print(u)
import numpy as np

i, j, k = 10, 11, 12

a = np.random.random((i, j))
b = np.random.random((j, k))

u = np.matmul(a, b)

print(u)

De-indenting nested code

def f(x) -> int:
    if x == 1:
        x = 2
    else:
        if x == 3:
            x = 7
        else:
            if x != 8:
                x = 99
            else:
                if x >= 912:
                    x = -2
                elif x ** x > x ** 3:
                    x = -1
                else:
                    x = 14

    return x

def _f(x) -> int:
    if x == 1:
        return 2
    if x == 3:
        return 7
    if x == 8:
        return 99
    if x >= 912:
        return -2
    if x**x > x**3:
        return -1

    return 14

Removing dead code

import random
import sys

def f(x: int) -> int:
    import heapq
    y = e = 112

    if x >= 2:
        d = 12

    if []:
        x *= 99

    if x == 3:
        y = x ** 13
        return 8
    else:
        return 19
while False:
    sys.exit(0)
def f(x: int) -> int:
    if x == 3:
        return 8

    return 19

A complete list of the things pyrefact can do can be found on the pyrefact github page.

  • Contact us
  • Jobs
  • Privacy
  • Manage cookies
  • Terms of use
  • Trademarks
© 2025 Microsoft