ruff
ruff copied to clipboard
Implement more flake8-bugbear opinionated rules
Picking up where https://github.com/charliermarsh/ruff/issues/2954 left off, there were a few opinionated (B9xx flake8-bugbear rules) checks left to be implemented in Ruff:
- [x] B901: Using
return x
in a generator function. (Somewhat bad reasoning in flake8-bugbear description, talks about Python 2, see comment here https://github.com/charliermarsh/ruff/issues/2954#issuecomment-1441162976 around its utility.)
- [x] B902: Implemented as N804 and N805.
- [ ] B903: Use
collections.namedtuple
(ortyping.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'sdataclasses
nowadays.) - [ ] ~B906:
visit_
function with no further call to avisit
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.
- [x] 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 https://github.com/charliermarsh/ruff/issues/2954#issuecomment-1483594606).
There's also one outstanding non-opinionated rule:
- [x] 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.
- [x] B038: Renamed to B909 in bugbear; implemented as B909 is Ruff.
B906
is flake8 plugins specific and has a false positive when the visit function is visiting an astroid node in a pylint plugin. It would be nice to not have this FP in ruff :)
Since then some new rule apppeared
- [ ] B907: (previous B028) Consider replacing
f"'{foo}'"
withf"{foo!r}"
which is both easier to read and will escape quotes inside foo if that would appear. The check tries to filter out any format specs that are invalid together with !r. If you're using other conversion flags then e.g.f"'{foo!a}'"
can be replaced withf"{ascii(foo)!r}"
. Not currently implemented for python<3.8 or str.format() calls. - [ ] B908: (looks like PT012 to me): Contexts with exceptions assertions like with
self.assertRaises
or withpytest.raises
should not have multiple top-level statements. Each statement should be in its own context. That way, the test ensures that the exception is raised only in the exact statement where you expect it.
Good first issues for anyone interested :)
Another 2 rules appeared in flake8-bugbear:
-
[x] B037: Found
return <value>
,yield
,yield <value>
, oryield from <value>
in class__init__()
method. No values should be returned or yielded, only bare returns are ok. (PLE0100) -
[ ] B038: Found a mutation of a mutable loop iterable inside the loop body. Changes to the iterable of a loop such as calls to list.remove() or via del can cause unintended bugs. https://github.com/astral-sh/ruff/issues/9511
B037 is already implemented as PLE0100, @charliermarsh maybe we should consider migrating PLE0100 to B037 though?
@Skylion007 - Yes good call -- I prefer indexing under bugbear over Pylint since it's more popular for Ruff users. I'll add a note to the 0.2.0 release list.
@zanieb Was this closed on purpose ?
I'm under the impression that some rules listed here are not part of ruff yet: B036, B038, B901, B902, B903, B907, B908
@mikaelarguedas - I think it was an oversight, but I'll leave it to @zanieb to confirm in the AM.
I think B036 is covered/aliased by BLE001.
@charliermarsh @zanieb friendly :bellhop_bell: if it was closed by mistake, is it possible to reopen and update according to current state ?
Current state: B036 -> covered by BLE001 B038 -> no implemented B901 -> no implemented B902 ->no implemented B903 ->no implemented B907 ->no implemented B908 ->no implemented
Will update the list, thanks.
Updated. We do actually have B038 (which bugbear moved to B909).
This issue says that B902
is "Implemented as N804
and N805
", but that is not accurate because N804
incorrectly says you should use cls
for "metaclass class methods" (e.g. __new__
, __prepare__
). These methods are the metaclass-equivalent of class methods and get passed the metaclass itself as the first argument. flake8-bugbear recommends metacls
, and pylint recommends mcs
(which flake8-bugbear will also accept).
Interesting task,can I help?