PyCQA / PyCQA/flake8-bugbear

How to handle B015 within pytest.raises blocks

Open
#462 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
1.1k
Forks
123
Avg merge
2d 5h
Merged PRs (30d)
5

Description

Often when defining custom objects we end up testing that the __eq__ raises a particular error for unsupported comparisons. In practice, this means we have an unused comparison that triggers the B015 rule—see the MWE below. I wonder if there's a recommended way to get around this (without suppressing B015 altogether), or whether it could say be disabled within a with pytest.raises context.

Minimal working example:

# B015.py
import pytest


class MyClass:
    def __init__(self, x):
        self.x = x

    def __eq__(self, other):
        if not isinstance(other, MyClass):
            raise TypeError(f"Cannot compare with {type(other)}")
        return self.x == other.x


a = MyClass(5)
b = MyClass(5)
c = MyClass(4)

assert a == b
assert a != c
with pytest.raises(TypeError):
    a == a.x

$ flake8 B015.py 
B015.py:21:5: B015 Result of comparison is not used. This line doesn't do anything. Did you intend to prepend it with assert?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the MWE in B015.py with the reported flake8 command, then inspect how B015 identifies unused comparisons and whether pytest.raises context is available to that analysis. Done means establishing a documented workaround or a clearly scoped behavior for comparisons inside pytest.raises blocks, with tests covering the chosen behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.