Skip to content

B017 false-negative with multiple context managers #13603

@samueljsb

Description

@samueljsb
$ cat t.py
import contextlib

import pytest

def test_():
    with pytest.raises(Exception):
        pass

    with (
        contextlib.nullcontext(),
        pytest.raises(Exception),
    ):
        pass

$ ruff check --select B017 t.py
t.py:6:10: B017 `pytest.raises(Exception)` should be considered evil
  |
5 | def test_():
6 |     with pytest.raises(Exception):
  |          ^^^^^^^^^^^^^^^^^^^^^^^^ B017
7 |         pass
  |

Found 1 error.

This should report both occurrences of pytest.raises(Exception), but the second one is missing.


$ python --version --version
Python 3.12.5 (v3.12.5:ff3bc82f7c9, Aug  7 2024, 05:32:06) [Clang 13.0.0 (clang-1300.0.29.30)]

$ ruff --version
ruff 0.6.8

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workinghelp wantedContributions especially welcome

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions