[3.10] bpo-44589: raise a SyntaxError when mapping patterns have duplicate literal keys (GH-27131) (GH-27157)

(cherry picked from commit 2693132292)


Co-authored-by: Jack DeVries <58614260+jdevries3133@users.noreply.github.com>

Automerge-Triggered-By: GH:brandtbucher
This commit is contained in:
Miss Islington (bot) 2021-07-14 18:00:35 -07:00 committed by GitHub
parent ff7af2203c
commit 016af14d93
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 82 additions and 19 deletions

View file

@ -2901,6 +2901,40 @@ class TestSyntaxErrors(unittest.TestCase):
pass
""")
def test_mapping_pattern_duplicate_key(self):
self.assert_syntax_error("""
match ...:
case {"a": _, "a": _}:
pass
""")
def test_mapping_pattern_duplicate_key_edge_case0(self):
self.assert_syntax_error("""
match ...:
case {0: _, False: _}:
pass
""")
def test_mapping_pattern_duplicate_key_edge_case1(self):
self.assert_syntax_error("""
match ...:
case {0: _, 0.0: _}:
pass
""")
def test_mapping_pattern_duplicate_key_edge_case2(self):
self.assert_syntax_error("""
match ...:
case {0: _, -0: _}:
pass
""")
def test_mapping_pattern_duplicate_key_edge_case3(self):
self.assert_syntax_error("""
match ...:
case {0: _, 0j: _}:
pass
""")
class TestTypeErrors(unittest.TestCase):
@ -3008,17 +3042,6 @@ class TestTypeErrors(unittest.TestCase):
class TestValueErrors(unittest.TestCase):
def test_mapping_pattern_checks_duplicate_key_0(self):
x = {"a": 0, "b": 1}
w = y = z = None
with self.assertRaises(ValueError):
match x:
case {"a": y, "a": z}:
w = 0
self.assertIs(w, None)
self.assertIs(y, None)
self.assertIs(z, None)
def test_mapping_pattern_checks_duplicate_key_1(self):
class Keys:
KEY = "a"