Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 12 additions & 6 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,7 @@ using ``pytest.raises``), or use the context manager form with a target
.. _B018:

**B018**: Found useless expression. Either assign it to a variable or remove it.
The check also considers function calls without side-effects such as ``isinstance``.
Note that dangling commas will cause things to be interpreted as useless tuples.
For example, in the statement ``print(".."),`` is the same as ``(print(".."),)``
which is an unassigned tuple. Simply remove the comma to clear the error.
Expand Down Expand Up @@ -292,16 +293,16 @@ second usage. Save the result to a list if the result is needed multiple times.

.. _B042:

**B042**: Exception classes with a custom `__init__` should pass all args to `super().__init__()` to work correctly with `copy.copy` and `pickle`.
Both `BaseException.__reduce__` and `BaseException.__str__` rely on the `args` attribute being set correctly, which is set in `BaseException.__new__` and `BaseException.__init__`.
If you define `__init__` yourself without passing all arguments to `super().__init__` it is very easy to break pickling, especially if they pass keyword arguments which both
`BaseException.__new__` and `BaseException.__init__` ignore. It's also important that `__init__` not accept any keyword-only parameters.
Alternately you can define both `__str__` and `__reduce__` to bypass the need for correct handling of `args`.
**B042**: Exception classes with a custom `__init__` should pass all args to `super().__init__()` to work correctly with `copy.copy` and `pickle`.
Both `BaseException.__reduce__` and `BaseException.__str__` rely on the `args` attribute being set correctly, which is set in `BaseException.__new__` and `BaseException.__init__`.
If you define `__init__` yourself without passing all arguments to `super().__init__` it is very easy to break pickling, especially if they pass keyword arguments which both
`BaseException.__new__` and `BaseException.__init__` ignore. It's also important that `__init__` not accept any keyword-only parameters.
Alternately you can define both `__str__` and `__reduce__` to bypass the need for correct handling of `args`.
If you define `__str__/__reduce__` in super classes this check is unable to detect it, and we advise disabling it.

.. _B043:

**B043**: Do not call ``delattr(x, 'attr')``, instead use ``del x.attr``.
**B043**: Do not call ``delattr(x, 'attr')``, instead use ``del x.attr``.
There is no additional safety in using ``delattr`` if you know the attribute name ahead of time.


Expand Down Expand Up @@ -494,6 +495,11 @@ MIT
Change Log
----------

UNRELEASED
~~~~~~~~~~

* B018: handle also useless calls such as `isinstance(x, int)` without assigning or using the result

25.11.29
~~~~~~~~

Expand Down
50 changes: 37 additions & 13 deletions bugbear.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,22 @@
ast.GeneratorExp,
)
FUNCTION_NODES = (ast.AsyncFunctionDef, ast.FunctionDef, ast.Lambda)
FUNCTIONS_WITHOUT_SIDE_EFFECTS = (
"all",
"any",
"dict",
"frozenset",
"isinstance",
"issubclass",
"len",
"max",
"min",
"repr",
"set",
"sorted",
"str",
"tuple",
)
B908_pytest_functions = {"raises", "warns"}
B908_unittest_methods = {
"assertRaises",
Expand Down Expand Up @@ -1466,19 +1482,27 @@ def check_for_b903(self, node: ast.ClassDef) -> None:
def check_for_b018(self, node: ast.AST) -> None:
if not isinstance(node, ast.Expr):
return
if isinstance(
node.value,
(
ast.List,
ast.Set,
ast.Dict,
ast.Tuple,
),
) or (
isinstance(node.value, ast.Constant)
and (
isinstance(node.value.value, (int, float, complex, bytes, bool))
or node.value.value is None
if (
isinstance(
node.value,
(
ast.List,
ast.Set,
ast.Dict,
ast.Tuple,
),
)
or (
isinstance(node.value, ast.Constant)
and (
isinstance(node.value.value, (int, float, complex, bytes, bool))
or node.value.value is None
)
)
or (
isinstance(node.value, ast.Call)
and isinstance(node.value.func, ast.Name)
and node.value.func.id in FUNCTIONS_WITHOUT_SIDE_EFFECTS
)
):
self.add_error("B018", node, node.value.__class__.__name__)
Expand Down
24 changes: 24 additions & 0 deletions tests/eval_files/b018_calls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
"""
Should emit:
B018 - on lines 11-24
"""

something()
assert issubclass(object, int)
x = sorted(foo)

# calls to be found
all() # B018: 0, "Call"
any() # B018: 0, "Call"
dict() # B018: 0, "Call"
frozenset() # B018: 0, "Call"
isinstance() # B018: 0, "Call"
issubclass() # B018: 0, "Call"
len() # B018: 0, "Call"
max() # B018: 0, "Call"
min() # B018: 0, "Call"
repr() # B018: 0, "Call"
set() # B018: 0, "Call"
sorted() # B018: 0, "Call"
str() # B018: 0, "Call"
tuple() # B018: 0, "Call"
78 changes: 39 additions & 39 deletions tests/eval_files/b023.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,50 +111,50 @@ def myfunc(x):
# argument or in a consumed `filter()` (even if a comprehension is better style)
for x in range(2):
# It's not a complete get-out-of-linting-free construct - these should fail:
min([None, lambda: x], key=repr) # B023: 23, "x"
sorted([None, lambda: x], key=repr) # B023: 26, "x"
any(filter(bool, [None, lambda: x])) # B023: 36, "x"
list(filter(bool, [None, lambda: x])) # B023: 37, "x"
all(reduce(bool, [None, lambda: x])) # B023: 36, "x"
_ = min([None, lambda: x], key=repr) # B023: 27, "x"
_ = sorted([None, lambda: x], key=repr) # B023: 30, "x"
_ = any(filter(bool, [None, lambda: x])) # B023: 40, "x"
_ = list(filter(bool, [None, lambda: x])) # B023: 41, "x"
_ = all(reduce(bool, [None, lambda: x])) # B023: 40, "x"

# But all these ones should be OK:
min(range(3), key=lambda y: x * y)
max(range(3), key=lambda y: x * y)
sorted(range(3), key=lambda y: x * y)

any(map(lambda y: x < y, range(3)))
all(map(lambda y: x < y, range(3)))
set(map(lambda y: x < y, range(3)))
list(map(lambda y: x < y, range(3)))
tuple(map(lambda y: x < y, range(3)))
sorted(map(lambda y: x < y, range(3)))
frozenset(map(lambda y: x < y, range(3)))

any(filter(lambda y: x < y, range(3)))
all(filter(lambda y: x < y, range(3)))
set(filter(lambda y: x < y, range(3)))
list(filter(lambda y: x < y, range(3)))
tuple(filter(lambda y: x < y, range(3)))
sorted(filter(lambda y: x < y, range(3)))
frozenset(filter(lambda y: x < y, range(3)))

any(reduce(lambda y: x | y, range(3)))
all(reduce(lambda y: x | y, range(3)))
set(reduce(lambda y: x | y, range(3)))
list(reduce(lambda y: x | y, range(3)))
tuple(reduce(lambda y: x | y, range(3)))
sorted(reduce(lambda y: x | y, range(3)))
frozenset(reduce(lambda y: x | y, range(3)))
_ = min(range(3), key=lambda y: x * y)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is so B018 does not fire now right?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

exactly

_ = max(range(3), key=lambda y: x * y)
_ = sorted(range(3), key=lambda y: x * y)

_ = any(map(lambda y: x < y, range(3)))
_ = all(map(lambda y: x < y, range(3)))
_ = set(map(lambda y: x < y, range(3)))
_ = list(map(lambda y: x < y, range(3)))
_ = tuple(map(lambda y: x < y, range(3)))
_ = sorted(map(lambda y: x < y, range(3)))
_ = frozenset(map(lambda y: x < y, range(3)))

_ = any(filter(lambda y: x < y, range(3)))
_ = all(filter(lambda y: x < y, range(3)))
_ = set(filter(lambda y: x < y, range(3)))
_ = list(filter(lambda y: x < y, range(3)))
_ = tuple(filter(lambda y: x < y, range(3)))
_ = sorted(filter(lambda y: x < y, range(3)))
_ = frozenset(filter(lambda y: x < y, range(3)))

_ = any(reduce(lambda y: x | y, range(3)))
_ = all(reduce(lambda y: x | y, range(3)))
_ = set(reduce(lambda y: x | y, range(3)))
_ = list(reduce(lambda y: x | y, range(3)))
_ = tuple(reduce(lambda y: x | y, range(3)))
_ = sorted(reduce(lambda y: x | y, range(3)))
_ = frozenset(reduce(lambda y: x | y, range(3)))

import functools

any(functools.reduce(lambda y: x | y, range(3)))
all(functools.reduce(lambda y: x | y, range(3)))
set(functools.reduce(lambda y: x | y, range(3)))
list(functools.reduce(lambda y: x | y, range(3)))
tuple(functools.reduce(lambda y: x | y, range(3)))
sorted(functools.reduce(lambda y: x | y, range(3)))
frozenset(functools.reduce(lambda y: x | y, range(3)))
_ = any(functools.reduce(lambda y: x | y, range(3)))
_ = all(functools.reduce(lambda y: x | y, range(3)))
_ = set(functools.reduce(lambda y: x | y, range(3)))
_ = list(functools.reduce(lambda y: x | y, range(3)))
_ = tuple(functools.reduce(lambda y: x | y, range(3)))
_ = sorted(functools.reduce(lambda y: x | y, range(3)))
_ = frozenset(functools.reduce(lambda y: x | y, range(3)))


# OK because the lambda which references a loop variable is defined in a `return`
Expand Down