Skip to main content

Flake8 checker for raw literals inside raises.

Project description

flake8-errmsg

Actions Status PyPI version PyPI platforms

Intro

A checker for Flake8 that helps format nice error messages. The checks are:

  • EM101: Check for raw usage of a string literal in Exception raising.
  • EM102: Check for raw usage of an f-string literal in Exception raising.
  • EM103: Check for raw usage of .format on a string literal in Exception raising.
  • EM104: Check for missing parentheses for built-in exceptions.
  • EM105: Check for missing message for built-in exceptions.
  • EM106: Check for walrus assignment inside a raise call's arguments.

The issue is that Python includes the line with the raise in the default traceback (and most other formatters, like Rich and IPython to too). That means a user gets a message like this:

sub = "Some value"
raise RuntimeError(f"{sub!r} is incorrect")
Traceback (most recent call last):
  File "tmp.py", line 2, in <module>
    raise RuntimeError(f"{sub!r} is incorrect")
RuntimeError: 'Some value' is incorrect

If this is longer or more complex, the duplication can be quite confusing for a user unaccustomed to reading tracebacks.

While if you always assign to something like msg, then you get:

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

Now there's a simpler traceback and no double message. If you have a long message, this also often formats better when using Black, too.

Reminder: Libraries should produce tracebacks with custom error classes, and applications should print nice errors, usually without a traceback, unless something unexpected occurred. An app should not print a traceback for an error that is known to be triggerable by a user.

Options

There is one option, --errmsg-max-string-length, which defaults to 0 but can be set to a larger value. The check will ignore string literals shorter than this length. This option is supported in configuration mode as well. This will only affect string literals and not f-strings. This option is also supported when running directly, without Flake8.

Usage

Just add this to your .pre-commit-config.yaml flake8 check under additional_dependencies. If you use extend-select, you should need no other config.

You can also manually run this check (without Flake8's noqa filtering) via script entry-point (pipx run flake8-errmsg <files>) or module entry-point (python -m flake8_errmsg <files> when installed).

FAQ

Q: Why Python 3.10+ only?
A: This is a static checker and for developers. Developers and static checks should be on 3.10 already. And I was lazy and match statements are fantastic for this sort of thing. And the AST module changed in 3.8 anyway. Use Ruff (which contains the checks from this plugin) if you need to run on older versions.

Q: What other sorts of checks are acceptable?
A: Things that help with nice errors. For example, maybe requiring raise SystemExit(n) over sys.exit, exit, etc. Possibly adding a check for warnings.warn without setting stacklevel to something (usually 2).

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

flake8_errmsg-0.6.0.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

flake8_errmsg-0.6.0-py3-none-any.whl (9.7 kB view details)

Uploaded Python 3

File details

Details for the file flake8_errmsg-0.6.0.tar.gz.

File metadata

  • Download URL: flake8_errmsg-0.6.0.tar.gz
  • Upload date:
  • Size: 13.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for flake8_errmsg-0.6.0.tar.gz
Algorithm Hash digest
SHA256 c74e2f9a38fe9f3cdffc15c389433b86fe863acce4d32838184926a54d01e4a4
MD5 b4f4ad6015a4087cfccd4444acde2498
BLAKE2b-256 1a9c33cb33699aee13543b48882d4013f4791bd59ffdbdb8a343e2745dc18515

See more details on using hashes here.

Provenance

The following attestation bundles were made for flake8_errmsg-0.6.0.tar.gz:

Publisher: cd.yml on henryiii/flake8-errmsg

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file flake8_errmsg-0.6.0-py3-none-any.whl.

File metadata

  • Download URL: flake8_errmsg-0.6.0-py3-none-any.whl
  • Upload date:
  • Size: 9.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for flake8_errmsg-0.6.0-py3-none-any.whl
Algorithm Hash digest
SHA256 34d54f94599e49198330bc1cfdfebafd192ac9bc93e7925396f56bdf59ac933f
MD5 c6e0ea667ec31efa5c790479a66fe78c
BLAKE2b-256 57c3f5a03dac42380b410fd3376f64e75fa79ea55c0c3e42dde114af4096ebf3

See more details on using hashes here.

Provenance

The following attestation bundles were made for flake8_errmsg-0.6.0-py3-none-any.whl:

Publisher: cd.yml on henryiii/flake8-errmsg

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page