Skip to main content

Prevent Exception Handling AntiPatterns

Project description

Prevent Exception Handling AntiPatterns in Python

Actions Status PyPI Semantic Release GitHub Downloads Code style: black try/except style: tryceratops Follow guilatrova

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:

  • Add a comment with noqa to the top of the file you want to ignore
  • Add a comment with noqa to the line you want to ignore
  • Add a comment with noqa: CODE to 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 = 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: v2.1.0
    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.

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

tryceratops-2.1.0.tar.gz (19.4 kB view details)

Uploaded Source

Built Distribution

tryceratops-2.1.0-py3-none-any.whl (25.6 kB view details)

Uploaded Python 3

File details

Details for the file tryceratops-2.1.0.tar.gz.

File metadata

  • Download URL: tryceratops-2.1.0.tar.gz
  • Upload date:
  • Size: 19.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/37.3 requests/2.29.0 requests-toolbelt/0.10.1 urllib3/1.26.15 tqdm/4.65.0 importlib-metadata/6.6.0 keyring/23.13.1 rfc3986/2.0.0 colorama/0.4.6 CPython/3.8.16

File hashes

Hashes for tryceratops-2.1.0.tar.gz
Algorithm Hash digest
SHA256 6d6a5b4ca69666c59c68a30fab898d540f91c64c2a8a385d2ea1ac9354893ad8
MD5 83cd499a8dac365a806188f956d8ef43
BLAKE2b-256 eba018b3ce79ce0835c9b3f1d967f5b523633e1243cf2e9930cdadf8cd35895c

See more details on using hashes here.

File details

Details for the file tryceratops-2.1.0-py3-none-any.whl.

File metadata

  • Download URL: tryceratops-2.1.0-py3-none-any.whl
  • Upload date:
  • Size: 25.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.9.6 readme-renderer/37.3 requests/2.29.0 requests-toolbelt/0.10.1 urllib3/1.26.15 tqdm/4.65.0 importlib-metadata/6.6.0 keyring/23.13.1 rfc3986/2.0.0 colorama/0.4.6 CPython/3.8.16

File hashes

Hashes for tryceratops-2.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5546988f689d3488e6dbe2813b54b07ad6abaebc7d18932d153939d71857ec85
MD5 bdf9576adfb5f2199e0ce14742cc128a
BLAKE2b-256 acbce45a74d36a24c65680358db600f4ff7709130aaed86041952291eabc89b4

See more details on using hashes here.

Supported by

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