Skip to content

Commit

Permalink
feat: first version
Browse files Browse the repository at this point in the history
Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com>
  • Loading branch information
henryiii committed Sep 6, 2022
1 parent 06b34ce commit 2822277
Show file tree
Hide file tree
Showing 16 changed files with 219 additions and 269 deletions.
5 changes: 1 addition & 4 deletions .flake8
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
[flake8]
extend-ignore = E203, E501, E722, B950
select = C,E,F,W,T,B,B9,I
per-file-ignores =
tests/*: T
noxfile.py: T
extend-select = B9
4 changes: 0 additions & 4 deletions .git_archival.txt

This file was deleted.

1 change: 0 additions & 1 deletion .gitattributes

This file was deleted.

21 changes: 5 additions & 16 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ on:
pull_request:
push:
branches:
- master
- main
- develop
release:
types:
- published
Expand All @@ -22,18 +20,16 @@ jobs:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
fetch-depth: 0
- uses: actions/setup-python@v4
with:
python-version: "3.x"
python-version: "3.10"
- uses: pre-commit/action@v3.0.0
with:
extra_args: --hook-stage manual --all-files
- name: Run PyLint
run: |
echo "::add-matcher::$GITHUB_WORKSPACE/.github/matchers/pylint.json"
pipx run nox -s pylint
pipx run --python python nox -s pylint
checks:
name: Check Python ${{ matrix.python-version }} on ${{ matrix.runs-on }}
Expand All @@ -42,13 +38,9 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.7", "3.10"]
python-version: ["3.10", "3.11-dev"]
runs-on: [ubuntu-latest, macos-latest, windows-latest]

include:
- python-version: pypy-3.8
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
with:
Expand All @@ -62,7 +54,7 @@ jobs:
run: python -m pip install .[test]

- name: Test package
run: python -m pytest -ra --cov=flake8-errmsg
run: python -m pytest -ra

- name: Upload coverage report
uses: codecov/codecov-action@v3.1.0
Expand All @@ -87,10 +79,7 @@ jobs:
- name: Check products
run: pipx run twine check dist/*

- uses: pypa/gh-action-pypi-publish@v1.5.0
- uses: pypa/gh-action-pypi-publish@v1.5.1
if: github.event_name == 'release' && github.event.action == 'published'
with:
# Remember to generate this and set it in "GitHub Secrets"
password: ${{ secrets.pypi_password }}
# Remove this line
repository_url: https://test.pypi.org/legacy/
1 change: 0 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ repos:
exclude: docs/conf.py
additional_dependencies: &flake8_dependencies
- flake8-bugbear
- flake8-print

- repo: https://github.com/pycqa/flake8
rev: 5.0.4
Expand Down
21 changes: 0 additions & 21 deletions .readthedocs.yml

This file was deleted.

83 changes: 74 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,29 +1,94 @@
# flake8-errmsg

[![Actions Status][actions-badge]][actions-link]
[![Documentation Status][rtd-badge]][rtd-link]
[![GitHub Discussion][github-discussions-badge]][github-discussions-link]
[![Gitter][gitter-badge]][gitter-link]

[![PyPI version][pypi-version]][pypi-link]
[![Conda-Forge][conda-badge]][conda-link]
[![PyPI platforms][pypi-platforms]][pypi-link]

[![GitHub Discussion][github-discussions-badge]][github-discussions-link]
[![Gitter][gitter-badge]][gitter-link]
## Intro

A checker for flake8 that helps format nice error messages. Currently there are
two checks:

- **EM101**: Check for raw usage of string literals in Exception raising.
- **EM102**: Check for raw usage of f-string literals in Exception raising.

The issue is that Python includes the line with the raise in the default
traceback (and most other formatters, like Rich and IPython to too). That means
a user gets a message like this:

```python
sub = "Some value"
raise RuntimeError(f"{sub!r} is incorrect")
```

```pytb
Traceback (most recent call last):
File "tmp.py", line 2, in <module>
raise RuntimeError(f"{sub!r} is incorrect")
RuntimeError: 'Some value' is incorrect
```

If this is longer or more complex, the duplication can be quite confusing for a
user unaccustomed to reading tracebacks.

While if you always assign to something like `msg`, then you get:

```python
sub = "Some value"
msg = f"{sub!r} is incorrect"
raise RunetimeError(msg)
```

```pytb
Traceback (most recent call last):
File "tmp.py", line 3, in <module>
raise RuntimeError(msg)
RuntimeError: 'Some value' is incorrect
```

Now there's a simpler traceback, less code, and no double message. If you have a
long message, this also often formats better when using Black, too.

Reminder: Libraries should produce tracebacks with custom error classes, and
applications should print nice errors, usually _without_ a traceback, unless
something _unexpected_ occurred. An app should not print a traceback for an
error that is known to be triggerable by a user.

## Usage

Just add this to your `.pre-commit-config.yaml` `flake8` check under
`additional_dependencies`. If you use `extend-select`, you should need no other
config.

You can also manually run this check (without flake8's `noqa` filtering) via
script entry-point (`pipx run flake8_errmsg <files>`) or module entry-point
(`python -m flake8_errmsg <files>` when installed).

## FAQ

Q: Why not look for `"".format()` too? <br/> A: Tools like pyupgrade should help
move to fstrings, so these should be rare. But it would likely be easy to add.

Q: Why Python 3.10 only? <br/> A: This is a static checker and for developers.
Developers and static checks should be on 3.10 already. And I was lazy and match
statements are fantastic for this sort of thing. And the AST module changed in
3.8 anyway.

Q: What other sorts of checks are acceptable? <br/> A: Things that help with
nice errors. For example, maybe requiring `raise SystemExit(n)` over `sys.exit`,
`exit`, etc.

<!-- prettier-ignore-start -->
[actions-badge]: https://github.com/henryiii/flake8-errmsg/workflows/CI/badge.svg
[actions-link]: https://github.com/henryiii/flake8-errmsg/actions
[conda-badge]: https://img.shields.io/conda/vn/conda-forge/flake8-errmsg
[conda-link]: https://github.com/conda-forge/flake8-errmsg-feedstock
[github-discussions-badge]: https://img.shields.io/static/v1?label=Discussions&message=Ask&color=blue&logo=github
[github-discussions-link]: https://github.com/henryiii/flake8-errmsg/discussions
[gitter-badge]: https://badges.gitter.im/https://github.com/henryiii/flake8-errmsg/community.svg
[gitter-link]: https://gitter.im/https://github.com/henryiii/flake8-errmsg/community?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge
[pypi-link]: https://pypi.org/project/flake8-errmsg/
[pypi-platforms]: https://img.shields.io/pypi/pyversions/flake8-errmsg
[pypi-version]: https://badge.fury.io/py/flake8-errmsg.svg
[rtd-badge]: https://readthedocs.org/projects/flake8-errmsg/badge/?version=latest
[rtd-link]: https://flake8-errmsg.readthedocs.io/en/latest/?badge=latest
[sk-badge]: https://scikit-hep.org/assets/images/Scikit--HEP-Project-blue.svg
<!-- prettier-ignore-end -->
20 changes: 0 additions & 20 deletions docs/Makefile

This file was deleted.

63 changes: 0 additions & 63 deletions docs/conf.py

This file was deleted.

24 changes: 0 additions & 24 deletions docs/index.rst

This file was deleted.

35 changes: 0 additions & 35 deletions docs/make.bat

This file was deleted.

0 comments on commit 2822277

Please sign in to comment.