You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
**B024**: Abstract base class has methods, but none of them are abstract. This
163
211
is not necessarily an error, but you might have forgotten to add the @abstractmethod
164
212
decorator, potentially in conjunction with @classmethod, @property and/or @staticmethod.
165
213
214
+
.. _B025:
215
+
166
216
**B025**: ``try-except`` block with duplicate exceptions found.
167
217
This check identifies exception types that are specified in multiple ``except``
168
218
clauses. The first specification is the only one ever considered, so all others can be removed.
169
219
220
+
.. _B026:
221
+
170
222
**B026**: Star-arg unpacking after a keyword argument is strongly discouraged, because
171
223
it only works when the keyword parameter is declared after all parameters supplied by
172
224
the unpacked sequence, and this change of ordering can surprise and mislead readers.
173
225
There was `cpython discussion of disallowing this syntax
174
226
<https://github.com/python/cpython/issues/82741>`_, but legacy usage and parser
175
227
limitations make it difficult.
176
228
229
+
.. _B027:
230
+
177
231
**B027**: Empty method in abstract base class, but has no abstract decorator. Consider adding @abstractmethod.
178
232
233
+
.. _B028:
234
+
179
235
**B028**: No explicit stacklevel argument found. The warn method from the warnings module uses a
180
236
stacklevel of 1 by default. This will only show a stack trace for the line on which the warn method is called.
181
237
It is therefore recommended to use a stacklevel of 2 or greater to provide more information to the user.
182
238
239
+
.. _B029:
240
+
183
241
**B029**: Using ``except ():`` with an empty tuple does not handle/catch anything. Add exceptions to handle.
184
242
243
+
.. _B030:
244
+
185
245
**B030**: Except handlers should only be exception classes or tuples of exception classes.
186
246
247
+
.. _B031:
248
+
187
249
**B031**: Using the generator returned from `itertools.groupby()` more than once will do nothing on the
188
250
second usage. Save the result to a list if the result is needed multiple times.
189
251
252
+
.. _B032:
253
+
190
254
**B032**: Possible unintentional type annotation (using ``:``). Did you mean to assign (using ``=``)?
191
255
256
+
.. _B033:
257
+
192
258
**B033**: Sets should not contain duplicate items. Duplicate items will be replaced with a single item at runtime.
193
259
260
+
.. _B034:
261
+
194
262
**B034**: Calls to `re.sub`, `re.subn` or `re.split` should pass `flags` or `count`/`maxsplit` as keyword arguments. It is commonly assumed that `flags` is the third positional parameter, forgetting about `count`/`maxsplit`, since many other `re` module functions are of the form `f(pattern, string, flags)`.
195
263
264
+
.. _B035:
265
+
196
266
**B035**: Found dict comprehension with a static key - either a constant value or variable not from the comprehension expression. This will result in a dict with a single key that was repeatedly overwritten.
197
267
268
+
.. _B036:
269
+
198
270
**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.
199
271
272
+
.. _B037:
273
+
200
274
**B037**: Found ``return <value>``, ``yield``, ``yield <value>``, or ``yield from <value>`` in class ``__init__()`` method. No values should be returned or yielded, only bare ``return``\s are ok.
201
275
276
+
.. _B038:
277
+
202
278
**B038**: **Moved to B909** - 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.
203
279
280
+
.. _B039:
281
+
204
282
**B039**: ``ContextVar`` with mutable literal or function call as default. This is only evaluated once, and all subsequent calls to `.get()` would return the same instance of the default. This uses the same logic as B006 and B008, including ignoring values in ``extend-immutable-calls``.
205
283
284
+
.. _B040:
285
+
206
286
**B040**: Caught exception with call to ``add_note`` not used. Did you forget to ``raise`` it?
207
287
208
288
**B041**: Repeated key-value pair in dictionary literal. Only emits errors when the key's value is *also* the same, being the opposite of the pyflakes like check.
@@ -215,29 +295,39 @@ controversial. They may or may not apply to you, enable them explicitly
215
295
in your configuration if you find them useful. Read below on how to
216
296
enable.
217
297
298
+
.. _B901:
299
+
218
300
**B901**: Using ``return x`` in a generator function used to be
219
301
syntactically invalid in Python 2. In Python 3 ``return x`` can be used
220
302
in a generator as a return value in conjunction with ``yield from``.
221
303
Users coming from Python 2 may expect the old behavior which might lead
222
304
to bugs. Use native ``async def`` coroutines or mark intentional
223
305
``return x`` usage with ``# noqa`` on the same line.
224
306
307
+
.. _B902:
308
+
225
309
**B902**: Invalid first argument used for method. Use ``self`` for
226
310
instance methods, and ``cls`` for class methods (which includes ``__new__``
227
311
and ``__init_subclass__``) or instance methods of metaclasses (detected as
228
312
classes directly inheriting from ``type``).
229
313
314
+
.. _B903:
315
+
230
316
**B903**: Use ``collections.namedtuple`` (or ``typing.NamedTuple``) for
231
317
data classes that only set attributes in an ``__init__`` method, and do
232
318
nothing else. If the attributes should be mutable, define the attributes
233
319
in ``__slots__`` to save per-instance memory and to prevent accidentally
234
320
creating additional attributes on instances.
235
321
322
+
.. _B904:
323
+
236
324
**B904**: Within an ``except`` clause, raise exceptions with ``raise ... from err``
237
325
or ``raise ... from None`` to distinguish them from errors in exception handling.
238
326
See `the exception chaining tutorial <https://docs.python.org/3/tutorial/errors.html#exception-chaining>`_
239
327
for details.
240
328
329
+
.. _B905:
330
+
241
331
**B905**: ``zip()`` without an explicit `strict=` parameter set. ``strict=True`` causes the resulting iterator
242
332
to raise a ``ValueError`` if the arguments are exhausted at differing lengths.
243
333
@@ -246,22 +336,36 @@ Exclusions are `itertools.count <https://docs.python.org/3/library/itertools.htm
246
336
The ``strict=`` argument was added in Python 3.10, so don't enable this flag for code that should work on <3.10.
247
337
For more information: https://peps.python.org/pep-0618/
248
338
339
+
.. _B906:
340
+
249
341
**B906**: ``visit_`` function with no further call to a ``visit`` function. This is often an error, and will stop the visitor from recursing into the subnodes of a visited node. Consider adding a call ``self.generic_visit(node)`` at the end of the function.
250
342
Will only trigger on function names where the part after ``visit_`` is a valid ``ast`` type with a non-empty ``_fields`` attribute.
251
343
This is meant to be enabled by developers writing visitors using the ``ast`` module, such as flake8 plugin writers.
252
344
345
+
.. _B907:
346
+
253
347
**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. 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 with ``f"{ascii(foo)!r}"``. Not currently implemented for python<3.8 or ``str.format()`` calls.
254
348
349
+
.. _B908:
350
+
255
351
**B908**: Contexts with exceptions assertions like ``with self.assertRaises`` or ``with pytest.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.
256
352
353
+
.. _B909:
354
+
257
355
**B909**: **Was 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.
258
356
357
+
.. _B910:
358
+
259
359
**B910**: Use Counter() instead of defaultdict(int) to avoid excessive memory use as the default dict will record missing keys with the default value when accessed.
260
360
361
+
.. _B911:
362
+
261
363
**B911**: ``itertools.batched()`` without an explicit `strict=` parameter set. ``strict=True`` causes the resulting iterator to raise a ``ValueError`` if the final batch is shorter than ``n``.
262
364
263
365
The ``strict=`` argument was added in Python 3.13, so don't enable this flag for code that should work on <3.13.
264
366
367
+
.. _B950:
368
+
265
369
**B950**: Line too long. This is a pragmatic equivalent of
266
370
``pycodestyle``'s ``E501``: it considers "max-line-length" but only triggers
267
371
when the value has been exceeded by **more than 10%**. ``noqa`` and ``type: ignore`` comments are ignored. You will no
@@ -324,11 +428,15 @@ Configuration
324
428
325
429
The plugin currently has the following settings:
326
430
431
+
.. _extend_immutable_calls:
432
+
327
433
``extend-immutable-calls``: Specify a list of additional immutable calls.
328
434
This could be useful, when using other libraries that provide more immutable calls,
329
435
beside those already handled by ``flake8-bugbear``. Calls to these method will no longer
330
436
raise a ``B008`` or ``B039`` warning.
331
437
438
+
.. _classmethod_decorators:
439
+
332
440
``classmethod-decorators``: Specify a list of decorators to additionally mark a method as a ``classmethod`` as used by B902. The default only checks for ``classmethod``. When an ``@obj.name`` decorator is specified it will match against either ``name`` or ``obj.name``.
333
441
This functions similarly to how `pep8-naming <https://github.com/PyCQA/pep8-naming>` handles it, but with different defaults, and they don't support specifying attributes such that a decorator will never match against a specified value ``obj.name`` even if decorated with ``@obj.name``.
0 commit comments