Skip to content

Commit

Permalink
Prevent applying boolean ops to NotImplemented (#1702)
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtylerwalls authored and Pierre-Sassoulas committed Jul 12, 2022
1 parent 875d42e commit 153ca73
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
9 changes: 6 additions & 3 deletions astroid/protocols.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,15 @@ def _augmented_name(name):


def _infer_unary_op(obj: Any, op: str) -> ConstFactoryResult:
"""Perform unary operation on object.
"""Perform unary operation on `obj`, unless it is `NotImplemented`.
Can raise TypeError if operation is unsupported.
"""
func = _UNARY_OPERATORS[op]
value = func(obj)
if obj is NotImplemented:
value = obj
else:
func = _UNARY_OPERATORS[op]
value = func(obj)
return nodes.const_factory(value)


Expand Down
6 changes: 6 additions & 0 deletions tests/unittest_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -1117,6 +1117,12 @@ def __radd__(self, other): return NotImplemented
first = next(ast_node.infer())
self.assertEqual(first, util.Uninferable)

@pytest.mark.filterwarnings("error::DeprecationWarning")
def test_binary_op_not_used_in_boolean_context(self) -> None:
ast_node = extract_node("not NotImplemented")
first = next(ast_node.infer())
self.assertIsInstance(first, nodes.Const)

def test_binary_op_list_mul(self) -> None:
for code in ("a = [[]] * 2", "a = 2 * [[]]"):
ast = builder.string_build(code, __name__, __file__)
Expand Down

0 comments on commit 153ca73

Please sign in to comment.