Skip to main content

A tool for refurbish and modernize Python codebases

Project description

Refurb

A tool for refurbishing and modernizing Python codebases.

Example

# main.py

with open("file.txt") as f:
    contents = f.read()

Running:

$ refurb main.py
tmp.py:3:1 [FURB101]: Use `y = Path(x).read_text()` instead of `with open(x, ...) as f: y = f.read()`

Installing

Before installing, it is recommended that you setup a virtual environment.

$ pip3 install refurb
$ refurb file.py

Installing (For Development)

$ git clone https://github.com/dosisod/refurb
$ cd refurb
$ make install
$ make install-local

Tests can be ran all at once with make, or you can run each tool on its own using make black, make flake8, and so on.

Unit tests can be ran with pytest or make test.

Since the end-to-end (e2e) tests are slow, they are not ran when running make test. You will need to run make test-e2e to run them.

Explainations For Checks

You can use refurb --explain FURB123, where FURB123 is the error code you are trying to look up. For example:

$ refurb --explain FURB123
Don't cast a variable or literal if it is already of that type. For
example:

Bad:

```
name = str("bob")
num = int(123)
```

Good:

```
name = "bob"
num = 123

Ignoring Certain Checks

Use --ignore 123 to ignore check 123. The error code can be in the form FURB123 or 123.

Configuring Refurb

In addition to the command line arguments, you can also add your settings in the pyproject.toml file. For example, the following command line arguments:

refurb file.py --ignore 100 --load some_dir

Corresponds to the following in your pyproject.toml file:

[tool.refurb]
ignore = [100]
load = ["some_dir"]

And all you need to run is refurb file.py!

Note that ignore and load are the only supported options in the config file, since all other command line options are one-offs, and don't make sense to be in the config file.

Writing Your Own Check

If you want to extend Refurb with your own custom checks, you can easily do so with the refurb gen command. Note that this command uses the fzf fuzzy-finder for the getting user input, so you will need to install it before continuing.

This is the basic overview of creating a new check using the refurb gen command:

  1. First select the node type you want to accept
  2. Then type in the path of where you want to put your check file
  3. Add your code to the generated file

To get an idea of what you need to do to get your check working the way you want it, use the --debug flag to see the AST representation of a given file.

To run, use refurb file.py --load your.path.here

Note that when using --load, you need to use dots in your argument, just like importing a normal python module.

Plugins (Coming Soon)

Work is underway to make Refurb plugin-extensible.

Why Does This Exist?

I love doing code reviews: I like taking something and making it better, faster, more elegant, and so on. Lots of static analysis tools already exist, but none of them seem to be focused on making code more elegant, more readable, more modern. That is what Refurb tries to do.

What Refurb Is Not

Refurb is not a linter or a type checker. It is not meant as a first-line of defense for finding bugs, it is meant for making nice code look even better.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

refurb-0.2.3.tar.gz (35.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

refurb-0.2.3-py3-none-any.whl (49.7 kB view details)

Uploaded Python 3

File details

Details for the file refurb-0.2.3.tar.gz.

File metadata

  • Download URL: refurb-0.2.3.tar.gz
  • Upload date:
  • Size: 35.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.1 CPython/3.8.10 Linux/5.15.0-1020-azure

File hashes

Hashes for refurb-0.2.3.tar.gz
Algorithm Hash digest
SHA256 9295df12f25009071908eca85931c3d68ec6788ed4ce9eb40e0e43616bad85b4
MD5 61e40ad0b526eb7c31f21a940b80a064
BLAKE2b-256 e11c26e130244361c2cae58fa294eb816bc990cbbb4b089e844e9800c435438b

See more details on using hashes here.

File details

Details for the file refurb-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: refurb-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 49.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.1 CPython/3.8.10 Linux/5.15.0-1020-azure

File hashes

Hashes for refurb-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 a0013988f3f642b9ad3b7bcca040dd93cc85ade53783b73c40196a909d35d371
MD5 1bce8b925987ccd3ffffd446646ccc3d
BLAKE2b-256 1b9a14888f28727ebb4f750be0f9bd3b6e22c6666b8cd1f5bbe96ff242d698a8

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page