summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorBatuhan Taskaya <isidentical@gmail.com>2022-06-05 21:50:56 +0300
committerGitHub <noreply@github.com>2022-06-05 14:50:56 -0400
commitc2135324b5fcb045000fbaee52e6c3f19b02c1f5 (patch)
tree6a875bef9db4a78423eb9e8627aec0fb0e4244a2 /tests
parent39c2a9805970ca57093d32bbaf0e6a63e05041d8 (diff)
downloadastroid-git-c2135324b5fcb045000fbaee52e6c3f19b02c1f5.tar.gz
Infer the actual unpacked value on Dict's getitem() (#1196)
Co-authored-by: Marc Mueller <30130371+cdce8p@users.noreply.github.com>
Diffstat (limited to 'tests')
-rw-r--r--tests/unittest_python3.py31
1 files changed, 30 insertions, 1 deletions
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)