• Stars
    star
    421
  • Rank 99,813 (Top 3 %)
  • Language
    Python
  • License
    MIT License
  • Created almost 3 years ago
  • Updated 5 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

A linter to prevent exception handling antipatterns in Python (limited only for those who like dinosaurs).

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 = 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.3.2
    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.

More Repositories

1

gracy

Gracy helps you handle failures, logging, retries, throttling, and tracking for all your HTTP interactions.
Python
238
star
2

GMaps-Crawler

Google Maps crawler using Selenium. All extracted data is forwarded to a SQS queue.
Python
72
star
3

ast_selector

Query Python AST elements by using CSS Selector-like syntax
Python
32
star
4

flask-sqlinjection-vulnerable

Example of a Flask API vulnerable to SQL Injection with SQLite
Python
7
star
5

python-template

Template containing cool badges, headers, and some default configs
5
star
6

python-async-scenarios

Create (somewhat) realistic scenarios for comparison to python sync and async
Python
4
star
7

guilatrova

3
star
8

base-dev-setup

Suggestion for backing up your data and kicking off faster your dev setup machine with ansible
Shell
3
star
9

pokedex

React+Redux app to save your favorite pokemons
JavaScript
2
star
10

sam-lambda-python-template

Python
1
star
11

incc-crawler

Python
1
star
12

python-ensure-contract

Stub project to showcase how Python can ensure contracts are being respected for Liskov Principle / Inversion of Control principle without an IoC framework
Python
1
star
13

profly-api

The purpose is to track how much I invested in which stocks (USA and Brazil) and track my emotions as I take actions so I can finetune my decisions, understand why I did what I did, and ultimately learn from my own mistakes.
Python
1
star
14

grasshopper-dashboard

A new dashboard for managing your Locust load testing tool
JavaScript
1
star
15

lambda-pr-checks

Using AWS SAM to create a lambda to validate PR checks
Python
1
star
16

profly

The purpose is to track how much I invested in which stocks (USA and Brazil) and track my emotions as I take actions so I can finetune my decisions, understand why I did what I did, and ultimately learn from my own mistakes.
JavaScript
1
star