tryceratops
tryceratops copied to clipboard
A linter to prevent exception handling antipatterns in Python (limited only for those who like dinosaurs).
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
- Installation
- Usage
flake8Plugin
- Violations
- Autofix support
- Ignoring violations
- Configuration
- 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 TCXXX repeatedly:
tryceratops --ignore TC201 --ignore TC202 [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 TC src/tests/samples/violations/call_raise_vanilla.py
src/tests/samples/violations/call_raise_vanilla.py:13:9: TC002 Create your own exception
src/tests/samples/violations/call_raise_vanilla.py:13:9: TC003 Avoid specifying long messages outside the exception class
src/tests/samples/violations/call_raise_vanilla.py:21:9: TC201 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: TC200, TC201, and TC400.
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: TC202
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 = ["TC002", "TC200", "TC300"]
experimental = true
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: v1.1.0
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:
- How to structure exceptions in Python π ποΈ π£
- How to log in Python ππ΄
- Book: Effective Python
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.