Skip to content

Commit

Permalink
Swap out Flake8 for ruff in linting (#232)
Browse files Browse the repository at this point in the history
## Ticket

Resolves #107

## Changes

Change our linter from flake8 to ruff

Python package updates

## Context for reviewers

[ruff](https://docs.astral.sh/ruff/) is a linter built in Rust that is
both better performing and contains much more options than flake8
(including many of the Flake8 rules).

Ruff has a lot of [potential rules](https://docs.astral.sh/ruff/rules/)
we could configure, but to keep things simple, I ported over the same
rules that we already had configured. If we want to try out any of the
other rules, we can follow up with that. For context, enabling all rules
causes ~1300 errors, although many of the common ones I see are not
issues I think we would want to actually consider (complaining about
using `assert` in test functions)

Ruff can also serve as a formatter of code, potentially replacing black
+ isort, but I'll explore using that later as the change is likely much
bigger as it would cause a lot of little formatting differences

## Testing
Was able to run ruff via `make lint-ruff` and only encountered a few
small issues that I fixed.

We also switched to using this approach a few months ago in the simpler
grants codebase and haven't run into any issues.
HHS/simpler-grants-gov#1626

---------

Co-authored-by: nava-platform-bot <[email protected]>
  • Loading branch information
chouinar and nava-platform-bot authored Jun 26, 2024
1 parent e6093b3 commit 39affbf
Show file tree
Hide file tree
Showing 10 changed files with 1,092 additions and 1,040 deletions.
11 changes: 4 additions & 7 deletions app/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,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 := app
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 @@ -199,10 +196,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
lint-ruff:
$(PY_RUN_CMD) ruff check src tests

lint-mypy:
$(PY_RUN_CMD) mypy --show-error-codes $(MYPY_FLAGS) src $(MYPY_POSTPROC)
Expand Down
230 changes: 220 additions & 10 deletions app/openapi.generated.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,28 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPError'
type: object
properties:
message:
type: string
description: The message to return
data:
$ref: '#/components/schemas/HTTPError'
status_code:
type: integer
description: The HTTP status code
warnings:
type: array
items:
$ref: '#/components/schemas/ValidationError'
errors:
type: array
items:
$ref: '#/components/schemas/ValidationError'
pagination_info:
description: The pagination information for paginated endpoints
allOf:
- $ref: '#/components/schemas/PaginationInfo'
description: Service Unavailable
tags:
- Health
Expand Down Expand Up @@ -85,13 +106,55 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/ValidationError'
type: object
properties:
message:
type: string
description: The message to return
data:
$ref: '#/components/schemas/ValidationError'
status_code:
type: integer
description: The HTTP status code
warnings:
type: array
items:
$ref: '#/components/schemas/ValidationError'
errors:
type: array
items:
$ref: '#/components/schemas/ValidationError'
pagination_info:
description: The pagination information for paginated endpoints
allOf:
- $ref: '#/components/schemas/PaginationInfo'
description: Validation error
'401':
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPError'
type: object
properties:
message:
type: string
description: The message to return
data:
$ref: '#/components/schemas/HTTPError'
status_code:
type: integer
description: The HTTP status code
warnings:
type: array
items:
$ref: '#/components/schemas/ValidationError'
errors:
type: array
items:
$ref: '#/components/schemas/ValidationError'
pagination_info:
description: The pagination information for paginated endpoints
allOf:
- $ref: '#/components/schemas/PaginationInfo'
description: Authentication error
tags:
- User
Expand Down Expand Up @@ -140,13 +203,55 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/ValidationError'
type: object
properties:
message:
type: string
description: The message to return
data:
$ref: '#/components/schemas/ValidationError'
status_code:
type: integer
description: The HTTP status code
warnings:
type: array
items:
$ref: '#/components/schemas/ValidationError'
errors:
type: array
items:
$ref: '#/components/schemas/ValidationError'
pagination_info:
description: The pagination information for paginated endpoints
allOf:
- $ref: '#/components/schemas/PaginationInfo'
description: Validation error
'401':
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPError'
type: object
properties:
message:
type: string
description: The message to return
data:
$ref: '#/components/schemas/HTTPError'
status_code:
type: integer
description: The HTTP status code
warnings:
type: array
items:
$ref: '#/components/schemas/ValidationError'
errors:
type: array
items:
$ref: '#/components/schemas/ValidationError'
pagination_info:
description: The pagination information for paginated endpoints
allOf:
- $ref: '#/components/schemas/PaginationInfo'
description: Authentication error
tags:
- User
Expand Down Expand Up @@ -198,13 +303,55 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPError'
type: object
properties:
message:
type: string
description: The message to return
data:
$ref: '#/components/schemas/HTTPError'
status_code:
type: integer
description: The HTTP status code
warnings:
type: array
items:
$ref: '#/components/schemas/ValidationError'
errors:
type: array
items:
$ref: '#/components/schemas/ValidationError'
pagination_info:
description: The pagination information for paginated endpoints
allOf:
- $ref: '#/components/schemas/PaginationInfo'
description: Authentication error
'404':
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPError'
type: object
properties:
message:
type: string
description: The message to return
data:
$ref: '#/components/schemas/HTTPError'
status_code:
type: integer
description: The HTTP status code
warnings:
type: array
items:
$ref: '#/components/schemas/ValidationError'
errors:
type: array
items:
$ref: '#/components/schemas/ValidationError'
pagination_info:
description: The pagination information for paginated endpoints
allOf:
- $ref: '#/components/schemas/PaginationInfo'
description: Not found
tags:
- User
Expand Down Expand Up @@ -250,19 +397,82 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/ValidationError'
type: object
properties:
message:
type: string
description: The message to return
data:
$ref: '#/components/schemas/ValidationError'
status_code:
type: integer
description: The HTTP status code
warnings:
type: array
items:
$ref: '#/components/schemas/ValidationError'
errors:
type: array
items:
$ref: '#/components/schemas/ValidationError'
pagination_info:
description: The pagination information for paginated endpoints
allOf:
- $ref: '#/components/schemas/PaginationInfo'
description: Validation error
'401':
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPError'
type: object
properties:
message:
type: string
description: The message to return
data:
$ref: '#/components/schemas/HTTPError'
status_code:
type: integer
description: The HTTP status code
warnings:
type: array
items:
$ref: '#/components/schemas/ValidationError'
errors:
type: array
items:
$ref: '#/components/schemas/ValidationError'
pagination_info:
description: The pagination information for paginated endpoints
allOf:
- $ref: '#/components/schemas/PaginationInfo'
description: Authentication error
'404':
content:
application/json:
schema:
$ref: '#/components/schemas/HTTPError'
type: object
properties:
message:
type: string
description: The message to return
data:
$ref: '#/components/schemas/HTTPError'
status_code:
type: integer
description: The HTTP status code
warnings:
type: array
items:
$ref: '#/components/schemas/ValidationError'
errors:
type: array
items:
$ref: '#/components/schemas/ValidationError'
pagination_info:
description: The pagination information for paginated endpoints
allOf:
- $ref: '#/components/schemas/PaginationInfo'
description: Not found
tags:
- User
Expand Down
Loading

0 comments on commit 39affbf

Please sign in to comment.