From cfb85e474e1ad8bc9eb8424e28ee04e6bf274048 Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Sat, 9 Jul 2022 16:34:18 -0400 Subject: [PATCH] Fix a crash when `None` participates in a `**` expression --- ChangeLog | 2 ++ astroid/protocols.py | 2 ++ tests/unittest_protocols.py | 4 ++++ 3 files changed, 8 insertions(+) diff --git a/ChangeLog b/ChangeLog index 24e9b16e52..429bb60f7a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -12,6 +12,8 @@ What's New in astroid 2.12.1? ============================= Release date: TBA +* Fix a crash when ``None`` (or a value inferred as ``None``) participates in a + ``**`` expression. What's New in astroid 2.12.0? diff --git a/astroid/protocols.py b/astroid/protocols.py index 6a6523513d..a7f35ea4a6 100644 --- a/astroid/protocols.py +++ b/astroid/protocols.py @@ -121,6 +121,8 @@ def const_infer_binary_op(self, opnode, operator, other, context, _): if ( operator == "**" and isinstance(self, nodes.Const) + and isinstance(self.value, (int, float)) + and isinstance(other.value, (int, float)) and (self.value > 1e5 or other.value > 1e5) ): yield not_implemented diff --git a/tests/unittest_protocols.py b/tests/unittest_protocols.py index dfac90193e..63765a5ea5 100644 --- a/tests/unittest_protocols.py +++ b/tests/unittest_protocols.py @@ -275,6 +275,10 @@ def test_uninferable_exponents() -> None: parsed = extract_node("15 ** 20220609") assert parsed.inferred() == [Uninferable] + # Test a pathological case (more realistic: None as naive inference result) + parsed = extract_node("None ** 2") + assert parsed.inferred() == [Uninferable] + @pytest.mark.skipif(not PY38_PLUS, reason="needs assignment expressions") def test_named_expr_inference() -> None: