Home

Awesome

<p align="center"> <img src="https://raw.githubusercontent.com/guilatrova/tryceratops/main/img/logo.png"> </p> <h2 align="center">Prevent Exception Handling AntiPatterns in Python</h2> <p align="center"> <a href="https://github.com/guilatrova/tryceratops/actions"><img alt="Actions Status" src="https://github.com/guilatrova/tryceratops/workflows/CI/badge.svg"></a> <a href="https://pypi.org/project/tryceratops/"><img alt="PyPI" src="https://img.shields.io/pypi/v/tryceratops"/></a> <img src="https://badgen.net/pypi/python/tryceratops" /> <a href="https://github.com/relekang/python-semantic-release"><img alt="Semantic Release" src="https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg"></a> <a href="https://github.com/guilatrova/tryceratops/blob/main/LICENSE"><img alt="GitHub" src="https://img.shields.io/github/license/guilatrova/tryceratops"/></a> <a href="https://pepy.tech/project/tryceratops/"><img alt="Downloads" src="https://static.pepy.tech/personalized-badge/tryceratops?period=total&units=international_system&left_color=grey&right_color=blue&left_text=%F0%9F%A6%96%20Downloads"/></a> <a href="https://github.com/psf/black"><img alt="Code style: black" src="https://img.shields.io/badge/code%20style-black-000000.svg"/></a> <a href="https://github.com/guilatrova/tryceratops"><img alt="try/except style: tryceratops" src="https://img.shields.io/badge/try%2Fexcept%20style-tryceratops%20%F0%9F%A6%96%E2%9C%A8-black" /></a> <a href="https://twitter.com/intent/user?screen_name=guilatrova"><img alt="Follow guilatrova" src="https://img.shields.io/twitter/follow/guilatrova?style=social"/></a> </p>

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

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...]

example

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:

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

try/except style: tryceratops

Add this fancy badge to your project's README.md:

[![try/except style: tryceratops](https://img.shields.io/badge/try%2Fexcept%20style-tryceratops%20%F0%9F%A6%96%E2%9C%A8-black)](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.