Skip to content

Implement more flake8-bugbear opinionated rules #3758

@sjdemartini

Description

@sjdemartini

Picking up where #2954 left off, there were a few opinionated (B9xx flake8-bugbear rules) checks left to be implemented in Ruff:

  • B901: Using return x in a generator function. (Somewhat bad reasoning in flake8-bugbear description, talks about Python 2, see comment here Update bugbear #2954 (comment) around its utility.)
  • B902: Implemented as N804 and N805.
  • B903: Use collections.namedtuple (or typing.NamedTuple) for data classes that only set attributes in an __init__ method, and do nothing else. (Probably should include dataclasses recommendation? NamedTuple injects extra tuple methods and is meant for backward compat, not a data class replacement. That's dataclasses nowadays.)
  • B906: visit_ function with no further call to a visit function.
  • B907: Consider replacing f"'{foo}'" with f"{foo!r}" which is both easier to read and will escape quotes inside foo if that would appear.
  • B908: Partly implemented as PT012.

There's an open question on how these should be included, since it would deviate from flake8-bugbear to have these on by default just by turning on the rest of the bugbear rules (see comment #2954 (comment)).

There's also one outstanding non-opinionated rule:

  • B036: Found except BaseException: without re-raising (no raise in the top-level of the except block). This catches all kinds of things (Exception, SystemExit, KeyboardInterrupt...) and may prevent a program from exiting as expected. Implemented as BLE001.
  • B038: Renamed to B909 in bugbear; implemented as B909 is Ruff.

Metadata

Metadata

Assignees

No one assigned

    Labels

    ruleImplementing or modifying a lint rule

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions