Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ repos:
hooks:
- id: flake8
additional_dependencies: [flake8-bugbear]
exclude: ^tests/b.*
exclude: ^tests/eval_files/.*

- repo: https://github.com/rstcheck/rstcheck
rev: v6.2.4
Expand Down
31 changes: 28 additions & 3 deletions DEVELOPMENT.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,48 @@ cd flake8-bugbear
/path/to/venv/bin/pip install -e '.[dev]'
```

## Writing Tests

flake8-bugbear has a test runner that will go through all files in `tests/eval_files/`, run them through the linter, and check that they emit the appropriate error messages.

The expected errors are specified by adding comments on the line where the error is expected, using the format `# <error_code>: <col_offset>[, <var1>][, <var2>][...]`. E.g.
```python
x = ++n # B002: 4
try:
...
except* (ValueError,): # B013: 0, "ValueError", "*"
...
```
The error code should be in the `error_codes` dict, and the other values are passed to `eval` so should be valid python objects.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand what the var1, var2, ... do, could you clarify?

the other values are passed to eval so should be valid python objects

The example uses "*" which fails eval.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tried to clarify it, is it better now?

The example uses "*" which fails eval.

well, eval('"*"') is fully valid. But I tried making that more explicit as well.

Feel free to suggest any changes if it still doesn't make sense, the relevant logic is

# get text between `B\d\d\d:` and (end of line or another comment)
k = re.findall(r"(B\d\d\d):([^#]*)(?=#|$)", line)
for err_code, err_args in k:
# evaluate the arguments as if in a tuple
args = eval(f"({err_args},)")
assert args, "you must specify at least column"
col, *vars = args
assert isinstance(col, int), "column must be an int"
error_class = error_codes[err_code]
expected.append(error_class(lineno, col, vars=vars))

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, this is much better!


You can also specify options to be passed to `BugBearChecker` with an `# OPTIONS` comments
```python
# OPTIONS: extend_immutable_calls=["fastapi.Depends", "fastapi.Query"]
# OPTIONS: classmethod_decorators=["mylibrary.makeclassmethod", "validator"], select=["B902"]
```

If you specify a python version somewhere in the file name with `_pyXX`, the file will be skipped on smaller versions. Otherwise the name has no impact on the test, and you can test multiple errors in the same file.

The infrastructure is based on the test runner in https://github.com/python-trio/flake8-async which has some additional features that can be pulled into flake8-bugbear when desired.


## Running Tests

flake8-bugbear uses coverage to run standard unittest tests.

```console
/path/to/venv/bin/coverage run tests/test_bugbear.py
/path/to/venv/bin/coverage run -m pytest tests/test_bugbear.py
```

You can also use [tox](https://tox.wiki/en/latest/index.html) to test with multiple different python versions, emulating what the CI does.

```console
/path/to/venv/bin/tox
```
will by default run all tests on python versions 3.8 through 3.12. If you only want to test a specific version you can specify the environment with `-e`
will by default run all tests on python versions 3.9 through 3.13. If you only want to test a specific version you can specify the environment with `-e`

```console
/path/to/venv/bin/tox -e py38
/path/to/venv/bin/tox -e py313
```

## Running linter
Expand Down
Loading