summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorMarc Mueller <30130371+cdce8p@users.noreply.github.com>2021-07-10 23:48:57 +0200
committerGitHub <noreply@github.com>2021-07-10 23:48:57 +0200
commitc9c498348174b38ce35bfe001353c8ebea262802 (patch)
treea638453198e158e9b2282094b4b2720b7fd2a101 /tests
parent2376f5e14b3d43bdd579f327ee75c16ccb778d7c (diff)
downloadastroid-git-c9c498348174b38ce35bfe001353c8ebea262802.tar.gz
Fix issues when inferring match variables (#1093)
* Fix issues when inferring match variables * Fix extract_node for MatchCase * Typing improvements * Fix pylint issues (no-name-in-module) * Use PY310_PLUS instead of sys.version_info
Diffstat (limited to 'tests')
-rw-r--r--tests/unittest_protocols.py70
1 files changed, 69 insertions, 1 deletions
diff --git a/tests/unittest_protocols.py b/tests/unittest_protocols.py
index 246cf1d9..e130b570 100644
--- a/tests/unittest_protocols.py
+++ b/tests/unittest_protocols.py
@@ -19,7 +19,7 @@ import pytest
import astroid
from astroid import extract_node, nodes, util
-from astroid.const import PY38_PLUS
+from astroid.const import PY38_PLUS, PY310_PLUS
from astroid.exceptions import InferenceError
from astroid.node_classes import AssignName, Const, Name, Starred
@@ -266,5 +266,73 @@ def test_named_expr_inference():
assert node.value == 1
+@pytest.mark.skipif(not PY310_PLUS, reason="Match requires python 3.10")
+def test_assigned_stmts_match_mapping():
+ """Assigned_stmts for MatchMapping not yet implemented.
+
+ Test the result is 'Uninferable' and no exception is raised.
+ """
+ assign_stmts = extract_node(
+ """
+ var = {1: "Hello", 2: "World"}
+ match var:
+ case {**rest}: #@
+ pass
+ """
+ )
+ match_mapping: nodes.MatchMapping = assign_stmts.pattern # type: ignore
+ assert match_mapping.rest
+ assigned = next(match_mapping.rest.assigned_stmts())
+ assert assigned == util.Uninferable
+
+
+@pytest.mark.skipif(not PY310_PLUS, reason="Match requires python 3.10")
+def test_assigned_stmts_match_star():
+ """Assigned_stmts for MatchStar not yet implemented.
+
+ Test the result is 'Uninferable' and no exception is raised.
+ """
+ assign_stmts = extract_node(
+ """
+ var = (0, 1, 2)
+ match var:
+ case (0, 1, *rest): #@
+ pass
+ """
+ )
+ match_sequence: nodes.MatchSequence = assign_stmts.pattern # type: ignore
+ match_star = match_sequence.patterns[2]
+ assert isinstance(match_star, nodes.MatchStar) and match_star.name
+ assigned = next(match_star.name.assigned_stmts())
+ assert assigned == util.Uninferable
+
+
+@pytest.mark.skipif(not PY310_PLUS, reason="Match requires python 3.10")
+def test_assigned_stmts_match_as():
+ """Assigned_stmts for MatchAs only implemented for the most basic case (y)."""
+ assign_stmts = extract_node(
+ """
+ var = 42
+ match var: #@
+ case 2 | x: #@
+ pass
+ case y: #@
+ pass
+ """
+ )
+ subject: nodes.Const = assign_stmts[0].subject # type: ignore
+ match_or: nodes.MatchOr = assign_stmts[1].pattern # type: ignore
+ match_as: nodes.MatchAs = assign_stmts[2].pattern # type: ignore
+
+ assert match_as.name
+ assigned_match_as = next(match_as.name.assigned_stmts())
+ assert assigned_match_as == subject
+
+ match_or_1 = match_or.patterns[1]
+ assert isinstance(match_or_1, nodes.MatchAs) and match_or_1.name
+ assigned_match_or_1 = next(match_or_1.name.assigned_stmts())
+ assert assigned_match_or_1 == util.Uninferable
+
+
if __name__ == "__main__":
unittest.main()