Flake8 checker for raw literals inside raises.
Project description
flake8-errmsg
Intro
A checker for flake8 that helps format nice error messages. Currently there are two checks:
- EM101: Check for raw usage of string literals in Exception raising.
- EM102: Check for raw usage of f-string literals in Exception raising.
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 RunetimeError(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, less code, 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.
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 not look for "".format()
too?
A: Tools like pyupgrade should help
move to fstrings, so these should be rare. But it would likely be easy to add.
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.
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.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file flake8_errmsg-0.2.3.tar.gz
.
File metadata
- Download URL: flake8_errmsg-0.2.3.tar.gz
- Upload date:
- Size: 12.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 22c0e278eef51b2db7dd44b4b17cc480c2f333e9bf494edfaeb6e47e11a751b7 |
|
MD5 | f6983d112e15be64f5a14aca9108a436 |
|
BLAKE2b-256 | 27dcc1affce66f351d079e8f7ed3f5fb0f45e64375749cc354c994ba0d295ee2 |
File details
Details for the file flake8_errmsg-0.2.3-py3-none-any.whl
.
File metadata
- Download URL: flake8_errmsg-0.2.3-py3-none-any.whl
- Upload date:
- Size: 8.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3b56e1b2c897e34807f4068e2f918709d5879e2b66e1a32dbe9307fd501c7c9c |
|
MD5 | 7e16c0b7c3ade182b8b3f0c3276eaa34 |
|
BLAKE2b-256 | bd7aa3eb6e93212dd6648eceb50082911a74e63ef7c92f2c1a3a589a26b7d81f |