From c2135324b5fcb045000fbaee52e6c3f19b02c1f5 Mon Sep 17 00:00:00 2001 From: Batuhan Taskaya Date: Sun, 5 Jun 2022 21:50:56 +0300 Subject: Infer the actual unpacked value on Dict's getitem() (#1196) Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com> --- tests/unittest_python3.py | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) (limited to 'tests') diff --git a/tests/unittest_python3.py b/tests/unittest_python3.py index 9f60833e..bf0a0b33 100644 --- a/tests/unittest_python3.py +++ b/tests/unittest_python3.py @@ -5,7 +5,9 @@ import unittest from textwrap import dedent -from astroid import nodes +import pytest + +from astroid import exceptions, nodes from astroid.builder import AstroidBuilder, extract_node from astroid.test_utils import require_version @@ -285,6 +287,33 @@ class Python3TC(unittest.TestCase): self.assertIsInstance(value, nodes.Const) self.assertEqual(value.value, expected) + @staticmethod + def test_unpacking_in_dict_getitem_with_ref() -> None: + node = extract_node( + """ + a = {1: 2} + {**a, 2: 3} #@ + """ + ) + assert isinstance(node, nodes.Dict) + + for key, expected in ((1, 2), (2, 3)): + value = node.getitem(nodes.Const(key)) + assert isinstance(value, nodes.Const) + assert value.value == expected + + @staticmethod + def test_unpacking_in_dict_getitem_uninferable() -> None: + node = extract_node("{**a, 2: 3}") + assert isinstance(node, nodes.Dict) + + with pytest.raises(exceptions.AstroidIndexError): + node.getitem(nodes.Const(1)) + + value = node.getitem(nodes.Const(2)) + assert isinstance(value, nodes.Const) + assert value.value == 3 + def test_format_string(self) -> None: code = "f'{greetings} {person}'" node = extract_node(code) -- cgit v1.2.1