Neovim for Beginners — Python Code Refactoring

Use Neovim and Refurb to modernize our Python codebase.

alpha2phi
4 min readOct 8, 2022
Neovim for Beginners — Python Code Refactoring

In this article, let’s learn how to refactor and modernize our Python codebase. For general refactoring techniques, do check out the previous Neovim for Beginners — Refactoring article.

This article is part of the Neovim for Beginners series.

The Neovim configuration files are available in this repository.

Getting Started

As the Python programming language evolves over the years, our existing codebase could be obsolete. The newer version of Python could also provide better or more performing APIs that we can leverage.

Refurb is a Python tool for refurbishing and modernizing Python codebases. It can make the codebase more elegant, and modern.

We can install it using pip.

$ pip3 install refurb

E.g., below is a sample output by Refurb.

$ refurb main.py
main.py:3:17 [FURB109]: Use `in (x, y, z)` instead of `in [x, y, z]`
main.py:4:5 [FURB101]: Use `y = Path(x).read_text()` instead of `with open(x, ...) as f: y = f.read()`
main.py:10:40 [FURB102]: Replace `x.startswith(y) or x.startswith(z)` with `x.startswith((y, z))`
main.py:16:9…

--

--

alpha2phi

Software engineer, Data Science and ML practitioner.