Open boxydog opened 1 week ago
For example, just adding "I", "UP", and "SIM" on my branch produced this:
$ ruff check --fix
src/functions/create_archive.py:88:5: SIM117 Use a single `with` statement with multiple contexts instead of nested `with` statements
src/functions/generate_treasury_report.py:457:9: SIM118 Use `key in dict` instead of `key in dict.keys()`
src/functions/subrecipient_treasury_report_gen.py:135:5: SIM117 Use a single `with` statement with multiple contexts instead of nested `with` statements
src/functions/validate_workbook.py:34:5: SIM117 Use a single `with` statement with multiple contexts instead of nested `with` statements
tests/conftest.py:32:12: SIM115 Use context handler for opening files
tests/conftest.py:231:12: SIM115 Use context handler for opening files
Found 318 errors (312 fixed, 6 remaining).
No fixes available (2 hidden fixes can be enabled with the `--unsafe-fixes` option).
$ git diff | wc -l
2121
A lot of it is just re-ordering imports, and using a newer style of type hinting.
This is stuff we'd like a computer to keep straight for us.
Why is this issue important?
ruff does a good job of checking and auto-fixing python to be more correct and more consistent.
This project is just using the default 4 checkers, but there are dozens. One I like is
UP
, like pyupgrade, that updates all the code to be most idiomatic for our chosen python version (e.g., python 3.12). As an example, I noticed a bunch ofOptional[str]
type hints, which is nowstr | None
.Another which is aggressive but helpful is "Q" just to make sure all quotes are the same (default: double quotes). This is the kind of tiny stuff that it's good to just have a machine do.
In combination with pre-commit, ruff can auto-rewrite our code safely and with no effort as we push stuff up.
Current State
Default checkers.
Expected State
More checkers.
Implementation Plan
tool.ruff.lint
section ofpyproject.toml
inpython
.Relevant Code Snippets