Prevent Exception Handling AntiPatterns in Python
Inspired by this blog post. I described the building process of this tool here.
“For those who like dinosaurs 🦖 and clean try/except ✨ blocks.”
Summary
- Installation and usage
- Violations
- Pre-commit
- Show your style
- Extra Resources
- Contributing
- Change log
- License
- Credits
Installation and usage
Installation
pip install tryceratops
OR
poetry add -D tryceratops
Usage
tryceratops [filename or dir...]
You can enable experimental analyzers by running:
tryceratops --experimental [filename or dir...]
You can ignore specific violations by using: --ignore TRYXXX repeatedly:
tryceratops --ignore TRY201 --ignore TRY202 [filename or dir...]
You can exclude dirs by using: --exclude dir/path repeatedly:
tryceratops --exclude tests --exclude .venv [filename or dir...]
You can also autofix some violations:
tryceratops --autofix [filename or dir...]
flake8 Plugin
🦖 Tryceratops is also a plugin for flake8, so you can:
❯ flake8 --select TRY src/tests/samples/violations/call_raise_vanilla.py
src/tests/samples/violations/call_raise_vanilla.py:13:9: TRY002 Create your own exception
src/tests/samples/violations/call_raise_vanilla.py:13:9: TRY003 Avoid specifying long messages outside the exception class
src/tests/samples/violations/call_raise_vanilla.py:21:9: TRY201 Simply use 'raise' without specifying exception object again
Violations
All violations and its descriptions can be found in docs.
Autofix support
So far, autofix only supports violations: TRY200, TRY201, and TRY400.
Ignoring violations
If you want to ignore a violation in a specific file, you can either:
- Add a comment with
noqato the top of the file you want to ignore - Add a comment with
noqato the line you want to ignore - Add a comment with
noqa: CODEto the line you want to ignore a specific violation
Example:
def verbose_reraise_1():
try:
a = 1
except Exception as ex:
raise ex # noqa: TRY202
Configuration
You can set up a pyproject.toml file to set rules.
This is useful to avoid reusing the same CLI flags over and over again and helps to define the structure of your project.
Example:
[tool.tryceratops]
exclude = ["samples"]
ignore = ["TRY002", "TRY200", "TRY300"]
experimental = false
check_pickable = false
allowed_base_exceptions = ["MyAppBase"]
CLI flags always overwrite the config file.
Pre-commit
If you wish to use pre-commit, add this:
- repo: https://github.com/guilatrova/tryceratops
rev: v2.4.1
hooks:
- id: tryceratops
Show your style
Add this fancy badge to your project's README.md:
[](https://github.com/guilatrova/tryceratops)
Extra Resources
If you want to read more about:
Contributing
Thank you for considering making Tryceratops better for everyone!
Refer to Contributing docs.
Change log
See CHANGELOG.
License
MIT
Credits
Thanks to God for the inspiration 🙌 ☁️ ☀️
The black project for insights.
Metadata
Release files for tryceratops 2.4.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| tryceratops-2.4.1.tar.gz | 20.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| tryceratops-2.4.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 48.0 kB
Release files / tryceratops-2.4.1.tar.gz
| Download URL | tryceratops-2.4.1.tar.gz |
|---|---|
| Size | 20.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fbfc14650004e27cd1e55ebb7c76b43ced04a4d3b4cbe42aac9d71e4623ed84d
|
|
BLAKE2b-256 checksum How to use checksums |
2ad343c2c190bb6b01decc92d87538b451d7fe8070aaaf18aa1e166bf432754e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.11.1 readme-renderer/43.0 requests/2.32.3 requests-toolbelt/1.0.0 urllib3/2.2.2 tqdm/4.66.4 importlib-metadata/8.0.0 keyring/24.3.1 rfc3986/2.0.0 colorama/0.4.6 CPython/3.10.15
|
Release files / tryceratops-2.4.1-py3-none-any.whl
| Download URL | tryceratops-2.4.1-py3-none-any.whl |
|---|---|
| Size | 27.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
271b92367be89b243918a56361618607d2a9aa4aa4ba766ecfabd5f98c00480c
|
|
BLAKE2b-256 checksum How to use checksums |
27b588caeba349f17b7fd37b61bc88f81c138b3b5149b4e2a9deec66eaad1bdb
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.8.0 pkginfo/1.11.1 readme-renderer/43.0 requests/2.32.3 requests-toolbelt/1.0.0 urllib3/2.2.2 tqdm/4.66.4 importlib-metadata/8.0.0 keyring/24.3.1 rfc3986/2.0.0 colorama/0.4.6 CPython/3.10.15
|