Skip to content

Commit

Permalink
[Issue #1587] Switch to use ruff instead of flake8 (#1626)
Browse files Browse the repository at this point in the history
## Summary
Fixes #1587

### Time to review: __3 mins__

## Changes proposed
Update our API linting to use ruff instead of flake8

## Context for reviewers
[ruff](https://docs.astral.sh/ruff/) is a linter (and formatter although
we aren't using that yet) that does what flake8 does, but does it faster
and with many more configuration options (not yet used). Conveniently,
it even uses the same rules as flake8, so switching just required
adjusting where the configuration was defined.

Note that https://pypi.org/project/flake8-mypy/ was something we
previously had configured, but has been abandoned for years, and I'm not
quite sure what its purpose was (we use mypy - which handles type
linting anyways?).

A few of the ignore rules were modified as we were either ignoring
things that were removed or that just made sense to no longer ignore.

There are many, many rules that we could add, but I wanted to keep this
PR contained to just porting us over, and not reconfiguring things -
turning on every rule gives 3k+ errors, mostly in our tests for things
we wouldn't care about. We likely will need to have different settings
for tests and non-tests if we want to turn on a lot more.

Additionally, while ruff can be used as a formatter, and replace our
usage of isort & black potentially, that would cause a fair number of
changes at the moment (~25 files, mostly commas, line-length and white
space), and I'll leave that as a later follow-up.
  • Loading branch information
chouinar authored Apr 15, 2024
1 parent b8c7ba0 commit bf0217e
Show file tree
Hide file tree
Showing 6 changed files with 365 additions and 447 deletions.
11 changes: 4 additions & 7 deletions api/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,14 @@ DECODE_LOG := 2>&1 | python3 -u src/logging/util/decodelog.py
SHELL = /bin/bash -o pipefail

# The APP_DIR variable is the path from the root of the repository to this Makefile.
# This variable is used to display errors from Flake8 and MyPy in the 'Files Changed'
# This variable is used to display errors from MyPy in the 'Files Changed'
# section of a pull request. If this is set to the incorrect value, you won't be able
# to see the errors on the correct files in that section
APP_DIR := api
ifdef CI
DOCKER_EXEC_ARGS := -T -e CI -e PYTEST_ADDOPTS="--color=yes"
FLAKE8_FORMAT := '::warning file=$(APP_DIR)/%(path)s,line=%(row)d,col=%(col)d::%(path)s:%(row)d:%(col)d: %(code)s %(text)s'
MYPY_FLAGS := --no-pretty
MYPY_POSTPROC := | perl -pe "s/^(.+):(\d+):(\d+): error: (.*)/::warning file=$(APP_DIR)\/\1,line=\2,col=\3::\4/"
else
FLAKE8_FORMAT := default
endif

# By default, all python/poetry commands will run inside of the docker container
Expand Down Expand Up @@ -220,10 +217,10 @@ format-check: ## Check file formatting

lint: lint-py ## Lint

lint-py: lint-flake lint-mypy
lint-py: lint-ruff lint-mypy

lint-flake:
$(PY_RUN_CMD) flake8 --format=$(FLAKE8_FORMAT) src tests bin
lint-ruff:
$(PY_RUN_CMD) ruff check src tests bin

lint-mypy:
$(PY_RUN_CMD) mypy --show-error-codes $(MYPY_FLAGS) src bin $(MYPY_POSTPROC)
Expand Down
Loading

0 comments on commit bf0217e

Please sign in to comment.