Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/guilatrova/tryceratops
A linter to prevent exception handling antipatterns in Python (limited only for those who like dinosaurs).
https://github.com/guilatrova/tryceratops
code hacktoberfest linter python python3 static-analysis static-code-analysis styleguide stylelint
Last synced: 3 months ago
JSON representation
A linter to prevent exception handling antipatterns in Python (limited only for those who like dinosaurs).
- Host: GitHub
- URL: https://github.com/guilatrova/tryceratops
- Owner: guilatrova
- License: mit
- Created: 2021-06-19T11:33:40.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-07-08T16:39:28.000Z (4 months ago)
- Last Synced: 2024-07-08T19:58:40.859Z (4 months ago)
- Topics: code, hacktoberfest, linter, python, python3, static-analysis, static-code-analysis, styleguide, stylelint
- Language: Python
- Homepage:
- Size: 1.6 MB
- Stars: 424
- Watchers: 10
- Forks: 22
- Open Issues: 8
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- Contributing: docs/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
- awesome-flake8-extensions - tryceratops - Prevent exception handling antipatterns. (Clean code)
README
Prevent Exception Handling AntiPatterns in Python
Inspired by [this blog post](https://blog.guilatrova.dev/handling-exceptions-in-python-like-a-pro/). I described [the building process of this tool here](https://blog.guilatrova.dev/project-tryceratops/).
> βFor those who like dinosaurs π¦ and clean try/except β¨ blocks.β
**Summary**
- [Installation and usage](#installation-and-usage)
- [Installation](#installation)
- [Usage](#usage)
- [`flake8` Plugin](#flake8-plugin)
- [Violations](#violations)
- [Autofix support](#autofix-support)
- [Ignoring violations](#ignoring-violations)
- [Configuration](#configuration)
- [Pre-commit](#pre-commit)
- [Show your style](#show-your-style)
- [Extra Resources](#extra-resources)
- [Contributing](#contributing)
- [Change log](#change-log)
- [License](#license)
- [Credits](#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...]
```![example](https://raw.githubusercontent.com/guilatrova/tryceratops/main/img/tryceratops-example3.gif)
### [`flake8`](https://github.com/PyCQA/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](https://github.com/guilatrova/tryceratops/tree/main/docs/violations).
### Autofix support
So far, autofix only supports violations: [TRY200](docs/violations/TRY200.md), [TRY201](docs/violations/TRY201.md), and [TRY400](docs/violations/TRY400.md).
### 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 violationExample:
```py
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:
```toml
[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:
```yaml
- repo: https://github.com/guilatrova/tryceratops
rev: v2.3.3
hooks:
- id: tryceratops
```## Show your style
[![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)
Add this fancy badge to your project's `README.md`:
```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:
- [How to structure exceptions in Python π ποΈ π£](https://blog.guilatrova.dev/how-to-structure-exception-in-python-like-a-pro/)
- [How to log in Python ππ΄](https://blog.guilatrova.dev/how-to-log-in-python-like-a-pro/)
- [Book: Effective Python](https://amzn.to/3bEVHpG)## Contributing
Thank you for considering making Tryceratops better for everyone!
Refer to [Contributing docs](docs/CONTRIBUTING.md).
## Change log
See [CHANGELOG](CHANGELOG.md).
## License
MIT
## Credits
Thanks to God for the inspiration π βοΈ βοΈ
The [black](https://github.com/psf/black) project for insights.