diff options
| author | Jacob Walls <jacobtylerwalls@gmail.com> | 2022-07-10 11:24:16 -0400 |
|---|---|---|
| committer | Pierre Sassoulas <pierre.sassoulas@gmail.com> | 2022-07-12 10:14:18 +0200 |
| commit | 153ca73f09f0699e2773864e6f5be50ce4031d21 (patch) | |
| tree | f1b50a1417cd66128753138a496c0714fb9a047e | |
| parent | 875d42e947cdd42adf9745eafaadb5cb8c407bb5 (diff) | |
| download | astroid-git-153ca73f09f0699e2773864e6f5be50ce4031d21.tar.gz | |
Prevent applying boolean ops to `NotImplemented` (#1702)
| -rw-r--r-- | astroid/protocols.py | 9 | ||||
| -rw-r--r-- | tests/unittest_inference.py | 6 |
2 files changed, 12 insertions, 3 deletions
diff --git a/astroid/protocols.py b/astroid/protocols.py index a7f35ea4..0d90d90b 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -78,12 +78,15 @@ _UNARY_OPERATORS: dict[str, Callable[[Any], Any]] = { 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) diff --git a/tests/unittest_inference.py b/tests/unittest_inference.py index 40364345..8673cdfc 100644 --- a/tests/unittest_inference.py +++ b/tests/unittest_inference.py @@ -1117,6 +1117,12 @@ class InferenceTest(resources.SysPathSetup, unittest.TestCase): 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__) |
