opensafely / opensafely/documentation

Address `ruff` formatting issues for local development

Open Beginner friendly
#2,051 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
48
Forks
10
Avg merge
2d 19h
Merged PRs (30d)
17

Description

Why are we doing this?

When on the dependabot rota, I reviewed a PR for a version bump for Ruff from 0.16.3 to 0.16.4.

This was a patch release that passed our CI checks, was merged, and deployed successfully. However, after pulling down the branch and running just fix locally (which runs the Ruff linter), I found that Ruff reported two remaining errors:

$ just fix
/home/katie/bin/just _compile requirements.prod.in requirements.prod.txt 
/home/katie/bin/just _compile requirements.dev.in requirements.dev.txt 
$BIN/ruff check --fix .
RUF015 Prefer `next(iter(section.keys()))` over single element slice
  --> hooks/ehrql_branch.py:22:12
   |
20 |         (i, section)
21 |         for i, section in enumerate(config["nav"])
22 |         if list(section.keys())[0] == "ehrQL"
   |            ^^^^^^^^^^^^^^^^^^^^^^^
23 |     )
24 |     new_import_string = ehrql_nav_section["ehrQL"].replace(
   |
help: Replace with `next(iter(section.keys()))`

INP001 File `scripts/wordcount.py` is part of an implicit namespace package. Add an `__init__.py`.
--> scripts/wordcount.py:1:1

Found 4 errors (2 fixed, 2 remaining).
No fixes available (1 hidden fix can be enabled with the `--unsafe-fixes` option).
error: recipe `fix` failed on line 123 with exit code 1

What are we doing

Resolve the Ruff lint failures introduced by the ruff upgrade.

How will we know when it's done?

just fix completes successfully with no remaining Ruff errors, and the existing CI checks continue to pass.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the Ruff output and inspect hooks/ehrql_branch.py:22 and scripts/wordcount.py:1, the two files named in the failing diagnostics. Run just fix before and after the changes, then confirm it completes with no Ruff errors and that the existing CI checks still pass.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
85/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.