Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

style: Enable flake8-errmsg rules and fix errors #4845

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

echoix
Copy link
Member

@echoix echoix commented Dec 15, 2024

This PR enables 3 more ruff rules, derived from the flake8-errmsg linter.

These rules checks for the use of string literals, f-strings or .format() strings used in exception constructors.
By moving the string to a separate variable, msg, this makes the traceback shown to users clearer, as the raise statement (repeated in the traceback) doesn't contain the (incomplete) error message twice.
It also helps a bit with python formatting, less often the statement needs to be broken in 3 lines with parentheses.

The examples from the ruff rule pages are really clear on the end result.
Taken from https://docs.astral.sh/ruff/rules/raw-string-in-exception/:

Example

Given:

raise RuntimeError("'Some value' is incorrect")

Python will produce a traceback like:

Traceback (most recent call last):
  File "tmp.py", line 2, in <module>
    raise RuntimeError("'Some value' is incorrect")
RuntimeError: 'Some value' is incorrect

Instead, assign the string to a variable:

msg = "'Some value' is incorrect"
raise RuntimeError(msg)

Which will produce a traceback like:

Traceback (most recent call last):
  File "tmp.py", line 3, in <module>
    raise RuntimeError(msg)
RuntimeError: 'Some value' is incorrect

Exception must not use a string literal, assign to variable first

Produces a clearer stack trace, without duplicating the text of the error message

Ruff rule: https://docs.astral.sh/ruff/rules/raw-string-in-exception/
Exception must not use an f-string literal, assign to variable first

Produces a clearer stack trace, without duplicating the text of the error message

Ruff rule: https://docs.astral.sh/ruff/rules/f-string-in-exception/
Exception must not use a `.format()` string directly, assign to variable first

Produces a clearer stack trace, without duplicating the text of the error message

Ruff rule: https://docs.astral.sh/ruff/rules/dot-format-in-exception/
@github-actions github-actions bot added GUI wxGUI related vector Related to vector data processing Python Related code is in Python database Related to database management libraries module general tests Related to Test Suite notebook labels Dec 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
database Related to database management general GUI wxGUI related libraries module notebook Python Related code is in Python tests Related to Test Suite vector Related to vector data processing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant