summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cython/Compiler/MatchCaseNodes.py255
-rw-r--r--Cython/Compiler/Nodes.py11
-rw-r--r--Cython/Compiler/ParseTreeTransforms.pxd1
-rw-r--r--Cython/Compiler/ParseTreeTransforms.py27
-rw-r--r--Cython/Compiler/Parsing.pxd2
-rw-r--r--Cython/Compiler/Parsing.py506
-rw-r--r--Cython/TestUtils.py24
-rw-r--r--Tools/ci-run.sh2
-rw-r--r--test-requirements-pypy27.txt1
-rw-r--r--tests/run/extra_patma.pyx18
-rw-r--r--tests/run/test_patma.py3454
11 files changed, 4275 insertions, 26 deletions
diff --git a/Cython/Compiler/MatchCaseNodes.py b/Cython/Compiler/MatchCaseNodes.py
new file mode 100644
index 000000000..dbd5b8770
--- /dev/null
+++ b/Cython/Compiler/MatchCaseNodes.py
@@ -0,0 +1,255 @@
+# Nodes for structural pattern matching.
+#
+# In a separate file because they're unlikely to be useful for much else.
+
+from .Nodes import Node, StatNode
+from .Errors import error
+
+
+class MatchNode(StatNode):
+ """
+ subject ExprNode The expression to be matched
+ cases [MatchCaseNode] list of cases
+ """
+
+ child_attrs = ["subject", "cases"]
+
+ def validate_irrefutable(self):
+ found_irrefutable_case = None
+ for case in self.cases:
+ if found_irrefutable_case:
+ error(
+ found_irrefutable_case.pos,
+ (
+ "%s makes remaining patterns unreachable"
+ % found_irrefutable_case.pattern.irrefutable_message()
+ ),
+ )
+ break
+ if case.is_irrefutable():
+ found_irrefutable_case = case
+ case.validate_irrefutable()
+
+ def analyse_expressions(self, env):
+ error(self.pos, "Structural pattern match is not yet implemented")
+ return self
+
+
+class MatchCaseNode(Node):
+ """
+ pattern PatternNode
+ body StatListNode
+ guard ExprNode or None
+ """
+
+ child_attrs = ["pattern", "body", "guard"]
+
+ def is_irrefutable(self):
+ return self.pattern.is_irrefutable() and not self.guard
+
+ def validate_targets(self):
+ self.pattern.get_targets()
+
+ def validate_irrefutable(self):
+ self.pattern.validate_irrefutable()
+
+
+class PatternNode(Node):
+ """
+ PatternNode is not an expression because
+ it does several things (evalutating a boolean expression,
+ assignment of targets), and they need to be done at different
+ times.
+
+ as_targets [NameNode] any target assign by "as"
+ """
+
+ child_attrs = ["as_targets"]
+
+ def __init__(self, pos, **kwds):
+ if "as_targets" not in kwds:
+ kwds["as_targets"] = []
+ super(PatternNode, self).__init__(pos, **kwds)
+
+ def is_irrefutable(self):
+ return False
+
+ def get_targets(self):
+ targets = self.get_main_pattern_targets()
+ for target in self.as_targets:
+ self.add_target_to_targets(targets, target.name)
+ return targets
+
+ def update_targets_with_targets(self, targets, other_targets):
+ for name in targets.intersection(other_targets):
+ error(self.pos, "multiple assignments to name '%s' in pattern" % name)
+ targets.update(other_targets)
+
+ def add_target_to_targets(self, targets, target):
+ if target in targets:
+ error(self.pos, "multiple assignments to name '%s in pattern" % target)
+ targets.add(target)
+
+ def get_main_pattern_targets(self):
+ # exclude "as" target
+ raise NotImplementedError
+
+ def validate_irrefutable(self):
+ for attr in self.child_attrs:
+ child = getattr(self, attr)
+ if child is not None and isinstance(child, PatternNode):
+ child.validate_irrefutable()
+
+
+class MatchValuePatternNode(PatternNode):
+ """
+ value ExprNode # todo be more specific
+ is_is_check bool Picks "is" or equality check
+ """
+
+ child_attrs = PatternNode.child_attrs + ["value"]
+ is_is_check = False
+
+ def get_main_pattern_targets(self):
+ return set()
+
+
+class MatchAndAssignPatternNode(PatternNode):
+ """
+ target NameNode or None the target to assign to (None = wildcard)
+ is_star bool
+ """
+
+ target = None
+ is_star = False
+
+ child_atts = PatternNode.child_attrs + ["target"]
+
+ def is_irrefutable(self):
+ return not self.is_star
+
+ def irrefutable_message(self):
+ if self.target:
+ return "name capture '%s'" % self.target.name
+ else:
+ return "wildcard"
+
+ def get_main_pattern_targets(self):
+ if self.target:
+ return {self.target.name}
+ else:
+ return set()
+
+
+class OrPatternNode(PatternNode):
+ """
+ alternatives list of PatternNodes
+ """
+
+ child_attrs = PatternNode.child_attrs + ["alternatives"]
+
+ def get_first_irrefutable(self):
+ for alternative in self.alternatives:
+ if alternative.is_irrefutable():
+ return alternative
+ return None
+
+ def is_irrefutable(self):
+ return self.get_first_irrefutable() is not None
+
+ def irrefutable_message(self):
+ return self.get_first_irrefutable().irrefutable_message()
+
+ def get_main_pattern_targets(self):
+ child_targets = None
+ for alternative in self.alternatives:
+ alternative_targets = alternative.get_targets()
+ if child_targets is not None and child_targets != alternative_targets:
+ error(self.pos, "alternative patterns bind different names")
+ child_targets = alternative_targets
+ return child_targets
+
+ def validate_irrefutable(self):
+ super(OrPatternNode, self).validate_irrefutable()
+ found_irrefutable_case = None
+ for alternative in self.alternatives:
+ if found_irrefutable_case:
+ error(
+ found_irrefutable_case.pos,
+ (
+ "%s makes remaining patterns unreachable"
+ % found_irrefutable_case.irrefutable_message()
+ ),
+ )
+ break
+ if alternative.is_irrefutable():
+ found_irrefutable_case = alternative
+ alternative.validate_irrefutable()
+
+
+class MatchSequencePatternNode(PatternNode):
+ """
+ patterns list of PatternNodes
+ """
+
+ child_attrs = PatternNode.child_attrs + ["patterns"]
+
+ def get_main_pattern_targets(self):
+ targets = set()
+ for pattern in self.patterns:
+ self.update_targets_with_targets(targets, pattern.get_targets())
+ return targets
+
+
+class MatchMappingPatternNode(PatternNode):
+ """
+ keys list of NameNodes
+ value_patterns list of PatternNodes of equal length to keys
+ double_star_capture_target NameNode or None
+ """
+
+ keys = []
+ value_patterns = []
+ double_star_capture_target = None
+
+ child_attrs = PatternNode.child_attrs + [
+ "keys",
+ "value_patterns",
+ "double_star_capture_target",
+ ]
+
+ def get_main_pattern_targets(self):
+ targets = set()
+ for pattern in self.value_patterns:
+ self.update_targets_with_targets(targets, pattern.get_targets())
+ if self.double_star_capture_target:
+ self.add_target_to_targets(targets, self.double_star_capture_target.name)
+ return targets
+
+
+class ClassPatternNode(PatternNode):
+ """
+ class_ NameNode or AttributeNode
+ positional_patterns list of PatternNodes
+ keyword_pattern_names list of NameNodes
+ keyword_pattern_patterns list of PatternNodes
+ (same length as keyword_pattern_names)
+ """
+
+ class_ = None
+ positional_patterns = []
+ keyword_pattern_names = []
+ keyword_pattern_patterns = []
+
+ child_attrs = PatternNode.child_attrs + [
+ "class_",
+ "positional_patterns",
+ "keyword_pattern_names",
+ "keyword_pattern_patterns",
+ ]
+
+ def get_main_pattern_targets(self):
+ targets = set()
+ for pattern in self.positional_patterns + self.keyword_pattern_patterns:
+ self.update_targets_with_targets(targets, pattern.get_targets())
+ return targets
diff --git a/Cython/Compiler/Nodes.py b/Cython/Compiler/Nodes.py
index 7a9192234..5c3321326 100644
--- a/Cython/Compiler/Nodes.py
+++ b/Cython/Compiler/Nodes.py
@@ -10155,6 +10155,17 @@ class CnameDecoratorNode(StatNode):
self.node.generate_execution_code(code)
+class ErrorNode(Node):
+ """
+ Node type for things that we want to get through the parser
+ (especially for things that are being scanned in "tentative_scan"
+ blocks), but should immediately raise and error afterwards.
+
+ what str
+ """
+ child_attrs = []
+
+
#------------------------------------------------------------------------------------
#
# Runtime support code
diff --git a/Cython/Compiler/ParseTreeTransforms.pxd b/Cython/Compiler/ParseTreeTransforms.pxd
index efbb14f70..2778be4ef 100644
--- a/Cython/Compiler/ParseTreeTransforms.pxd
+++ b/Cython/Compiler/ParseTreeTransforms.pxd
@@ -18,6 +18,7 @@ cdef class PostParse(ScopeTrackingTransform):
cdef dict specialattribute_handlers
cdef size_t lambda_counter
cdef size_t genexpr_counter
+ cdef bint in_pattern_node
cdef _visit_assignment_node(self, node, list expr_list)
diff --git a/Cython/Compiler/ParseTreeTransforms.py b/Cython/Compiler/ParseTreeTransforms.py
index bc4943b79..5301578c3 100644
--- a/Cython/Compiler/ParseTreeTransforms.py
+++ b/Cython/Compiler/ParseTreeTransforms.py
@@ -193,6 +193,7 @@ class PostParse(ScopeTrackingTransform):
self.specialattribute_handlers = {
'__cythonbufferdefaults__' : self.handle_bufferdefaults
}
+ self.in_pattern_node = False
def visit_LambdaNode(self, node):
# unpack a lambda expression into the corresponding DefNode
@@ -385,6 +386,32 @@ class PostParse(ScopeTrackingTransform):
self.visitchildren(node)
return node
+ def visit_ErrorNode(self, node):
+ error(node.pos, node.what)
+ return None
+
+ def visit_MatchCaseNode(self, node):
+ node.validate_targets()
+ self.visitchildren(node)
+ return node
+
+ def visit_MatchNode(self, node):
+ node.validate_irrefutable()
+ self.visitchildren(node)
+ return node
+
+ def visit_PatternNode(self, node):
+ in_pattern_node, self.in_pattern_node = self.in_pattern_node, True
+ self.visitchildren(node)
+ self.in_pattern_node = in_pattern_node
+ return node
+
+ def visit_JoinedStrNode(self, node):
+ if self.in_pattern_node:
+ error(node.pos, "f-strings are not accepted for pattern matching")
+ self.visitchildren(node)
+ return node
+
class _AssignmentExpressionTargetNameFinder(TreeVisitor):
def __init__(self):
super(_AssignmentExpressionTargetNameFinder, self).__init__()
diff --git a/Cython/Compiler/Parsing.pxd b/Cython/Compiler/Parsing.pxd
index 72a855fd4..997cdf513 100644
--- a/Cython/Compiler/Parsing.pxd
+++ b/Cython/Compiler/Parsing.pxd
@@ -62,6 +62,8 @@ cdef expect_ellipsis(PyrexScanner s)
cdef make_slice_nodes(pos, subscripts)
cpdef make_slice_node(pos, start, stop = *, step = *)
cdef p_atom(PyrexScanner s)
+cdef p_atom_string(PyrexScanner s)
+cdef p_atom_ident_constants(PyrexScanner s)
@cython.locals(value=unicode)
cdef p_int_literal(PyrexScanner s)
cdef p_name(PyrexScanner s, name)
diff --git a/Cython/Compiler/Parsing.py b/Cython/Compiler/Parsing.py
index 30d73588d..94fc2eca1 100644
--- a/Cython/Compiler/Parsing.py
+++ b/Cython/Compiler/Parsing.py
@@ -25,6 +25,7 @@ from functools import partial, reduce
from .Scanning import PyrexScanner, FileSourceDescriptor, tentatively_scan
from . import Nodes
from . import ExprNodes
+from . import MatchCaseNodes
from . import Builtin
from . import StringEncoding
from .StringEncoding import EncodedString, bytes_literal, _unicode, _bytes
@@ -717,36 +718,55 @@ def p_atom(s):
s.next()
return ExprNodes.ImagNode(pos, value = value)
elif sy == 'BEGIN_STRING':
- kind, bytes_value, unicode_value = p_cat_string_literal(s)
- if kind == 'c':
- return ExprNodes.CharNode(pos, value = bytes_value)
- elif kind == 'u':
- return ExprNodes.UnicodeNode(pos, value = unicode_value, bytes_value = bytes_value)
- elif kind == 'b':
- return ExprNodes.BytesNode(pos, value = bytes_value)
- elif kind == 'f':
- return ExprNodes.JoinedStrNode(pos, values = unicode_value)
- elif kind == '':
- return ExprNodes.StringNode(pos, value = bytes_value, unicode_value = unicode_value)
- else:
- s.error("invalid string kind '%s'" % kind)
+ return p_atom_string(s)
elif sy == 'IDENT':
- name = s.systring
- if name == "None":
- result = ExprNodes.NoneNode(pos)
- elif name == "True":
- result = ExprNodes.BoolNode(pos, value=True)
- elif name == "False":
- result = ExprNodes.BoolNode(pos, value=False)
- elif name == "NULL" and not s.in_python_file:
- result = ExprNodes.NullNode(pos)
- else:
- result = p_name(s, name)
- s.next()
+ result = p_atom_ident_constants(s)
+ if result is None:
+ result = p_name(s, s.systring)
+ s.next()
return result
else:
s.error("Expected an identifier or literal")
+
+def p_atom_string(s):
+ pos = s.position()
+ kind, bytes_value, unicode_value = p_cat_string_literal(s)
+ if kind == 'c':
+ return ExprNodes.CharNode(pos, value=bytes_value)
+ elif kind == 'u':
+ return ExprNodes.UnicodeNode(pos, value=unicode_value, bytes_value=bytes_value)
+ elif kind == 'b':
+ return ExprNodes.BytesNode(pos, value=bytes_value)
+ elif kind == 'f':
+ return ExprNodes.JoinedStrNode(pos, values=unicode_value)
+ elif kind == '':
+ return ExprNodes.StringNode(pos, value=bytes_value, unicode_value=unicode_value)
+ else:
+ s.error("invalid string kind '%s'" % kind)
+
+
+def p_atom_ident_constants(s):
+ """
+ Returns None if it isn't one special-cased named constants.
+ Only calls s.next() if it successfully matches a matches.
+ """
+ pos = s.position()
+ name = s.systring
+ result = None
+ if name == "None":
+ result = ExprNodes.NoneNode(pos)
+ elif name == "True":
+ result = ExprNodes.BoolNode(pos, value=True)
+ elif name == "False":
+ result = ExprNodes.BoolNode(pos, value=False)
+ elif name == "NULL" and not s.in_python_file:
+ result = ExprNodes.NullNode(pos)
+ if result:
+ s.next()
+ return result
+
+
def p_int_literal(s):
pos = s.position()
value = s.systring
@@ -2443,6 +2463,11 @@ def p_statement(s, ctx, first_statement = 0):
elif decorators:
s.error("Decorators can only be followed by functions or classes")
s.put_back(u'IDENT', ident_name, ident_pos) # re-insert original token
+ if s.sy == 'IDENT' and s.systring == 'match':
+ # p_match_statement returns None on a "soft" initial failure
+ match_statement = p_match_statement(s, ctx)
+ if match_statement:
+ return match_statement
return p_simple_statement_list(s, ctx, first_statement=first_statement)
@@ -4019,6 +4044,437 @@ def p_cpp_class_attribute(s, ctx):
return node
+def p_match_statement(s, ctx):
+ assert s.sy == "IDENT" and s.systring == "match"
+ pos = s.position()
+ with tentatively_scan(s) as errors:
+ s.next()
+ subject = p_namedexpr_test(s)
+ subjects = None
+ if s.sy == ",":
+ subjects = [subject]
+ while s.sy == ",":
+ s.next()
+ if s.sy == ":":
+ break
+ subjects.append(p_test(s))
+ if subjects is not None:
+ subject = ExprNodes.TupleNode(pos, args=subjects)
+ s.expect(":")
+ if errors:
+ return None
+
+ # at this stage were commited to it being a match block so continue
+ # outside "with tentatively_scan"
+ # (I think this deviates from the PEG parser slightly, and it'd
+ # backtrack on the whole thing)
+ s.expect_newline()
+ s.expect_indent()
+ cases = []
+ while s.sy != "DEDENT":
+ cases.append(p_case_block(s, ctx))
+ s.expect_dedent()
+ return MatchCaseNodes.MatchNode(pos, subject=subject, cases=cases)
+
+
+def p_case_block(s, ctx):
+ if not (s.sy == "IDENT" and s.systring == "case"):
+ s.error("Expected 'case'")
+ s.next()
+ pos = s.position()
+ pattern = p_patterns(s)
+ guard = None
+ if s.sy == 'if':
+ s.next()
+ guard = p_test(s)
+ body = p_suite(s, ctx)
+
+ return MatchCaseNodes.MatchCaseNode(pos, pattern=pattern, body=body, guard=guard)
+
+
+def p_patterns(s):
+ # note - in slight contrast to the name (which comes from the Python grammar),
+ # returns a single pattern
+ patterns = []
+ seq = False
+ pos = s.position()
+ while True:
+ with tentatively_scan(s) as errors:
+ pattern = p_maybe_star_pattern(s)
+ if errors:
+ if patterns:
+ break # all is good provided we have at least 1 pattern
+ else:
+ e = errors[0]
+ s.error(e.args[1], pos=e.args[0])
+ patterns.append(pattern)
+
+ if s.sy == ",":
+ seq = True
+ s.next()
+ if s.sy in [":", "if"]:
+ break # common reasons to break
+ else:
+ break
+
+ if seq:
+ return MatchCaseNodes.MatchSequencePatternNode(pos, patterns=patterns)
+ else:
+ return patterns[0]
+
+
+def p_maybe_star_pattern(s):
+ # For match case. Either star_pattern or pattern
+ if s.sy == "*":
+ # star pattern
+ s.next()
+ target = None
+ if s.systring != "_": # for match-case '_' is treated as a special wildcard
+ target = p_pattern_capture_target(s)
+ else:
+ s.next()
+ pattern = MatchCaseNodes.MatchAndAssignPatternNode(
+ s.position(), target=target, is_star=True
+ )
+ return pattern
+ else:
+ pattern = p_pattern(s)
+ return pattern
+
+
+def p_pattern(s):
+ # try "as_pattern" then "or_pattern"
+ # (but practically "as_pattern" starts with "or_pattern" too)
+ patterns = []
+ pos = s.position()
+ while True:
+ patterns.append(p_closed_pattern(s))
+ if s.sy == "|":
+ s.next()
+ else:
+ break
+
+ if len(patterns) > 1:
+ pattern = MatchCaseNodes.OrPatternNode(
+ pos,
+ alternatives=patterns
+ )
+ else:
+ pattern = patterns[0]
+
+ if s.sy == 'IDENT' and s.systring == 'as':
+ s.next()
+ with tentatively_scan(s) as errors:
+ pattern.as_targets.append(p_pattern_capture_target(s))
+ if errors and s.sy == "_":
+ s.next()
+ # make this a specific error
+ return Nodes.ErrorNode(errors[0].args[0], what=errors[0].args[1])
+ elif errors:
+ with tentatively_scan(s):
+ expr = p_test(s)
+ return Nodes.ErrorNode(expr.pos, what="Invalid pattern target")
+ s.error(errors[0])
+ return pattern
+
+
+def p_closed_pattern(s):
+ """
+ The PEG parser specifies it as
+ | literal_pattern
+ | capture_pattern
+ | wildcard_pattern
+ | value_pattern
+ | group_pattern
+ | sequence_pattern
+ | mapping_pattern
+ | class_pattern
+
+ For the sake avoiding too much backtracking, we know:
+ * starts with "{" is a sequence_pattern
+ * starts with "[" is a mapping_pattern
+ * starts with "(" is a group_pattern or sequence_pattern
+ * wildcard pattern is just identifier=='_'
+ The rest are then tried in order with backtracking
+ """
+ if s.sy == 'IDENT' and s.systring == '_':
+ pos = s.position()
+ s.next()
+ return MatchCaseNodes.MatchAndAssignPatternNode(pos)
+ elif s.sy == '{':
+ return p_mapping_pattern(s)
+ elif s.sy == '[':
+ return p_sequence_pattern(s)
+ elif s.sy == '(':
+ with tentatively_scan(s) as errors:
+ result = p_group_pattern(s)
+ if not errors:
+ return result
+ return p_sequence_pattern(s)
+
+ with tentatively_scan(s) as errors:
+ result = p_literal_pattern(s)
+ if not errors:
+ return result
+ with tentatively_scan(s) as errors:
+ result = p_capture_pattern(s)
+ if not errors:
+ return result
+ with tentatively_scan(s) as errors:
+ result = p_value_pattern(s)
+ if not errors:
+ return result
+ return p_class_pattern(s)
+
+
+def p_literal_pattern(s):
+ # a lot of duplication in this function with "p_atom"
+ next_must_be_a_number = False
+ sign = ''
+ if s.sy == '-':
+ sign = s.sy
+ sign_pos = s.position()
+ s.next()
+ next_must_be_a_number = True
+
+ sy = s.sy
+ pos = s.position()
+
+ res = None
+ if sy == 'INT':
+ res = p_int_literal(s)
+ elif sy == 'FLOAT':
+ value = s.systring
+ s.next()
+ res = ExprNodes.FloatNode(pos, value=value)
+
+ if res and sign == "-":
+ res = ExprNodes.UnaryMinusNode(sign_pos, operand=res)
+
+ if res and s.sy in ['+', '-']:
+ sign = s.sy
+ s.next()
+ if s.sy != 'IMAG':
+ s.error("Expected imaginary number")
+ else:
+ add_pos = s.position()
+ value = s.systring[:-1]
+ s.next()
+ res = ExprNodes.binop_node(
+ add_pos,
+ sign,
+ operand1=res,
+ operand2=ExprNodes.ImagNode(s.position(), value=value)
+ )
+
+ if not res and sy == 'IMAG':
+ value = s.systring[:-1]
+ s.next()
+ res = ExprNodes.ImagNode(pos, value=sign+value)
+ if sign == "-":
+ res = ExprNodes.UnaryMinusNode(sign_pos, operand=res)
+
+ if res:
+ return MatchCaseNodes.MatchValuePatternNode(pos, value=res)
+
+ if next_must_be_a_number:
+ s.error("Expected a number")
+ if sy == 'BEGIN_STRING':
+ res = p_atom_string(s)
+ # f-strings not being accepted is validated in PostParse
+ return MatchCaseNodes.MatchValuePatternNode(pos, value=res)
+ elif sy == 'IDENT':
+ # Note that p_atom_ident_constants includes NULL.
+ # This is a deliberate Cython addition to the pattern matching specification
+ result = p_atom_ident_constants(s)
+ if result:
+ return MatchCaseNodes.MatchValuePatternNode(pos, value=result, is_is_check=True)
+
+ s.error("Failed to match literal")
+
+
+def p_capture_pattern(s):
+ return MatchCaseNodes.MatchAndAssignPatternNode(
+ s.position(),
+ target=p_pattern_capture_target(s)
+ )
+
+
+def p_value_pattern(s):
+ if s.sy != "IDENT":
+ s.error("Expected identifier")
+ pos = s.position()
+ res = p_name(s, s.systring)
+ s.next()
+ if s.sy != '.':
+ s.error("Expected '.'")
+ while s.sy == '.':
+ attr_pos = s.position()
+ s.next()
+ attr = p_ident(s)
+ res = ExprNodes.AttributeNode(attr_pos, obj=res, attribute=attr)
+ if s.sy in ['(', '=']:
+ s.error("Unexpected symbol '%s'" % s.sy)
+ return MatchCaseNodes.MatchValuePatternNode(pos, value=res)
+
+
+def p_group_pattern(s):
+ s.expect("(")
+ pattern = p_pattern(s)
+ s.expect(")")
+ return pattern
+
+
+def p_sequence_pattern(s):
+ opener = s.sy
+ pos = s.position()
+ if opener in ['[', '(']:
+ closer = ']' if opener == '[' else ')'
+ s.next()
+ # maybe_sequence_pattern and open_sequence_pattern
+ patterns = []
+ if s.sy == closer:
+ s.next()
+ else:
+ while True:
+ patterns.append(p_maybe_star_pattern(s))
+ if s.sy == ",":
+ s.next()
+ if s.sy == closer:
+ break
+ else:
+ if opener == ')' and len(patterns) == 1:
+ s.error("tuple-like pattern of length 1 must finish with ','")
+ break
+ s.expect(closer)
+ return MatchCaseNodes.MatchSequencePatternNode(pos, patterns=patterns)
+ else:
+ s.error("Expected '[' or '('")
+
+
+def p_mapping_pattern(s):
+ pos = s.position()
+ s.expect('{')
+ if s.sy == '}':
+ # trivial empty mapping
+ s.next()
+ return MatchCaseNodes.MatchMappingPatternNode(pos)
+
+ double_star_capture_target = None
+ items_patterns = []
+ star_star_arg_pos = None
+ while True:
+ if double_star_capture_target and not star_star_arg_pos:
+ star_star_arg_pos = s.position()
+ if s.sy == '**':
+ s.next()
+ double_star_capture_target = p_pattern_capture_target(s)
+ else:
+ # key=(literal_expr | attr)
+ with tentatively_scan(s) as errors:
+ pattern = p_literal_pattern(s)
+ key = pattern.value
+ if errors:
+ pattern = p_value_pattern(s)
+ key = pattern.value
+ s.expect(':')
+ value = p_pattern(s)
+ items_patterns.append((key, value))
+ if s.sy != ',':
+ break
+ s.next()
+ if s.sy == '}':
+ break # Allow trailing comma.
+ s.expect('}')
+
+ if star_star_arg_pos is not None:
+ return Nodes.ErrorNode(
+ star_star_arg_pos,
+ what = "** pattern must be the final part of a mapping pattern."
+ )
+ return MatchCaseNodes.MatchMappingPatternNode(
+ pos,
+ keys = [kv[0] for kv in items_patterns],
+ value_patterns = [kv[1] for kv in items_patterns],
+ double_star_capture_target = double_star_capture_target
+ )
+
+
+def p_class_pattern(s):
+ # start by parsing the class as name_or_attr
+ pos = s.position()
+ res = p_name(s, s.systring)
+ s.next()
+ while s.sy == '.':
+ attr_pos = s.position()
+ s.next()
+ attr = p_ident(s)
+ res = ExprNodes.AttributeNode(attr_pos, obj=res, attribute=attr)
+ class_ = res
+
+ s.expect("(")
+ if s.sy == ")":
+ # trivial case with no arguments matched
+ s.next()
+ return MatchCaseNodes.ClassPatternNode(pos, class_=class_)
+
+ # parse the arguments
+ positional_patterns = []
+ keyword_patterns = []
+ keyword_patterns_error = None
+ while True:
+ with tentatively_scan(s) as errors:
+ positional_patterns.append(p_pattern(s))
+ if not errors:
+ if keyword_patterns:
+ keyword_patterns_error = s.position()
+ else:
+ with tentatively_scan(s) as errors:
+ keyword_patterns.append(p_keyword_pattern(s))
+ if s.sy != ",":
+ break
+ s.next()
+ if s.sy == ")":
+ break # Allow trailing comma.
+ s.expect(")")
+
+ if keyword_patterns_error is not None:
+ return Nodes.ErrorNode(
+ keyword_patterns_error,
+ what="Positional patterns follow keyword patterns"
+ )
+ return MatchCaseNodes.ClassPatternNode(
+ pos, class_ = class_,
+ positional_patterns = positional_patterns,
+ keyword_pattern_names = [kv[0] for kv in keyword_patterns],
+ keyword_pattern_patterns = [kv[1] for kv in keyword_patterns],
+ )
+
+
+def p_keyword_pattern(s):
+ if s.sy != "IDENT":
+ s.error("Expected identifier")
+ arg = p_name(s, s.systring)
+ s.next()
+ s.expect("=")
+ value = p_pattern(s)
+ return arg, value
+
+
+def p_pattern_capture_target(s):
+ # any name but '_', and with some constraints on what follows
+ if s.sy != 'IDENT':
+ s.error("Expected identifier")
+ if s.systring == '_':
+ s.error("Pattern capture target cannot be '_'")
+ target = p_name(s, s.systring)
+ s.next()
+ if s.sy in ['.', '(', '=']:
+ s.error("Illegal next symbol '%s'" % s.sy)
+ return target
+
+
+
#----------------------------------------------
#
# Debugging
diff --git a/Cython/TestUtils.py b/Cython/TestUtils.py
index 8bcd26b6f..45a8e6f59 100644
--- a/Cython/TestUtils.py
+++ b/Cython/TestUtils.py
@@ -12,9 +12,10 @@ from functools import partial
from .Compiler import Errors
from .CodeWriter import CodeWriter
-from .Compiler.TreeFragment import TreeFragment, strip_common_indent
+from .Compiler.TreeFragment import TreeFragment, strip_common_indent, StringParseContext
from .Compiler.Visitor import TreeVisitor, VisitorTransform
from .Compiler import TreePath
+from .Compiler.ParseTreeTransforms import PostParse
class NodeTypeWriter(TreeVisitor):
@@ -357,3 +358,24 @@ def write_newer_file(file_path, newer_than, content, dedent=False, encoding=None
while other_time is None or other_time >= os.path.getmtime(file_path):
write_file(file_path, content, dedent=dedent, encoding=encoding)
+
+
+def py_parse_code(code):
+ """
+ Compiles code far enough to get errors from the parser and post-parse stage.
+
+ Is useful for checking for syntax errors, however it doesn't generate runable
+ code.
+ """
+ context = StringParseContext("test")
+ # all the errors we care about are in the parsing or postparse stage
+ try:
+ with Errors.local_errors() as errors:
+ result = TreeFragment(code, pipeline=[PostParse(context)])
+ result = result.substitute()
+ if errors:
+ raise errors[0] # compile error, which should get caught
+ else:
+ return result
+ except Errors.CompileError as e:
+ raise SyntaxError(e.message_only)
diff --git a/Tools/ci-run.sh b/Tools/ci-run.sh
index f25041415..0fde602fd 100644
--- a/Tools/ci-run.sh
+++ b/Tools/ci-run.sh
@@ -78,6 +78,8 @@ else
python -m pip install -r test-requirements.txt || exit 1
if [[ $PYTHON_VERSION != "pypy"* && $PYTHON_VERSION != "3."[1]* ]]; then
python -m pip install -r test-requirements-cpython.txt || exit 1
+ elif [[ $PYTHON_VERSION == "pypy-2.7" ]]; then
+ python -m pip install -r test-requirements-pypy27.txt || exit 1
fi
fi
fi
diff --git a/test-requirements-pypy27.txt b/test-requirements-pypy27.txt
index 9f9505240..6d4f83bca 100644
--- a/test-requirements-pypy27.txt
+++ b/test-requirements-pypy27.txt
@@ -1,2 +1,3 @@
-r test-requirements.txt
+enum34==1.1.10
mock==3.0.5
diff --git a/tests/run/extra_patma.pyx b/tests/run/extra_patma.pyx
new file mode 100644
index 000000000..b2303f45b
--- /dev/null
+++ b/tests/run/extra_patma.pyx
@@ -0,0 +1,18 @@
+# mode: run
+
+cdef bint is_null(int* x):
+ return False # disabled - currently just a parser test
+ match x:
+ case NULL:
+ return True
+ case _:
+ return False
+
+def test_is_null():
+ """
+ >>> test_is_null()
+ """
+ cdef int some_int = 1
+ return # disabled - currently just a parser test
+ assert is_null(&some_int) == False
+ assert is_null(NULL) == True
diff --git a/tests/run/test_patma.py b/tests/run/test_patma.py
new file mode 100644
index 000000000..e55827f35
--- /dev/null
+++ b/tests/run/test_patma.py
@@ -0,0 +1,3454 @@
+### COPIED FROM CPython 3.12 alpha (July 2022)
+### Original part after ############
+# cython: language_level=3
+
+# new code
+import cython
+from Cython.TestUtils import py_parse_code
+
+
+if cython.compiled:
+ def compile(code, name, what):
+ assert what == 'exec'
+ py_parse_code(code)
+
+
+def disable(func):
+ pass
+
+
+############## SLIGHTLY MODIFIED ORIGINAL CODE
+import array
+import collections
+import enum
+import inspect
+import sys
+import unittest
+
+if sys.version_info > (3, 10):
+ import dataclasses
+ @dataclasses.dataclass
+ class Point:
+ x: int
+ y: int
+else:
+ # predates dataclasses with match args
+ class Point:
+ __match_args__ = ("x", "y")
+ x: int
+ y: int
+
+# TestCompiler removed - it's very CPython-specific
+# TestTracing also mainly removed - doesn't seem like a core test
+# except for one test that seems misplaced in CPython (which is below)
+
+class TestTracing(unittest.TestCase):
+ if sys.version_info < (3, 4):
+ class SubTestClass(object):
+ def __enter__(self):
+ return self
+ def __exit__(self, exc_type, exc_value, traceback):
+ return
+ def __call__(self, *args):
+ return self
+ subTest = SubTestClass()
+
+ def test_parser_deeply_nested_patterns(self):
+ # Deeply nested patterns can cause exponential backtracking when parsing.
+ # See CPython gh-93671 for more information.
+ #
+ # DW: Cython note - this doesn't break the parser but may cause a
+ # RecursionError later in the code-generation. I don't believe that's
+ # easily avoidable with the way Cython visitors currently work
+
+ levels = 100
+
+ patterns = [
+ "A" + "(" * levels + ")" * levels,
+ "{1:" * levels + "1" + "}" * levels,
+ "[" * levels + "1" + "]" * levels,
+ ]
+
+ for pattern in patterns:
+ with self.subTest(pattern):
+ code = inspect.cleandoc("""
+ match None:
+ case {}:
+ pass
+ """.format(pattern))
+ compile(code, "<string>", "exec")
+
+
+# FIXME - remove all the "return"s added to cause code to be dropped
+############## ORIGINAL PART FROM CPYTHON
+
+
+class TestInheritance(unittest.TestCase):
+
+ @staticmethod
+ def check_sequence_then_mapping(x):
+ return # disabled
+ match x:
+ case [*_]:
+ return "seq"
+ case {}:
+ return "map"
+
+ @staticmethod
+ def check_mapping_then_sequence(x):
+ return # disabled
+ match x:
+ case {}:
+ return "map"
+ case [*_]:
+ return "seq"
+
+ def test_multiple_inheritance_mapping(self):
+ return # disabled
+ class C:
+ pass
+ class M1(collections.UserDict, collections.abc.Sequence):
+ pass
+ class M2(C, collections.UserDict, collections.abc.Sequence):
+ pass
+ class M3(collections.UserDict, C, list):
+ pass
+ class M4(dict, collections.abc.Sequence, C):
+ pass
+ self.assertEqual(self.check_sequence_then_mapping(M1()), "map")
+ self.assertEqual(self.check_sequence_then_mapping(M2()), "map")
+ self.assertEqual(self.check_sequence_then_mapping(M3()), "map")
+ self.assertEqual(self.check_sequence_then_mapping(M4()), "map")
+ self.assertEqual(self.check_mapping_then_sequence(M1()), "map")
+ self.assertEqual(self.check_mapping_then_sequence(M2()), "map")
+ self.assertEqual(self.check_mapping_then_sequence(M3()), "map")
+ self.assertEqual(self.check_mapping_then_sequence(M4()), "map")
+
+ def test_multiple_inheritance_sequence(self):
+ return # disabled
+ class C:
+ pass
+ class S1(collections.UserList, collections.abc.Mapping):
+ pass
+ class S2(C, collections.UserList, collections.abc.Mapping):
+ pass
+ class S3(list, C, collections.abc.Mapping):
+ pass
+ class S4(collections.UserList, dict, C):
+ pass
+ self.assertEqual(self.check_sequence_then_mapping(S1()), "seq")
+ self.assertEqual(self.check_sequence_then_mapping(S2()), "seq")
+ self.assertEqual(self.check_sequence_then_mapping(S3()), "seq")
+ self.assertEqual(self.check_sequence_then_mapping(S4()), "seq")
+ self.assertEqual(self.check_mapping_then_sequence(S1()), "seq")
+ self.assertEqual(self.check_mapping_then_sequence(S2()), "seq")
+ self.assertEqual(self.check_mapping_then_sequence(S3()), "seq")
+ self.assertEqual(self.check_mapping_then_sequence(S4()), "seq")
+
+ def test_late_registration_mapping(self):
+ return # disabled
+ class Parent:
+ pass
+ class ChildPre(Parent):
+ pass
+ class GrandchildPre(ChildPre):
+ pass
+ collections.abc.Mapping.register(Parent)
+ class ChildPost(Parent):
+ pass
+ class GrandchildPost(ChildPost):
+ pass
+ self.assertEqual(self.check_sequence_then_mapping(Parent()), "map")
+ self.assertEqual(self.check_sequence_then_mapping(ChildPre()), "map")
+ self.assertEqual(self.check_sequence_then_mapping(GrandchildPre()), "map")
+ self.assertEqual(self.check_sequence_then_mapping(ChildPost()), "map")
+ self.assertEqual(self.check_sequence_then_mapping(GrandchildPost()), "map")
+ self.assertEqual(self.check_mapping_then_sequence(Parent()), "map")
+ self.assertEqual(self.check_mapping_then_sequence(ChildPre()), "map")
+ self.assertEqual(self.check_mapping_then_sequence(GrandchildPre()), "map")
+ self.assertEqual(self.check_mapping_then_sequence(ChildPost()), "map")
+ self.assertEqual(self.check_mapping_then_sequence(GrandchildPost()), "map")
+
+ def test_late_registration_sequence(self):
+ return # disabled
+ class Parent:
+ pass
+ class ChildPre(Parent):
+ pass
+ class GrandchildPre(ChildPre):
+ pass
+ collections.abc.Sequence.register(Parent)
+ class ChildPost(Parent):
+ pass
+ class GrandchildPost(ChildPost):
+ pass
+ self.assertEqual(self.check_sequence_then_mapping(Parent()), "seq")
+ self.assertEqual(self.check_sequence_then_mapping(ChildPre()), "seq")
+ self.assertEqual(self.check_sequence_then_mapping(GrandchildPre()), "seq")
+ self.assertEqual(self.check_sequence_then_mapping(ChildPost()), "seq")
+ self.assertEqual(self.check_sequence_then_mapping(GrandchildPost()), "seq")
+ self.assertEqual(self.check_mapping_then_sequence(Parent()), "seq")
+ self.assertEqual(self.check_mapping_then_sequence(ChildPre()), "seq")
+ self.assertEqual(self.check_mapping_then_sequence(GrandchildPre()), "seq")
+ self.assertEqual(self.check_mapping_then_sequence(ChildPost()), "seq")
+ self.assertEqual(self.check_mapping_then_sequence(GrandchildPost()), "seq")
+
+
+class TestPatma(unittest.TestCase):
+
+ def test_patma_000(self):
+ return # disabled
+ match 0:
+ case 0:
+ x = True
+ self.assertIs(x, True)
+
+ def test_patma_001(self):
+ return # disabled
+ match 0:
+ case 0 if False:
+ x = False
+ case 0 if True:
+ x = True
+ self.assertIs(x, True)
+
+ def test_patma_002(self):
+ return # disabled
+ match 0:
+ case 0:
+ x = True
+ case 0:
+ x = False
+ self.assertIs(x, True)
+
+ def test_patma_003(self):
+ return # disabled
+ x = False
+ match 0:
+ case 0 | 1 | 2 | 3:
+ x = True
+ self.assertIs(x, True)
+
+ def test_patma_004(self):
+ return # disabled
+ x = False
+ match 1:
+ case 0 | 1 | 2 | 3:
+ x = True
+ self.assertIs(x, True)
+
+ def test_patma_005(self):
+ return # disabled
+ x = False
+ match 2:
+ case 0 | 1 | 2 | 3:
+ x = True
+ self.assertIs(x, True)
+
+ def test_patma_006(self):
+ return # disabled
+ x = False
+ match 3:
+ case 0 | 1 | 2 | 3:
+ x = True
+ self.assertIs(x, True)
+
+ def test_patma_007(self):
+ return # disabled
+ x = False
+ match 4:
+ case 0 | 1 | 2 | 3:
+ x = True
+ self.assertIs(x, False)
+
+ def test_patma_008(self):
+ return # disabled
+ x = 0
+ class A:
+ y = 1
+ match x:
+ case A.y as z:
+ pass
+ self.assertEqual(x, 0)
+ self.assertEqual(A.y, 1)
+
+ def test_patma_009(self):
+ return # disabled
+ class A:
+ B = 0
+ match 0:
+ case x if x:
+ z = 0
+ case _ as y if y == x and y:
+ z = 1
+ case A.B:
+ z = 2
+ self.assertEqual(A.B, 0)
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 0)
+ self.assertEqual(z, 2)
+
+ def test_patma_010(self):
+ return # disabled
+ match ():
+ case []:
+ x = 0
+ self.assertEqual(x, 0)
+
+ def test_patma_011(self):
+ return # disabled
+ match (0, 1, 2):
+ case [*x]:
+ y = 0
+ self.assertEqual(x, [0, 1, 2])
+ self.assertEqual(y, 0)
+
+ def test_patma_012(self):
+ return # disabled
+ match (0, 1, 2):
+ case [0, *x]:
+ y = 0
+ self.assertEqual(x, [1, 2])
+ self.assertEqual(y, 0)
+
+ def test_patma_013(self):
+ return # disabled
+ match (0, 1, 2):
+ case [0, 1, *x,]:
+ y = 0
+ self.assertEqual(x, [2])
+ self.assertEqual(y, 0)
+
+ def test_patma_014(self):
+ return # disabled
+ match (0, 1, 2):
+ case [0, 1, 2, *x]:
+ y = 0
+ self.assertEqual(x, [])
+ self.assertEqual(y, 0)
+
+ def test_patma_015(self):
+ return # disabled
+ match (0, 1, 2):
+ case [*x, 2,]:
+ y = 0
+ self.assertEqual(x, [0, 1])
+ self.assertEqual(y, 0)
+
+ def test_patma_016(self):
+ return # disabled
+ match (0, 1, 2):
+ case [*x, 1, 2]:
+ y = 0
+ self.assertEqual(x, [0])
+ self.assertEqual(y, 0)
+
+ def test_patma_017(self):
+ return # disabled
+ match (0, 1, 2):
+ case [*x, 0, 1, 2,]:
+ y = 0
+ self.assertEqual(x, [])
+ self.assertEqual(y, 0)
+
+ def test_patma_018(self):
+ return # disabled
+ match (0, 1, 2):
+ case [0, *x, 2]:
+ y = 0
+ self.assertEqual(x, [1])
+ self.assertEqual(y, 0)
+
+ def test_patma_019(self):
+ return # disabled
+ match (0, 1, 2):
+ case [0, 1, *x, 2,]:
+ y = 0
+ self.assertEqual(x, [])
+ self.assertEqual(y, 0)
+
+ def test_patma_020(self):
+ return # disabled
+ match (0, 1, 2):
+ case [0, *x, 1, 2]:
+ y = 0
+ self.assertEqual(x, [])
+ self.assertEqual(y, 0)
+
+ def test_patma_021(self):
+ return # disabled
+ match (0, 1, 2):
+ case [*x,]:
+ y = 0
+ self.assertEqual(x, [0, 1, 2])
+ self.assertEqual(y, 0)
+
+ def test_patma_022(self):
+ return # disabled
+ x = {}
+ match x:
+ case {}:
+ y = 0
+ self.assertEqual(x, {})
+ self.assertEqual(y, 0)
+
+ def test_patma_023(self):
+ return # disabled
+ x = {0: 0}
+ match x:
+ case {}:
+ y = 0
+ self.assertEqual(x, {0: 0})
+ self.assertEqual(y, 0)
+
+ def test_patma_024(self):
+ return # disabled
+ x = {}
+ y = None
+ match x:
+ case {0: 0}:
+ y = 0
+ self.assertEqual(x, {})
+ self.assertIs(y, None)
+
+ def test_patma_025(self):
+ return # disabled
+ x = {0: 0}
+ match x:
+ case {0: (0 | 1 | 2 as z)}:
+ y = 0
+ self.assertEqual(x, {0: 0})
+ self.assertEqual(y, 0)
+ self.assertEqual(z, 0)
+
+ def test_patma_026(self):
+ return # disabled
+ x = {0: 1}
+ match x:
+ case {0: (0 | 1 | 2 as z)}:
+ y = 0
+ self.assertEqual(x, {0: 1})
+ self.assertEqual(y, 0)
+ self.assertEqual(z, 1)
+
+ def test_patma_027(self):
+ return # disabled
+ x = {0: 2}
+ match x:
+ case {0: (0 | 1 | 2 as z)}:
+ y = 0
+ self.assertEqual(x, {0: 2})
+ self.assertEqual(y, 0)
+ self.assertEqual(z, 2)
+
+ def test_patma_028(self):
+ return # disabled
+ x = {0: 3}
+ y = None
+ match x:
+ case {0: (0 | 1 | 2 as z)}:
+ y = 0
+ self.assertEqual(x, {0: 3})
+ self.assertIs(y, None)
+
+ def test_patma_029(self):
+ return # disabled
+ x = {}
+ y = None
+ match x:
+ case {0: [1, 2, {}]}:
+ y = 0
+ case {0: [1, 2, {}], 1: [[]]}:
+ y = 1
+ case []:
+ y = 2
+ self.assertEqual(x, {})
+ self.assertIs(y, None)
+
+ def test_patma_030(self):
+ return # disabled
+ x = {False: (True, 2.0, {})}
+ match x:
+ case {0: [1, 2, {}]}:
+ y = 0
+ case {0: [1, 2, {}], 1: [[]]}:
+ y = 1
+ case []:
+ y = 2
+ self.assertEqual(x, {False: (True, 2.0, {})})
+ self.assertEqual(y, 0)
+
+ def test_patma_031(self):
+ return # disabled
+ x = {False: (True, 2.0, {}), 1: [[]], 2: 0}
+ match x:
+ case {0: [1, 2, {}]}:
+ y = 0
+ case {0: [1, 2, {}], 1: [[]]}:
+ y = 1
+ case []:
+ y = 2
+ self.assertEqual(x, {False: (True, 2.0, {}), 1: [[]], 2: 0})
+ self.assertEqual(y, 0)
+
+ def test_patma_032(self):
+ return # disabled
+ x = {False: (True, 2.0, {}), 1: [[]], 2: 0}
+ match x:
+ case {0: [1, 2]}:
+ y = 0
+ case {0: [1, 2, {}], 1: [[]]}:
+ y = 1
+ case []:
+ y = 2
+ self.assertEqual(x, {False: (True, 2.0, {}), 1: [[]], 2: 0})
+ self.assertEqual(y, 1)
+
+ def test_patma_033(self):
+ return # disabled
+ x = []
+ match x:
+ case {0: [1, 2, {}]}:
+ y = 0
+ case {0: [1, 2, {}], 1: [[]]}:
+ y = 1
+ case []:
+ y = 2
+ self.assertEqual(x, [])
+ self.assertEqual(y, 2)
+
+ def test_patma_034(self):
+ return # disabled
+ x = {0: 0}
+ match x:
+ case {0: [1, 2, {}]}:
+ y = 0
+ case {0: ([1, 2, {}] | False)} | {1: [[]]} | {0: [1, 2, {}]} | [] | "X" | {}:
+ y = 1
+ case []:
+ y = 2
+ self.assertEqual(x, {0: 0})
+ self.assertEqual(y, 1)
+
+ def test_patma_035(self):
+ return # disabled
+ x = {0: 0}
+ match x:
+ case {0: [1, 2, {}]}:
+ y = 0
+ case {0: [1, 2, {}] | True} | {1: [[]]} | {0: [1, 2, {}]} | [] | "X" | {}:
+ y = 1
+ case []:
+ y = 2
+ self.assertEqual(x, {0: 0})
+ self.assertEqual(y, 1)
+
+ def test_patma_036(self):
+ return # disabled
+ x = 0
+ match x:
+ case 0 | 1 | 2:
+ y = 0
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 0)
+
+ def test_patma_037(self):
+ return # disabled
+ x = 1
+ match x:
+ case 0 | 1 | 2:
+ y = 0
+ self.assertEqual(x, 1)
+ self.assertEqual(y, 0)
+
+ def test_patma_038(self):
+ return # disabled
+ x = 2
+ match x:
+ case 0 | 1 | 2:
+ y = 0
+ self.assertEqual(x, 2)
+ self.assertEqual(y, 0)
+
+ def test_patma_039(self):
+ return # disabled
+ x = 3
+ y = None
+ match x:
+ case 0 | 1 | 2:
+ y = 0
+ self.assertEqual(x, 3)
+ self.assertIs(y, None)
+
+ def test_patma_040(self):
+ return # disabled
+ x = 0
+ match x:
+ case (0 as z) | (1 as z) | (2 as z) if z == x % 2:
+ y = 0
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 0)
+ self.assertEqual(z, 0)
+
+ def test_patma_041(self):
+ return # disabled
+ x = 1
+ match x:
+ case (0 as z) | (1 as z) | (2 as z) if z == x % 2:
+ y = 0
+ self.assertEqual(x, 1)
+ self.assertEqual(y, 0)
+ self.assertEqual(z, 1)
+
+ def test_patma_042(self):
+ return # disabled
+ x = 2
+ y = None
+ match x:
+ case (0 as z) | (1 as z) | (2 as z) if z == x % 2:
+ y = 0
+ self.assertEqual(x, 2)
+ self.assertIs(y, None)
+ self.assertEqual(z, 2)
+
+ def test_patma_043(self):
+ return # disabled
+ x = 3
+ y = None
+ match x:
+ case (0 as z) | (1 as z) | (2 as z) if z == x % 2:
+ y = 0
+ self.assertEqual(x, 3)
+ self.assertIs(y, None)
+
+ def test_patma_044(self):
+ return # disabled
+ x = ()
+ match x:
+ case []:
+ y = 0
+ self.assertEqual(x, ())
+ self.assertEqual(y, 0)
+
+ def test_patma_045(self):
+ return # disabled
+ x = ()
+ match x:
+ case ():
+ y = 0
+ self.assertEqual(x, ())
+ self.assertEqual(y, 0)
+
+ def test_patma_046(self):
+ return # disabled
+ x = (0,)
+ match x:
+ case [0]:
+ y = 0
+ self.assertEqual(x, (0,))
+ self.assertEqual(y, 0)
+
+ def test_patma_047(self):
+ return # disabled
+ x = ((),)
+ match x:
+ case [[]]:
+ y = 0
+ self.assertEqual(x, ((),))
+ self.assertEqual(y, 0)
+
+ def test_patma_048(self):
+ return # disabled
+ x = [0, 1]
+ match x:
+ case [0, 1] | [1, 0]:
+ y = 0
+ self.assertEqual(x, [0, 1])
+ self.assertEqual(y, 0)
+
+ def test_patma_049(self):
+ return # disabled
+ x = [1, 0]
+ match x:
+ case [0, 1] | [1, 0]:
+ y = 0
+ self.assertEqual(x, [1, 0])
+ self.assertEqual(y, 0)
+
+ def test_patma_050(self):
+ return # disabled
+ x = [0, 0]
+ y = None
+ match x:
+ case [0, 1] | [1, 0]:
+ y = 0
+ self.assertEqual(x, [0, 0])
+ self.assertIs(y, None)
+
+ def test_patma_051(self):
+ return # disabled
+ w = None
+ x = [1, 0]
+ match x:
+ case [(0 as w)]:
+ y = 0
+ case [z] | [1, (0 | 1 as z)] | [z]:
+ y = 1
+ self.assertIs(w, None)
+ self.assertEqual(x, [1, 0])
+ self.assertEqual(y, 1)
+ self.assertEqual(z, 0)
+
+ def test_patma_052(self):
+ return # disabled
+ x = [1, 0]
+ match x:
+ case [0]:
+ y = 0
+ case [1, 0] if (x := x[:0]):
+ y = 1
+ case [1, 0]:
+ y = 2
+ self.assertEqual(x, [])
+ self.assertEqual(y, 2)
+
+ def test_patma_053(self):
+ return # disabled
+ x = {0}
+ y = None
+ match x:
+ case [0]:
+ y = 0
+ self.assertEqual(x, {0})
+ self.assertIs(y, None)
+
+ def test_patma_054(self):
+ return # disabled
+ x = set()
+ y = None
+ match x:
+ case []:
+ y = 0
+ self.assertEqual(x, set())
+ self.assertIs(y, None)
+
+ def test_patma_055(self):
+ return # disabled
+ x = iter([1, 2, 3])
+ y = None
+ match x:
+ case []:
+ y = 0
+ self.assertEqual([*x], [1, 2, 3])
+ self.assertIs(y, None)
+
+ def test_patma_056(self):
+ return # disabled
+ x = {}
+ y = None
+ match x:
+ case []:
+ y = 0
+ self.assertEqual(x, {})
+ self.assertIs(y, None)
+
+ def test_patma_057(self):
+ return # disabled
+ x = {0: False, 1: True}
+ y = None
+ match x:
+ case [0, 1]:
+ y = 0
+ self.assertEqual(x, {0: False, 1: True})
+ self.assertIs(y, None)
+
+ def test_patma_058(self):
+ return # disabled
+ x = 0
+ match x:
+ case 0:
+ y = 0
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 0)
+
+ def test_patma_059(self):
+ return # disabled
+ x = 0
+ y = None
+ match x:
+ case False:
+ y = 0
+ self.assertEqual(x, 0)
+ self.assertEqual(y, None)
+
+ def test_patma_060(self):
+ return # disabled
+ x = 0
+ y = None
+ match x:
+ case 1:
+ y = 0
+ self.assertEqual(x, 0)
+ self.assertIs(y, None)
+
+ def test_patma_061(self):
+ return # disabled
+ x = 0
+ y = None
+ match x:
+ case None:
+ y = 0
+ self.assertEqual(x, 0)
+ self.assertIs(y, None)
+
+ def test_patma_062(self):
+ return # disabled
+ x = 0
+ match x:
+ case 0:
+ y = 0
+ case 0:
+ y = 1
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 0)
+
+ def test_patma_063(self):
+ return # disabled
+ x = 0
+ y = None
+ match x:
+ case 1:
+ y = 0
+ case 1:
+ y = 1
+ self.assertEqual(x, 0)
+ self.assertIs(y, None)
+
+ def test_patma_064(self):
+ return # disabled
+ x = "x"
+ match x:
+ case "x":
+ y = 0
+ case "y":
+ y = 1
+ self.assertEqual(x, "x")
+ self.assertEqual(y, 0)
+
+ def test_patma_065(self):
+ return # disabled
+ x = "x"
+ match x:
+ case "y":
+ y = 0
+ case "x":
+ y = 1
+ self.assertEqual(x, "x")
+ self.assertEqual(y, 1)
+
+ def test_patma_066(self):
+ return # disabled
+ x = "x"
+ match x:
+ case "":
+ y = 0
+ case "x":
+ y = 1
+ self.assertEqual(x, "x")
+ self.assertEqual(y, 1)
+
+ def test_patma_067(self):
+ return # disabled
+ x = b"x"
+ match x:
+ case b"y":
+ y = 0
+ case b"x":
+ y = 1
+ self.assertEqual(x, b"x")
+ self.assertEqual(y, 1)
+
+ def test_patma_068(self):
+ return # disabled
+ x = 0
+ match x:
+ case 0 if False:
+ y = 0
+ case 0:
+ y = 1
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 1)
+
+ def test_patma_069(self):
+ return # disabled
+ x = 0
+ y = None
+ match x:
+ case 0 if 0:
+ y = 0
+ case 0 if 0:
+ y = 1
+ self.assertEqual(x, 0)
+ self.assertIs(y, None)
+
+ def test_patma_070(self):
+ return # disabled
+ x = 0
+ match x:
+ case 0 if True:
+ y = 0
+ case 0 if True:
+ y = 1
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 0)
+
+ def test_patma_071(self):
+ return # disabled
+ x = 0
+ match x:
+ case 0 if 1:
+ y = 0
+ case 0 if 1:
+ y = 1
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 0)
+
+ def test_patma_072(self):
+ return # disabled
+ x = 0
+ match x:
+ case 0 if True:
+ y = 0
+ case 0 if True:
+ y = 1
+ y = 2
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 2)
+
+ def test_patma_073(self):
+ return # disabled
+ x = 0
+ match x:
+ case 0 if 0:
+ y = 0
+ case 0 if 1:
+ y = 1
+ y = 2
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 2)
+
+ def test_patma_074(self):
+ return # disabled
+ x = 0
+ y = None
+ match x:
+ case 0 if not (x := 1):
+ y = 0
+ case 1:
+ y = 1
+ self.assertEqual(x, 1)
+ self.assertIs(y, None)
+
+ def test_patma_075(self):
+ return # disabled
+ x = "x"
+ match x:
+ case ["x"]:
+ y = 0
+ case "x":
+ y = 1
+ self.assertEqual(x, "x")
+ self.assertEqual(y, 1)
+
+ def test_patma_076(self):
+ return # disabled
+ x = b"x"
+ match x:
+ case [b"x"]:
+ y = 0
+ case ["x"]:
+ y = 1
+ case [120]:
+ y = 2
+ case b"x":
+ y = 4
+ self.assertEqual(x, b"x")
+ self.assertEqual(y, 4)
+
+ def test_patma_077(self):
+ return # disabled
+ x = bytearray(b"x")
+ y = None
+ match x:
+ case [120]:
+ y = 0
+ case 120:
+ y = 1
+ self.assertEqual(x, b"x")
+ self.assertIs(y, None)
+
+ def test_patma_078(self):
+ return # disabled
+ x = ""
+ match x:
+ case []:
+ y = 0
+ case [""]:
+ y = 1
+ case "":
+ y = 2
+ self.assertEqual(x, "")
+ self.assertEqual(y, 2)
+
+ def test_patma_079(self):
+ return # disabled
+ x = "xxx"
+ match x:
+ case ["x", "x", "x"]:
+ y = 0
+ case ["xxx"]:
+ y = 1
+ case "xxx":
+ y = 2
+ self.assertEqual(x, "xxx")
+ self.assertEqual(y, 2)
+
+ def test_patma_080(self):
+ return # disabled
+ x = b"xxx"
+ match x:
+ case [120, 120, 120]:
+ y = 0
+ case [b"xxx"]:
+ y = 1
+ case b"xxx":
+ y = 2
+ self.assertEqual(x, b"xxx")
+ self.assertEqual(y, 2)
+
+ def test_patma_081(self):
+ return # disabled
+ x = 0
+ match x:
+ case 0 if not (x := 1):
+ y = 0
+ case (0 as z):
+ y = 1
+ self.assertEqual(x, 1)
+ self.assertEqual(y, 1)
+ self.assertEqual(z, 0)
+
+ def test_patma_082(self):
+ return # disabled
+ x = 0
+ match x:
+ case (1 as z) if not (x := 1):
+ y = 0
+ case 0:
+ y = 1
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 1)
+
+ def test_patma_083(self):
+ return # disabled
+ x = 0
+ match x:
+ case (0 as z):
+ y = 0
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 0)
+ self.assertEqual(z, 0)
+
+ def test_patma_084(self):
+ return # disabled
+ x = 0
+ y = None
+ match x:
+ case (1 as z):
+ y = 0
+ self.assertEqual(x, 0)
+ self.assertIs(y, None)
+
+ def test_patma_085(self):
+ return # disabled
+ x = 0
+ y = None
+ match x:
+ case (0 as z) if (w := 0):
+ y = 0
+ self.assertEqual(w, 0)
+ self.assertEqual(x, 0)
+ self.assertIs(y, None)
+ self.assertEqual(z, 0)
+
+ def test_patma_086(self):
+ return # disabled
+ x = 0
+ match x:
+ case ((0 as w) as z):
+ y = 0
+ self.assertEqual(w, 0)
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 0)
+ self.assertEqual(z, 0)
+
+ def test_patma_087(self):
+ return # disabled
+ x = 0
+ match x:
+ case (0 | 1) | 2:
+ y = 0
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 0)
+
+ def test_patma_088(self):
+ return # disabled
+ x = 1
+ match x:
+ case (0 | 1) | 2:
+ y = 0
+ self.assertEqual(x, 1)
+ self.assertEqual(y, 0)
+
+ def test_patma_089(self):
+ return # disabled
+ x = 2
+ match x:
+ case (0 | 1) | 2:
+ y = 0
+ self.assertEqual(x, 2)
+ self.assertEqual(y, 0)
+
+ def test_patma_090(self):
+ return # disabled
+ x = 3
+ y = None
+ match x:
+ case (0 | 1) | 2:
+ y = 0
+ self.assertEqual(x, 3)
+ self.assertIs(y, None)
+
+ def test_patma_091(self):
+ return # disabled
+ x = 0
+ match x:
+ case 0 | (1 | 2):
+ y = 0
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 0)
+
+ def test_patma_092(self):
+ return # disabled
+ x = 1
+ match x:
+ case 0 | (1 | 2):
+ y = 0
+ self.assertEqual(x, 1)
+ self.assertEqual(y, 0)
+
+ def test_patma_093(self):
+ return # disabled
+ x = 2
+ match x:
+ case 0 | (1 | 2):
+ y = 0
+ self.assertEqual(x, 2)
+ self.assertEqual(y, 0)
+
+ def test_patma_094(self):
+ return # disabled
+ x = 3
+ y = None
+ match x:
+ case 0 | (1 | 2):
+ y = 0
+ self.assertEqual(x, 3)
+ self.assertIs(y, None)
+
+ def test_patma_095(self):
+ return # disabled
+ x = 0
+ match x:
+ case -0:
+ y = 0
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 0)
+
+ def test_patma_096(self):
+ return # disabled
+ x = 0
+ match x:
+ case -0.0:
+ y = 0
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 0)
+
+ def test_patma_097(self):
+ return # disabled
+ x = 0
+ match x:
+ case -0j:
+ y = 0
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 0)
+
+ def test_patma_098(self):
+ return # disabled
+ x = 0
+ match x:
+ case -0.0j:
+ y = 0
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 0)
+
+ def test_patma_099(self):
+ return # disabled
+ x = -1
+ match x:
+ case -1:
+ y = 0
+ self.assertEqual(x, -1)
+ self.assertEqual(y, 0)
+
+ def test_patma_100(self):
+ return # disabled
+ x = -1.5
+ match x:
+ case -1.5:
+ y = 0
+ self.assertEqual(x, -1.5)
+ self.assertEqual(y, 0)
+
+ def test_patma_101(self):
+ return # disabled
+ x = -1j
+ match x:
+ case -1j:
+ y = 0
+ self.assertEqual(x, -1j)
+ self.assertEqual(y, 0)
+
+ def test_patma_102(self):
+ return # disabled
+ x = -1.5j
+ match x:
+ case -1.5j:
+ y = 0
+ self.assertEqual(x, -1.5j)
+ self.assertEqual(y, 0)
+
+ def test_patma_103(self):
+ return # disabled
+ x = 0
+ match x:
+ case 0 + 0j:
+ y = 0
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 0)
+
+ def test_patma_104(self):
+ return # disabled
+ x = 0
+ match x:
+ case 0 - 0j:
+ y = 0
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 0)
+
+ def test_patma_105(self):
+ return # disabled
+ x = 0
+ match x:
+ case -0 + 0j:
+ y = 0
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 0)
+
+ def test_patma_106(self):
+ return # disabled
+ x = 0
+ match x:
+ case -0 - 0j:
+ y = 0
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 0)
+
+ def test_patma_107(self):
+ return # disabled
+ x = 0.25 + 1.75j
+ match x:
+ case 0.25 + 1.75j:
+ y = 0
+ self.assertEqual(x, 0.25 + 1.75j)
+ self.assertEqual(y, 0)
+
+ def test_patma_108(self):
+ return # disabled
+ x = 0.25 - 1.75j
+ match x:
+ case 0.25 - 1.75j:
+ y = 0
+ self.assertEqual(x, 0.25 - 1.75j)
+ self.assertEqual(y, 0)
+
+ def test_patma_109(self):
+ return # disabled
+ x = -0.25 + 1.75j
+ match x:
+ case -0.25 + 1.75j:
+ y = 0
+ self.assertEqual(x, -0.25 + 1.75j)
+ self.assertEqual(y, 0)
+
+ def test_patma_110(self):
+ return # disabled
+ x = -0.25 - 1.75j
+ match x:
+ case -0.25 - 1.75j:
+ y = 0
+ self.assertEqual(x, -0.25 - 1.75j)
+ self.assertEqual(y, 0)
+
+ def test_patma_111(self):
+ return # disabled
+ class A:
+ B = 0
+ x = 0
+ match x:
+ case A.B:
+ y = 0
+ self.assertEqual(A.B, 0)
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 0)
+
+ def test_patma_112(self):
+ return # disabled
+ class A:
+ class B:
+ C = 0
+ x = 0
+ match x:
+ case A.B.C:
+ y = 0
+ self.assertEqual(A.B.C, 0)
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 0)
+
+ def test_patma_113(self):
+ return # disabled
+ class A:
+ class B:
+ C = 0
+ D = 1
+ x = 1
+ match x:
+ case A.B.C:
+ y = 0
+ case A.B.D:
+ y = 1
+ self.assertEqual(A.B.C, 0)
+ self.assertEqual(A.B.D, 1)
+ self.assertEqual(x, 1)
+ self.assertEqual(y, 1)
+
+ def test_patma_114(self):
+ return # disabled
+ class A:
+ class B:
+ class C:
+ D = 0
+ x = 0
+ match x:
+ case A.B.C.D:
+ y = 0
+ self.assertEqual(A.B.C.D, 0)
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 0)
+
+ def test_patma_115(self):
+ return # disabled
+ class A:
+ class B:
+ class C:
+ D = 0
+ E = 1
+ x = 1
+ match x:
+ case A.B.C.D:
+ y = 0
+ case A.B.C.E:
+ y = 1
+ self.assertEqual(A.B.C.D, 0)
+ self.assertEqual(A.B.C.E, 1)
+ self.assertEqual(x, 1)
+ self.assertEqual(y, 1)
+
+ def test_patma_116(self):
+ return # disabled
+ match = case = 0
+ match match:
+ case case:
+ x = 0
+ self.assertEqual(match, 0)
+ self.assertEqual(case, 0)
+ self.assertEqual(x, 0)
+
+ def test_patma_117(self):
+ return # disabled
+ match = case = 0
+ match case:
+ case match:
+ x = 0
+ self.assertEqual(match, 0)
+ self.assertEqual(case, 0)
+ self.assertEqual(x, 0)
+
+ def test_patma_118(self):
+ return # disabled
+ x = []
+ match x:
+ case [*_, _]:
+ y = 0
+ case []:
+ y = 1
+ self.assertEqual(x, [])
+ self.assertEqual(y, 1)
+
+ def test_patma_119(self):
+ return # disabled
+ x = collections.defaultdict(int)
+ match x:
+ case {0: 0}:
+ y = 0
+ case {}:
+ y = 1
+ self.assertEqual(x, {})
+ self.assertEqual(y, 1)
+
+ def test_patma_120(self):
+ return # disabled
+ x = collections.defaultdict(int)
+ match x:
+ case {0: 0}:
+ y = 0
+ case {**z}:
+ y = 1
+ self.assertEqual(x, {})
+ self.assertEqual(y, 1)
+ self.assertEqual(z, {})
+
+ def test_patma_121(self):
+ return # disabled
+ match ():
+ case ():
+ x = 0
+ self.assertEqual(x, 0)
+
+ def test_patma_122(self):
+ return # disabled
+ match (0, 1, 2):
+ case (*x,):
+ y = 0
+ self.assertEqual(x, [0, 1, 2])
+ self.assertEqual(y, 0)
+
+ def test_patma_123(self):
+ return # disabled
+ match (0, 1, 2):
+ case 0, *x:
+ y = 0
+ self.assertEqual(x, [1, 2])
+ self.assertEqual(y, 0)
+
+ def test_patma_124(self):
+ return # disabled
+ match (0, 1, 2):
+ case (0, 1, *x,):
+ y = 0
+ self.assertEqual(x, [2])
+ self.assertEqual(y, 0)
+
+ def test_patma_125(self):
+ return # disabled
+ match (0, 1, 2):
+ case 0, 1, 2, *x:
+ y = 0
+ self.assertEqual(x, [])
+ self.assertEqual(y, 0)
+
+ def test_patma_126(self):
+ return # disabled
+ match (0, 1, 2):
+ case *x, 2,:
+ y = 0
+ self.assertEqual(x, [0, 1])
+ self.assertEqual(y, 0)
+
+ def test_patma_127(self):
+ return # disabled
+ match (0, 1, 2):
+ case (*x, 1, 2):
+ y = 0
+ self.assertEqual(x, [0])
+ self.assertEqual(y, 0)
+
+ def test_patma_128(self):
+ return # disabled
+ match (0, 1, 2):
+ case *x, 0, 1, 2,:
+ y = 0
+ self.assertEqual(x, [])
+ self.assertEqual(y, 0)
+
+ def test_patma_129(self):
+ return # disabled
+ match (0, 1, 2):
+ case (0, *x, 2):
+ y = 0
+ self.assertEqual(x, [1])
+ self.assertEqual(y, 0)
+
+ def test_patma_130(self):
+ return # disabled
+ match (0, 1, 2):
+ case 0, 1, *x, 2,:
+ y = 0
+ self.assertEqual(x, [])
+ self.assertEqual(y, 0)
+
+ def test_patma_131(self):
+ return # disabled
+ match (0, 1, 2):
+ case (0, *x, 1, 2):
+ y = 0
+ self.assertEqual(x, [])
+ self.assertEqual(y, 0)
+
+ def test_patma_132(self):
+ return # disabled
+ match (0, 1, 2):
+ case *x,:
+ y = 0
+ self.assertEqual(x, [0, 1, 2])
+ self.assertEqual(y, 0)
+
+ def test_patma_133(self):
+ return # disabled
+ x = collections.defaultdict(int, {0: 1})
+ match x:
+ case {1: 0}:
+ y = 0
+ case {0: 0}:
+ y = 1
+ case {}:
+ y = 2
+ self.assertEqual(x, {0: 1})
+ self.assertEqual(y, 2)
+
+ def test_patma_134(self):
+ return # disabled
+ x = collections.defaultdict(int, {0: 1})
+ match x:
+ case {1: 0}:
+ y = 0
+ case {0: 0}:
+ y = 1
+ case {**z}:
+ y = 2
+ self.assertEqual(x, {0: 1})
+ self.assertEqual(y, 2)
+ self.assertEqual(z, {0: 1})
+
+ def test_patma_135(self):
+ return # disabled
+ x = collections.defaultdict(int, {0: 1})
+ match x:
+ case {1: 0}:
+ y = 0
+ case {0: 0}:
+ y = 1
+ case {0: _, **z}:
+ y = 2
+ self.assertEqual(x, {0: 1})
+ self.assertEqual(y, 2)
+ self.assertEqual(z, {})
+
+ def test_patma_136(self):
+ return # disabled
+ x = {0: 1}
+ match x:
+ case {1: 0}:
+ y = 0
+ case {0: 0}:
+ y = 0
+ case {}:
+ y = 1
+ self.assertEqual(x, {0: 1})
+ self.assertEqual(y, 1)
+
+ def test_patma_137(self):
+ return # disabled
+ x = {0: 1}
+ match x:
+ case {1: 0}:
+ y = 0
+ case {0: 0}:
+ y = 0
+ case {**z}:
+ y = 1
+ self.assertEqual(x, {0: 1})
+ self.assertEqual(y, 1)
+ self.assertEqual(z, {0: 1})
+
+ def test_patma_138(self):
+ return # disabled
+ x = {0: 1}
+ match x:
+ case {1: 0}:
+ y = 0
+ case {0: 0}:
+ y = 0
+ case {0: _, **z}:
+ y = 1
+ self.assertEqual(x, {0: 1})
+ self.assertEqual(y, 1)
+ self.assertEqual(z, {})
+
+ def test_patma_139(self):
+ return # disabled
+ x = False
+ match x:
+ case bool(z):
+ y = 0
+ self.assertIs(x, False)
+ self.assertEqual(y, 0)
+ self.assertIs(z, x)
+
+ def test_patma_140(self):
+ return # disabled
+ x = True
+ match x:
+ case bool(z):
+ y = 0
+ self.assertIs(x, True)
+ self.assertEqual(y, 0)
+ self.assertIs(z, x)
+
+ def test_patma_141(self):
+ return # disabled
+ x = bytearray()
+ match x:
+ case bytearray(z):
+ y = 0
+ self.assertEqual(x, bytearray())
+ self.assertEqual(y, 0)
+ self.assertIs(z, x)
+
+ def test_patma_142(self):
+ return # disabled
+ x = b""
+ match x:
+ case bytes(z):
+ y = 0
+ self.assertEqual(x, b"")
+ self.assertEqual(y, 0)
+ self.assertIs(z, x)
+
+ def test_patma_143(self):
+ return # disabled
+ x = {}
+ match x:
+ case dict(z):
+ y = 0
+ self.assertEqual(x, {})
+ self.assertEqual(y, 0)
+ self.assertIs(z, x)
+
+ def test_patma_144(self):
+ return # disabled
+ x = 0.0
+ match x:
+ case float(z):
+ y = 0
+ self.assertEqual(x, 0.0)
+ self.assertEqual(y, 0)
+ self.assertIs(z, x)
+
+ def test_patma_145(self):
+ return # disabled
+ x = frozenset()
+ match x:
+ case frozenset(z):
+ y = 0
+ self.assertEqual(x, frozenset())
+ self.assertEqual(y, 0)
+ self.assertIs(z, x)
+
+ def test_patma_146(self):
+ return # disabled
+ x = 0
+ match x:
+ case int(z):
+ y = 0
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 0)
+ self.assertIs(z, x)
+
+ def test_patma_147(self):
+ return # disabled
+ x = []
+ match x:
+ case list(z):
+ y = 0
+ self.assertEqual(x, [])
+ self.assertEqual(y, 0)
+ self.assertIs(z, x)
+
+ def test_patma_148(self):
+ return # disabled
+ x = set()
+ match x:
+ case set(z):
+ y = 0
+ self.assertEqual(x, set())
+ self.assertEqual(y, 0)
+ self.assertIs(z, x)
+
+ def test_patma_149(self):
+ return # disabled
+ x = ""
+ match x:
+ case str(z):
+ y = 0
+ self.assertEqual(x, "")
+ self.assertEqual(y, 0)
+ self.assertIs(z, x)
+
+ def test_patma_150(self):
+ return # disabled
+ x = ()
+ match x:
+ case tuple(z):
+ y = 0
+ self.assertEqual(x, ())
+ self.assertEqual(y, 0)
+ self.assertIs(z, x)
+
+ def test_patma_151(self):
+ return # disabled
+ x = 0
+ match x,:
+ case y,:
+ z = 0
+ self.assertEqual(x, 0)
+ self.assertIs(y, x)
+ self.assertIs(z, 0)
+
+ def test_patma_152(self):
+ return # disabled
+ w = 0
+ x = 0
+ match w, x:
+ case y, z:
+ v = 0
+ self.assertEqual(w, 0)
+ self.assertEqual(x, 0)
+ self.assertIs(y, w)
+ self.assertIs(z, x)
+ self.assertEqual(v, 0)
+
+ def test_patma_153(self):
+ return # disabled
+ x = 0
+ match w := x,:
+ case y as v,:
+ z = 0
+ self.assertEqual(x, 0)
+ self.assertIs(y, x)
+ self.assertEqual(z, 0)
+ self.assertIs(w, x)
+ self.assertIs(v, y)
+
+ def test_patma_154(self):
+ return # disabled
+ x = 0
+ y = None
+ match x:
+ case 0 if x:
+ y = 0
+ self.assertEqual(x, 0)
+ self.assertIs(y, None)
+
+ def test_patma_155(self):
+ return # disabled
+ x = 0
+ y = None
+ match x:
+ case 1e1000:
+ y = 0
+ self.assertEqual(x, 0)
+ self.assertIs(y, None)
+
+ def test_patma_156(self):
+ return # disabled
+ x = 0
+ match x:
+ case z:
+ y = 0
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 0)
+ self.assertIs(z, x)
+
+ def test_patma_157(self):
+ return # disabled
+ x = 0
+ y = None
+ match x:
+ case _ if x:
+ y = 0
+ self.assertEqual(x, 0)
+ self.assertIs(y, None)
+
+ def test_patma_158(self):
+ return # disabled
+ x = 0
+ match x:
+ case -1e1000:
+ y = 0
+ case 0:
+ y = 1
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 1)
+
+ def test_patma_159(self):
+ return # disabled
+ x = 0
+ match x:
+ case 0 if not x:
+ y = 0
+ case 1:
+ y = 1
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 0)
+
+ def test_patma_160(self):
+ return # disabled
+ x = 0
+ z = None
+ match x:
+ case 0:
+ y = 0
+ case z if x:
+ y = 1
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 0)
+ self.assertIs(z, None)
+
+ def test_patma_161(self):
+ return # disabled
+ x = 0
+ match x:
+ case 0:
+ y = 0
+ case _:
+ y = 1
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 0)
+
+ def test_patma_162(self):
+ return # disabled
+ x = 0
+ match x:
+ case 1 if x:
+ y = 0
+ case 0:
+ y = 1
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 1)
+
+ def test_patma_163(self):
+ return # disabled
+ x = 0
+ y = None
+ match x:
+ case 1:
+ y = 0
+ case 1 if not x:
+ y = 1
+ self.assertEqual(x, 0)
+ self.assertIs(y, None)
+
+ def test_patma_164(self):
+ return # disabled
+ x = 0
+ match x:
+ case 1:
+ y = 0
+ case z:
+ y = 1
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 1)
+ self.assertIs(z, x)
+
+ def test_patma_165(self):
+ return # disabled
+ x = 0
+ match x:
+ case 1 if x:
+ y = 0
+ case _:
+ y = 1
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 1)
+
+ def test_patma_166(self):
+ return # disabled
+ x = 0
+ match x:
+ case z if not z:
+ y = 0
+ case 0 if x:
+ y = 1
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 0)
+ self.assertIs(z, x)
+
+ def test_patma_167(self):
+ return # disabled
+ x = 0
+ match x:
+ case z if not z:
+ y = 0
+ case 1:
+ y = 1
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 0)
+ self.assertIs(z, x)
+
+ def test_patma_168(self):
+ return # disabled
+ x = 0
+ match x:
+ case z if not x:
+ y = 0
+ case z:
+ y = 1
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 0)
+ self.assertIs(z, x)
+
+ def test_patma_169(self):
+ return # disabled
+ x = 0
+ match x:
+ case z if not z:
+ y = 0
+ case _ if x:
+ y = 1
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 0)
+ self.assertIs(z, x)
+
+ def test_patma_170(self):
+ return # disabled
+ x = 0
+ match x:
+ case _ if not x:
+ y = 0
+ case 0:
+ y = 1
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 0)
+
+ def test_patma_171(self):
+ return # disabled
+ x = 0
+ y = None
+ match x:
+ case _ if x:
+ y = 0
+ case 1:
+ y = 1
+ self.assertEqual(x, 0)
+ self.assertIs(y, None)
+
+ def test_patma_172(self):
+ return # disabled
+ x = 0
+ z = None
+ match x:
+ case _ if not x:
+ y = 0
+ case z if not x:
+ y = 1
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 0)
+ self.assertIs(z, None)
+
+ def test_patma_173(self):
+ return # disabled
+ x = 0
+ match x:
+ case _ if not x:
+ y = 0
+ case _:
+ y = 1
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 0)
+
+ def test_patma_174(self):
+ return # disabled
+ def http_error(status):
+ match status:
+ case 400:
+ return "Bad request"
+ case 401:
+ return "Unauthorized"
+ case 403:
+ return "Forbidden"
+ case 404:
+ return "Not found"
+ case 418:
+ return "I'm a teapot"
+ case _:
+ return "Something else"
+ self.assertEqual(http_error(400), "Bad request")
+ self.assertEqual(http_error(401), "Unauthorized")
+ self.assertEqual(http_error(403), "Forbidden")
+ self.assertEqual(http_error(404), "Not found")
+ self.assertEqual(http_error(418), "I'm a teapot")
+ self.assertEqual(http_error(123), "Something else")
+ self.assertEqual(http_error("400"), "Something else")
+ self.assertEqual(http_error(401 | 403 | 404), "Something else") # 407
+
+ def test_patma_175(self):
+ return # disabled
+ def http_error(status):
+ match status:
+ case 400:
+ return "Bad request"
+ case 401 | 403 | 404:
+ return "Not allowed"
+ case 418:
+ return "I'm a teapot"
+ self.assertEqual(http_error(400), "Bad request")
+ self.assertEqual(http_error(401), "Not allowed")
+ self.assertEqual(http_error(403), "Not allowed")
+ self.assertEqual(http_error(404), "Not allowed")
+ self.assertEqual(http_error(418), "I'm a teapot")
+ self.assertIs(http_error(123), None)
+ self.assertIs(http_error("400"), None)
+ self.assertIs(http_error(401 | 403 | 404), None) # 407
+
+ def test_patma_176(self):
+ return # disabled
+ def whereis(point):
+ match point:
+ case (0, 0):
+ return "Origin"
+ case (0, y):
+ return f"Y={y}"
+ case (x, 0):
+ return f"X={x}"
+ case (x, y):
+ return f"X={x}, Y={y}"
+ case _:
+ return "Not a point"
+ self.assertEqual(whereis((0, 0)), "Origin")
+ self.assertEqual(whereis((0, -1.0)), "Y=-1.0")
+ self.assertEqual(whereis(("X", 0)), "X=X")
+ self.assertEqual(whereis((None, 1j)), "X=None, Y=1j")
+ self.assertEqual(whereis(42), "Not a point")
+
+ def test_patma_177(self):
+ return # disabled
+ def whereis(point):
+ match point:
+ case Point(0, 0):
+ return "Origin"
+ case Point(0, y):
+ return f"Y={y}"
+ case Point(x, 0):
+ return f"X={x}"
+ case Point():
+ return "Somewhere else"
+ case _:
+ return "Not a point"
+ self.assertEqual(whereis(Point(1, 0)), "X=1")
+ self.assertEqual(whereis(Point(0, 0)), "Origin")
+ self.assertEqual(whereis(10), "Not a point")
+ self.assertEqual(whereis(Point(False, False)), "Origin")
+ self.assertEqual(whereis(Point(0, -1.0)), "Y=-1.0")
+ self.assertEqual(whereis(Point("X", 0)), "X=X")
+ self.assertEqual(whereis(Point(None, 1j)), "Somewhere else")
+ self.assertEqual(whereis(Point), "Not a point")
+ self.assertEqual(whereis(42), "Not a point")
+
+ def test_patma_178(self):
+ return # disabled
+ def whereis(point):
+ match point:
+ case Point(1, var):
+ return var
+ self.assertEqual(whereis(Point(1, 0)), 0)
+ self.assertIs(whereis(Point(0, 0)), None)
+
+ def test_patma_179(self):
+ return # disabled
+ def whereis(point):
+ match point:
+ case Point(1, y=var):
+ return var
+ self.assertEqual(whereis(Point(1, 0)), 0)
+ self.assertIs(whereis(Point(0, 0)), None)
+
+ def test_patma_180(self):
+ return # disabled
+ def whereis(point):
+ match point:
+ case Point(x=1, y=var):
+ return var
+ self.assertEqual(whereis(Point(1, 0)), 0)
+ self.assertIs(whereis(Point(0, 0)), None)
+
+ def test_patma_181(self):
+ return # disabled
+ def whereis(point):
+ match point:
+ case Point(y=var, x=1):
+ return var
+ self.assertEqual(whereis(Point(1, 0)), 0)
+ self.assertIs(whereis(Point(0, 0)), None)
+
+ def test_patma_182(self):
+ return # disabled
+ def whereis(points):
+ match points:
+ case []:
+ return "No points"
+ case [Point(0, 0)]:
+ return "The origin"
+ case [Point(x, y)]:
+ return f"Single point {x}, {y}"
+ case [Point(0, y1), Point(0, y2)]:
+ return f"Two on the Y axis at {y1}, {y2}"
+ case _:
+ return "Something else"
+ self.assertEqual(whereis([]), "No points")
+ self.assertEqual(whereis([Point(0, 0)]), "The origin")
+ self.assertEqual(whereis([Point(0, 1)]), "Single point 0, 1")
+ self.assertEqual(whereis([Point(0, 0), Point(0, 0)]), "Two on the Y axis at 0, 0")
+ self.assertEqual(whereis([Point(0, 1), Point(0, 1)]), "Two on the Y axis at 1, 1")
+ self.assertEqual(whereis([Point(0, 0), Point(1, 0)]), "Something else")
+ self.assertEqual(whereis([Point(0, 0), Point(0, 0), Point(0, 0)]), "Something else")
+ self.assertEqual(whereis([Point(0, 1), Point(0, 1), Point(0, 1)]), "Something else")
+
+ def test_patma_183(self):
+ return # disabled
+ def whereis(point):
+ match point:
+ case Point(x, y) if x == y:
+ return f"Y=X at {x}"
+ case Point(x, y):
+ return "Not on the diagonal"
+ self.assertEqual(whereis(Point(0, 0)), "Y=X at 0")
+ self.assertEqual(whereis(Point(0, False)), "Y=X at 0")
+ self.assertEqual(whereis(Point(False, 0)), "Y=X at False")
+ self.assertEqual(whereis(Point(-1 - 1j, -1 - 1j)), "Y=X at (-1-1j)")
+ self.assertEqual(whereis(Point("X", "X")), "Y=X at X")
+ self.assertEqual(whereis(Point("X", "x")), "Not on the diagonal")
+
+ def test_patma_184(self):
+ return # disabled
+ class Seq(collections.abc.Sequence):
+ __getitem__ = None
+ def __len__(self):
+ return 0
+ match Seq():
+ case []:
+ y = 0
+ self.assertEqual(y, 0)
+
+ def test_patma_185(self):
+ return # disabled
+ class Seq(collections.abc.Sequence):
+ __getitem__ = None
+ def __len__(self):
+ return 42
+ match Seq():
+ case [*_]:
+ y = 0
+ self.assertEqual(y, 0)
+
+ def test_patma_186(self):
+ return # disabled
+ class Seq(collections.abc.Sequence):
+ def __getitem__(self, i):
+ return i
+ def __len__(self):
+ return 42
+ match Seq():
+ case [x, *_, y]:
+ z = 0
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 41)
+ self.assertEqual(z, 0)
+
+ def test_patma_187(self):
+ return # disabled
+ w = range(10)
+ match w:
+ case [x, y, *rest]:
+ z = 0
+ self.assertEqual(w, range(10))
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 1)
+ self.assertEqual(z, 0)
+ self.assertEqual(rest, list(range(2, 10)))
+
+ def test_patma_188(self):
+ return # disabled
+ w = range(100)
+ match w:
+ case (x, y, *rest):
+ z = 0
+ self.assertEqual(w, range(100))
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 1)
+ self.assertEqual(z, 0)
+ self.assertEqual(rest, list(range(2, 100)))
+
+ def test_patma_189(self):
+ return # disabled
+ w = range(1000)
+ match w:
+ case x, y, *rest:
+ z = 0
+ self.assertEqual(w, range(1000))
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 1)
+ self.assertEqual(z, 0)
+ self.assertEqual(rest, list(range(2, 1000)))
+
+ def test_patma_190(self):
+ return # disabled
+ w = range(1 << 10)
+ match w:
+ case [x, y, *_]:
+ z = 0
+ self.assertEqual(w, range(1 << 10))
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 1)
+ self.assertEqual(z, 0)
+
+ def test_patma_191(self):
+ return # disabled
+ w = range(1 << 20)
+ match w:
+ case (x, y, *_):
+ z = 0
+ self.assertEqual(w, range(1 << 20))
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 1)
+ self.assertEqual(z, 0)
+
+ def test_patma_192(self):
+ return # disabled
+ w = range(1 << 30)
+ match w:
+ case x, y, *_:
+ z = 0
+ self.assertEqual(w, range(1 << 30))
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 1)
+ self.assertEqual(z, 0)
+
+ def test_patma_193(self):
+ return # disabled
+ x = {"bandwidth": 0, "latency": 1}
+ match x:
+ case {"bandwidth": b, "latency": l}:
+ y = 0
+ self.assertEqual(x, {"bandwidth": 0, "latency": 1})
+ self.assertIs(b, x["bandwidth"])
+ self.assertIs(l, x["latency"])
+ self.assertEqual(y, 0)
+
+ def test_patma_194(self):
+ return # disabled
+ x = {"bandwidth": 0, "latency": 1, "key": "value"}
+ match x:
+ case {"latency": l, "bandwidth": b}:
+ y = 0
+ self.assertEqual(x, {"bandwidth": 0, "latency": 1, "key": "value"})
+ self.assertIs(l, x["latency"])
+ self.assertIs(b, x["bandwidth"])
+ self.assertEqual(y, 0)
+
+ def test_patma_195(self):
+ return # disabled
+ x = {"bandwidth": 0, "latency": 1, "key": "value"}
+ match x:
+ case {"bandwidth": b, "latency": l, **rest}:
+ y = 0
+ self.assertEqual(x, {"bandwidth": 0, "latency": 1, "key": "value"})
+ self.assertIs(b, x["bandwidth"])
+ self.assertIs(l, x["latency"])
+ self.assertEqual(rest, {"key": "value"})
+ self.assertEqual(y, 0)
+
+ def test_patma_196(self):
+ return # disabled
+ x = {"bandwidth": 0, "latency": 1}
+ match x:
+ case {"latency": l, "bandwidth": b, **rest}:
+ y = 0
+ self.assertEqual(x, {"bandwidth": 0, "latency": 1})
+ self.assertIs(l, x["latency"])
+ self.assertIs(b, x["bandwidth"])
+ self.assertEqual(rest, {})
+ self.assertEqual(y, 0)
+
+ def test_patma_197(self):
+ return # disabled
+ w = [Point(-1, 0), Point(1, 2)]
+ match w:
+ case (Point(x1, y1), Point(x2, y2) as p2):
+ z = 0
+ self.assertEqual(w, [Point(-1, 0), Point(1, 2)])
+ self.assertIs(x1, w[0].x)
+ self.assertIs(y1, w[0].y)
+ self.assertIs(p2, w[1])
+ self.assertIs(x2, w[1].x)
+ self.assertIs(y2, w[1].y)
+ self.assertIs(z, 0)
+
+ def test_patma_198(self):
+ return # disabled
+ class Color(enum.Enum):
+ RED = 0
+ GREEN = 1
+ BLUE = 2
+ def f(color):
+ match color:
+ case Color.RED:
+ return "I see red!"
+ case Color.GREEN:
+ return "Grass is green"
+ case Color.BLUE:
+ return "I'm feeling the blues :("
+ self.assertEqual(f(Color.RED), "I see red!")
+ self.assertEqual(f(Color.GREEN), "Grass is green")
+ self.assertEqual(f(Color.BLUE), "I'm feeling the blues :(")
+ self.assertIs(f(Color), None)
+ self.assertIs(f(0), None)
+ self.assertIs(f(1), None)
+ self.assertIs(f(2), None)
+ self.assertIs(f(3), None)
+ self.assertIs(f(False), None)
+ self.assertIs(f(True), None)
+ self.assertIs(f(2+0j), None)
+ self.assertIs(f(3.0), None)
+
+ def test_patma_199(self):
+ return # disabled
+ class Color(int, enum.Enum):
+ RED = 0
+ GREEN = 1
+ BLUE = 2
+ def f(color):
+ match color:
+ case Color.RED:
+ return "I see red!"
+ case Color.GREEN:
+ return "Grass is green"
+ case Color.BLUE:
+ return "I'm feeling the blues :("
+ self.assertEqual(f(Color.RED), "I see red!")
+ self.assertEqual(f(Color.GREEN), "Grass is green")
+ self.assertEqual(f(Color.BLUE), "I'm feeling the blues :(")
+ self.assertIs(f(Color), None)
+ self.assertEqual(f(0), "I see red!")
+ self.assertEqual(f(1), "Grass is green")
+ self.assertEqual(f(2), "I'm feeling the blues :(")
+ self.assertIs(f(3), None)
+ self.assertEqual(f(False), "I see red!")
+ self.assertEqual(f(True), "Grass is green")
+ self.assertEqual(f(2+0j), "I'm feeling the blues :(")
+ self.assertIs(f(3.0), None)
+
+ def test_patma_200(self):
+ return # disabled
+ class Class:
+ __match_args__ = ("a", "b")
+ c = Class()
+ c.a = 0
+ c.b = 1
+ match c:
+ case Class(x, y):
+ z = 0
+ self.assertIs(x, c.a)
+ self.assertIs(y, c.b)
+ self.assertEqual(z, 0)
+
+ def test_patma_201(self):
+ return # disabled
+ class Class:
+ __match_args__ = ("a", "b")
+ c = Class()
+ c.a = 0
+ c.b = 1
+ match c:
+ case Class(x, b=y):
+ z = 0
+ self.assertIs(x, c.a)
+ self.assertIs(y, c.b)
+ self.assertEqual(z, 0)
+
+ def test_patma_202(self):
+ return # disabled
+ class Parent:
+ __match_args__ = "a", "b"
+ class Child(Parent):
+ __match_args__ = ("c", "d")
+ c = Child()
+ c.a = 0
+ c.b = 1
+ match c:
+ case Parent(x, y):
+ z = 0
+ self.assertIs(x, c.a)
+ self.assertIs(y, c.b)
+ self.assertEqual(z, 0)
+
+ def test_patma_203(self):
+ return # disabled
+ class Parent:
+ __match_args__ = ("a", "b")
+ class Child(Parent):
+ __match_args__ = "c", "d"
+ c = Child()
+ c.a = 0
+ c.b = 1
+ match c:
+ case Parent(x, b=y):
+ z = 0
+ self.assertIs(x, c.a)
+ self.assertIs(y, c.b)
+ self.assertEqual(z, 0)
+
+ def test_patma_204(self):
+ return # disabled
+ def f(w):
+ match w:
+ case 42:
+ out = locals()
+ del out["w"]
+ return out
+ self.assertEqual(f(42), {})
+ self.assertIs(f(0), None)
+ self.assertEqual(f(42.0), {})
+ self.assertIs(f("42"), None)
+
+ def test_patma_205(self):
+ return # disabled
+ def f(w):
+ match w:
+ case 42.0:
+ out = locals()
+ del out["w"]
+ return out
+ self.assertEqual(f(42.0), {})
+ self.assertEqual(f(42), {})
+ self.assertIs(f(0.0), None)
+ self.assertIs(f(0), None)
+
+ def test_patma_206(self):
+ return # disabled
+ def f(w):
+ match w:
+ case 1 | 2 | 3:
+ out = locals()
+ del out["w"]
+ return out
+ self.assertEqual(f(1), {})
+ self.assertEqual(f(2), {})
+ self.assertEqual(f(3), {})
+ self.assertEqual(f(3.0), {})
+ self.assertIs(f(0), None)
+ self.assertIs(f(4), None)
+ self.assertIs(f("1"), None)
+
+ def test_patma_207(self):
+ return # disabled
+ def f(w):
+ match w:
+ case [1, 2] | [3, 4]:
+ out = locals()
+ del out["w"]
+ return out
+ self.assertEqual(f([1, 2]), {})
+ self.assertEqual(f([3, 4]), {})
+ self.assertIs(f(42), None)
+ self.assertIs(f([2, 3]), None)
+ self.assertIs(f([1, 2, 3]), None)
+ self.assertEqual(f([1, 2.0]), {})
+
+ def test_patma_208(self):
+ return # disabled
+ def f(w):
+ match w:
+ case x:
+ out = locals()
+ del out["w"]
+ return out
+ self.assertEqual(f(42), {"x": 42})
+ self.assertEqual(f((1, 2)), {"x": (1, 2)})
+ self.assertEqual(f(None), {"x": None})
+
+ def test_patma_209(self):
+ return # disabled
+ def f(w):
+ match w:
+ case _:
+ out = locals()
+ del out["w"]
+ return out
+ self.assertEqual(f(42), {})
+ self.assertEqual(f(None), {})
+ self.assertEqual(f((1, 2)), {})
+
+ def test_patma_210(self):
+ return # disabled
+ def f(w):
+ match w:
+ case (x, y, z):
+ out = locals()
+ del out["w"]
+ return out
+ self.assertEqual(f((1, 2, 3)), {"x": 1, "y": 2, "z": 3})
+ self.assertIs(f((1, 2)), None)
+ self.assertIs(f((1, 2, 3, 4)), None)
+ self.assertIs(f(123), None)
+ self.assertIs(f("abc"), None)
+ self.assertIs(f(b"abc"), None)
+ self.assertEqual(f(array.array("b", b"abc")), {'x': 97, 'y': 98, 'z': 99})
+ self.assertEqual(f(memoryview(b"abc")), {"x": 97, "y": 98, "z": 99})
+ self.assertIs(f(bytearray(b"abc")), None)
+
+ def test_patma_211(self):
+ return # disabled
+ def f(w):
+ match w:
+ case {"x": x, "y": "y", "z": z}:
+ out = locals()
+ del out["w"]
+ return out
+ self.assertEqual(f({"x": "x", "y": "y", "z": "z"}), {"x": "x", "z": "z"})
+ self.assertEqual(f({"x": "x", "y": "y", "z": "z", "a": "a"}), {"x": "x", "z": "z"})
+ self.assertIs(f(({"x": "x", "y": "yy", "z": "z", "a": "a"})), None)
+ self.assertIs(f(({"x": "x", "y": "y"})), None)
+
+ def test_patma_212(self):
+ return # disabled
+ def f(w):
+ match w:
+ case Point(int(xx), y="hello"):
+ out = locals()
+ del out["w"]
+ return out
+ self.assertEqual(f(Point(42, "hello")), {"xx": 42})
+
+ def test_patma_213(self):
+ return # disabled
+ def f(w):
+ match w:
+ case (p, q) as x:
+ out = locals()
+ del out["w"]
+ return out
+ self.assertEqual(f((1, 2)), {"p": 1, "q": 2, "x": (1, 2)})
+ self.assertEqual(f([1, 2]), {"p": 1, "q": 2, "x": [1, 2]})
+ self.assertIs(f(12), None)
+ self.assertIs(f((1, 2, 3)), None)
+
+ def test_patma_214(self):
+ return # disabled
+ def f():
+ match 42:
+ case 42:
+ return locals()
+ self.assertEqual(set(f()), set())
+
+ def test_patma_215(self):
+ return # disabled
+ def f():
+ match 1:
+ case 1 | 2 | 3:
+ return locals()
+ self.assertEqual(set(f()), set())
+
+ def test_patma_216(self):
+ return # disabled
+ def f():
+ match ...:
+ case _:
+ return locals()
+ self.assertEqual(set(f()), set())
+
+ def test_patma_217(self):
+ return # disabled
+ def f():
+ match ...:
+ case abc:
+ return locals()
+ self.assertEqual(set(f()), {"abc"})
+
+ def test_patma_218(self):
+ return # disabled
+ def f():
+ match ..., ...:
+ case a, b:
+ return locals()
+ self.assertEqual(set(f()), {"a", "b"})
+
+ def test_patma_219(self):
+ return # disabled
+ def f():
+ match {"k": ..., "l": ...}:
+ case {"k": a, "l": b}:
+ return locals()
+ self.assertEqual(set(f()), {"a", "b"})
+
+ def test_patma_220(self):
+ return # disabled
+ def f():
+ match Point(..., ...):
+ case Point(x, y=y):
+ return locals()
+ self.assertEqual(set(f()), {"x", "y"})
+
+ def test_patma_221(self):
+ return # disabled
+ def f():
+ match ...:
+ case b as a:
+ return locals()
+ self.assertEqual(set(f()), {"a", "b"})
+
+ def test_patma_222(self):
+ return # disabled
+ def f(x):
+ match x:
+ case _:
+ return 0
+ self.assertEqual(f(0), 0)
+ self.assertEqual(f(1), 0)
+ self.assertEqual(f(2), 0)
+ self.assertEqual(f(3), 0)
+
+ def test_patma_223(self):
+ return # disabled
+ def f(x):
+ match x:
+ case 0:
+ return 0
+ self.assertEqual(f(0), 0)
+ self.assertIs(f(1), None)
+ self.assertIs(f(2), None)
+ self.assertIs(f(3), None)
+
+ def test_patma_224(self):
+ return # disabled
+ def f(x):
+ match x:
+ case 0:
+ return 0
+ case _:
+ return 1
+ self.assertEqual(f(0), 0)
+ self.assertEqual(f(1), 1)
+ self.assertEqual(f(2), 1)
+ self.assertEqual(f(3), 1)
+
+ def test_patma_225(self):
+ return # disabled
+ def f(x):
+ match x:
+ case 0:
+ return 0
+ case 1:
+ return 1
+ self.assertEqual(f(0), 0)
+ self.assertEqual(f(1), 1)
+ self.assertIs(f(2), None)
+ self.assertIs(f(3), None)
+
+ def test_patma_226(self):
+ return # disabled
+ def f(x):
+ match x:
+ case 0:
+ return 0
+ case 1:
+ return 1
+ case _:
+ return 2
+ self.assertEqual(f(0), 0)
+ self.assertEqual(f(1), 1)
+ self.assertEqual(f(2), 2)
+ self.assertEqual(f(3), 2)
+
+ def test_patma_227(self):
+ return # disabled
+ def f(x):
+ match x:
+ case 0:
+ return 0
+ case 1:
+ return 1
+ case 2:
+ return 2
+ self.assertEqual(f(0), 0)
+ self.assertEqual(f(1), 1)
+ self.assertEqual(f(2), 2)
+ self.assertIs(f(3), None)
+
+ def test_patma_228(self):
+ return # disabled
+ match():
+ case():
+ x = 0
+ self.assertEqual(x, 0)
+
+ def test_patma_229(self):
+ return # disabled
+ x = 0
+ match(x):
+ case(x):
+ y = 0
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 0)
+
+ def test_patma_230(self):
+ return # disabled
+ x = 0
+ match x:
+ case False:
+ y = 0
+ case 0:
+ y = 1
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 1)
+
+ def test_patma_231(self):
+ return # disabled
+ x = 1
+ match x:
+ case True:
+ y = 0
+ case 1:
+ y = 1
+ self.assertEqual(x, 1)
+ self.assertEqual(y, 1)
+
+ def test_patma_232(self):
+ return # disabled
+ class Eq:
+ def __eq__(self, other):
+ return True
+ x = eq = Eq()
+ y = None
+ match x:
+ case None:
+ y = 0
+ self.assertIs(x, eq)
+ self.assertEqual(y, None)
+
+ def test_patma_233(self):
+ return # disabled
+ x = False
+ match x:
+ case False:
+ y = 0
+ self.assertIs(x, False)
+ self.assertEqual(y, 0)
+
+ def test_patma_234(self):
+ return # disabled
+ x = True
+ match x:
+ case True:
+ y = 0
+ self.assertIs(x, True)
+ self.assertEqual(y, 0)
+
+ def test_patma_235(self):
+ return # disabled
+ x = None
+ match x:
+ case None:
+ y = 0
+ self.assertIs(x, None)
+ self.assertEqual(y, 0)
+
+ def test_patma_236(self):
+ return # disabled
+ x = 0
+ match x:
+ case (0 as w) as z:
+ y = 0
+ self.assertEqual(w, 0)
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 0)
+ self.assertEqual(z, 0)
+
+ def test_patma_237(self):
+ return # disabled
+ x = 0
+ match x:
+ case (0 as w) as z:
+ y = 0
+ self.assertEqual(w, 0)
+ self.assertEqual(x, 0)
+ self.assertEqual(y, 0)
+ self.assertEqual(z, 0)
+
+ def test_patma_238(self):
+ return # disabled
+ x = ((0, 1), (2, 3))
+ match x:
+ case ((a as b, c as d) as e) as w, ((f as g, h) as i) as z:
+ y = 0
+ self.assertEqual(a, 0)
+ self.assertEqual(b, 0)
+ self.assertEqual(c, 1)
+ self.assertEqual(d, 1)
+ self.assertEqual(e, (0, 1))
+ self.assertEqual(f, 2)
+ self.assertEqual(g, 2)
+ self.assertEqual(h, 3)
+ self.assertEqual(i, (2, 3))
+ self.assertEqual(w, (0, 1))
+ self.assertEqual(x, ((0, 1), (2, 3)))
+ self.assertEqual(y, 0)
+ self.assertEqual(z, (2, 3))
+
+ def test_patma_239(self):
+ return # disabled
+ x = collections.UserDict({0: 1, 2: 3})
+ match x:
+ case {2: 3}:
+ y = 0
+ self.assertEqual(x, {0: 1, 2: 3})
+ self.assertEqual(y, 0)
+
+ def test_patma_240(self):
+ return # disabled
+ x = collections.UserDict({0: 1, 2: 3})
+ match x:
+ case {2: 3, **z}:
+ y = 0
+ self.assertEqual(x, {0: 1, 2: 3})
+ self.assertEqual(y, 0)
+ self.assertEqual(z, {0: 1})
+
+ def test_patma_241(self):
+ return # disabled
+ x = [[{0: 0}]]
+ match x:
+ case list([({-0-0j: int(real=0+0j, imag=0-0j) | (1) as z},)]):
+ y = 0
+ self.assertEqual(x, [[{0: 0}]])
+ self.assertEqual(y, 0)
+ self.assertEqual(z, 0)
+
+ def test_patma_242(self):
+ return # disabled
+ x = range(3)
+ match x:
+ case [y, *_, z]:
+ w = 0
+ self.assertEqual(w, 0)
+ self.assertEqual(x, range(3))
+ self.assertEqual(y, 0)
+ self.assertEqual(z, 2)
+
+ def test_patma_243(self):
+ return # disabled
+ x = range(3)
+ match x:
+ case [_, *_, y]:
+ z = 0
+ self.assertEqual(x, range(3))
+ self.assertEqual(y, 2)
+ self.assertEqual(z, 0)
+
+ def test_patma_244(self):
+ return # disabled
+ x = range(3)
+ match x:
+ case [*_, y]:
+ z = 0
+ self.assertEqual(x, range(3))
+ self.assertEqual(y, 2)
+ self.assertEqual(z, 0)
+
+ def test_patma_245(self):
+ return # disabled
+ x = {"y": 1}
+ match x:
+ case {"y": (0 as y) | (1 as y)}:
+ z = 0
+ self.assertEqual(x, {"y": 1})
+ self.assertEqual(y, 1)
+ self.assertEqual(z, 0)
+
+ def test_patma_246(self):
+ return # disabled
+ def f(x):
+ match x:
+ case ((a, b, c, d, e, f, g, h, i, 9) |
+ (h, g, i, a, b, d, e, c, f, 10) |
+ (g, b, a, c, d, -5, e, h, i, f) |
+ (-1, d, f, b, g, e, i, a, h, c)):
+ w = 0
+ out = locals()
+ del out["x"]
+ return out
+ alts = [
+ dict(a=0, b=1, c=2, d=3, e=4, f=5, g=6, h=7, i=8, w=0),
+ dict(h=1, g=2, i=3, a=4, b=5, d=6, e=7, c=8, f=9, w=0),
+ dict(g=0, b=-1, a=-2, c=-3, d=-4, e=-6, h=-7, i=-8, f=-9, w=0),
+ dict(d=-2, f=-3, b=-4, g=-5, e=-6, i=-7, a=-8, h=-9, c=-10, w=0),
+ dict(),
+ ]
+ self.assertEqual(f(range(10)), alts[0])
+ self.assertEqual(f(range(1, 11)), alts[1])
+ self.assertEqual(f(range(0, -10, -1)), alts[2])
+ self.assertEqual(f(range(-1, -11, -1)), alts[3])
+ self.assertEqual(f(range(10, 20)), alts[4])
+
+ def test_patma_247(self):
+ return # disabled
+ def f(x):
+ match x:
+ case [y, (a, b, c, d, e, f, g, h, i, 9) |
+ (h, g, i, a, b, d, e, c, f, 10) |
+ (g, b, a, c, d, -5, e, h, i, f) |
+ (-1, d, f, b, g, e, i, a, h, c), z]:
+ w = 0
+ out = locals()
+ del out["x"]
+ return out
+ alts = [
+ dict(a=0, b=1, c=2, d=3, e=4, f=5, g=6, h=7, i=8, w=0, y=False, z=True),
+ dict(h=1, g=2, i=3, a=4, b=5, d=6, e=7, c=8, f=9, w=0, y=False, z=True),
+ dict(g=0, b=-1, a=-2, c=-3, d=-4, e=-6, h=-7, i=-8, f=-9, w=0, y=False, z=True),
+ dict(d=-2, f=-3, b=-4, g=-5, e=-6, i=-7, a=-8, h=-9, c=-10, w=0, y=False, z=True),
+ dict(),
+ ]
+ self.assertEqual(f((False, range(10), True)), alts[0])
+ self.assertEqual(f((False, range(1, 11), True)), alts[1])
+ self.assertEqual(f((False, range(0, -10, -1), True)), alts[2])
+ self.assertEqual(f((False, range(-1, -11, -1), True)), alts[3])
+ self.assertEqual(f((False, range(10, 20), True)), alts[4])
+
+ def test_patma_248(self):
+ return # disabled
+ class C(dict):
+ @staticmethod
+ def get(key, default=None):
+ return 'bar'
+
+ x = C({'foo': 'bar'})
+ match x:
+ case {'foo': bar}:
+ y = bar
+
+ self.assertEqual(y, 'bar')
+
+ def test_patma_249(self):
+ return # disabled
+ class C:
+ __attr = "eggs" # mangled to _C__attr
+ _Outer__attr = "bacon"
+ class Outer:
+ def f(self, x):
+ match x:
+ # looks up __attr, not _C__attr or _Outer__attr
+ case C(__attr=y):
+ return y
+ c = C()
+ setattr(c, "__attr", "spam") # setattr is needed because we're in a class scope
+ self.assertEqual(Outer().f(c), "spam")
+
+
+class TestSyntaxErrors(unittest.TestCase):
+
+ def assert_syntax_error(self, code: str):
+ with self.assertRaises(SyntaxError):
+ compile(inspect.cleandoc(code), "<test>", "exec")
+
+ def test_alternative_patterns_bind_different_names_0(self):
+ self.assert_syntax_error("""
+ match ...:
+ case "a" | a:
+ pass
+ """)
+
+ def test_alternative_patterns_bind_different_names_1(self):
+ self.assert_syntax_error("""
+ match ...:
+ case [a, [b] | [c] | [d]]:
+ pass
+ """)
+
+
+ @disable # validation will be added when class patterns are added
+ def test_attribute_name_repeated_in_class_pattern(self):
+ self.assert_syntax_error("""
+ match ...:
+ case Class(a=_, a=_):
+ pass
+ """)
+
+ def test_imaginary_number_required_in_complex_literal_0(self):
+ self.assert_syntax_error("""
+ match ...:
+ case 0+0:
+ pass
+ """)
+
+ def test_imaginary_number_required_in_complex_literal_1(self):
+ self.assert_syntax_error("""
+ match ...:
+ case {0+0: _}:
+ pass
+ """)
+
+ def test_invalid_syntax_0(self):
+ self.assert_syntax_error("""
+ match ...:
+ case {**rest, "key": value}:
+ pass
+ """)
+
+ def test_invalid_syntax_1(self):
+ self.assert_syntax_error("""
+ match ...:
+ case {"first": first, **rest, "last": last}:
+ pass
+ """)
+
+ def test_invalid_syntax_2(self):
+ self.assert_syntax_error("""
+ match ...:
+ case {**_}:
+ pass
+ """)
+
+ def test_invalid_syntax_3(self):
+ self.assert_syntax_error("""
+ match ...:
+ case 42 as _:
+ pass
+ """)
+
+ def test_mapping_pattern_keys_may_only_match_literals_and_attribute_lookups(self):
+ self.assert_syntax_error("""
+ match ...:
+ case {f"": _}:
+ pass
+ """)
+
+ def test_multiple_assignments_to_name_in_pattern_0(self):
+ self.assert_syntax_error("""
+ match ...:
+ case a, a:
+ pass
+ """)
+
+ def test_multiple_assignments_to_name_in_pattern_1(self):
+ self.assert_syntax_error("""
+ match ...:
+ case {"k": a, "l": a}:
+ pass
+ """)
+
+ def test_multiple_assignments_to_name_in_pattern_2(self):
+ self.assert_syntax_error("""
+ match ...:
+ case MyClass(x, x):
+ pass
+ """)
+
+ def test_multiple_assignments_to_name_in_pattern_3(self):
+ self.assert_syntax_error("""
+ match ...:
+ case MyClass(x=x, y=x):
+ pass
+ """)
+
+ def test_multiple_assignments_to_name_in_pattern_4(self):
+ self.assert_syntax_error("""
+ match ...:
+ case MyClass(x, y=x):
+ pass
+ """)
+
+ def test_multiple_assignments_to_name_in_pattern_5(self):
+ self.assert_syntax_error("""
+ match ...:
+ case a as a:
+ pass
+ """)
+
+ @disable # will be implemented as part of sequence patterns
+ def test_multiple_starred_names_in_sequence_pattern_0(self):
+ self.assert_syntax_error("""
+ match ...:
+ case *a, b, *c, d, *e:
+ pass
+ """)
+
+ @disable # will be implemented as part of sequence patterns
+ def test_multiple_starred_names_in_sequence_pattern_1(self):
+ self.assert_syntax_error("""
+ match ...:
+ case a, *b, c, *d, e:
+ pass
+ """)
+
+ def test_name_capture_makes_remaining_patterns_unreachable_0(self):
+ self.assert_syntax_error("""
+ match ...:
+ case a | "a":
+ pass
+ """)
+
+ def test_name_capture_makes_remaining_patterns_unreachable_1(self):
+ self.assert_syntax_error("""
+ match 42:
+ case x:
+ pass
+ case y:
+ pass
+ """)
+
+ def test_name_capture_makes_remaining_patterns_unreachable_2(self):
+ self.assert_syntax_error("""
+ match ...:
+ case x | [_ as x] if x:
+ pass
+ """)
+
+ def test_name_capture_makes_remaining_patterns_unreachable_3(self):
+ self.assert_syntax_error("""
+ match ...:
+ case x:
+ pass
+ case [x] if x:
+ pass
+ """)
+
+ def test_name_capture_makes_remaining_patterns_unreachable_4(self):
+ self.assert_syntax_error("""
+ match ...:
+ case x:
+ pass
+ case _:
+ pass
+ """)
+
+ def test_patterns_may_only_match_literals_and_attribute_lookups_0(self):
+ self.assert_syntax_error("""
+ match ...:
+ case f"":
+ pass
+ """)
+
+ def test_patterns_may_only_match_literals_and_attribute_lookups_1(self):
+ self.assert_syntax_error("""
+ match ...:
+ case f"{x}":
+ pass
+ """)
+
+ def test_real_number_required_in_complex_literal_0(self):
+ self.assert_syntax_error("""
+ match ...:
+ case 0j+0:
+ pass
+ """)
+
+ def test_real_number_required_in_complex_literal_1(self):
+ self.assert_syntax_error("""
+ match ...:
+ case 0j+0j:
+ pass
+ """)
+
+ def test_real_number_required_in_complex_literal_2(self):
+ self.assert_syntax_error("""
+ match ...:
+ case {0j+0: _}:
+ pass
+ """)
+
+ def test_real_number_required_in_complex_literal_3(self):
+ self.assert_syntax_error("""
+ match ...:
+ case {0j+0j: _}:
+ pass
+ """)
+
+ def test_wildcard_makes_remaining_patterns_unreachable_0(self):
+ self.assert_syntax_error("""
+ match ...:
+ case _ | _:
+ pass
+ """)
+
+ def test_wildcard_makes_remaining_patterns_unreachable_1(self):
+ self.assert_syntax_error("""
+ match ...:
+ case (_ as x) | [x]:
+ pass
+ """)
+
+ def test_wildcard_makes_remaining_patterns_unreachable_2(self):
+ self.assert_syntax_error("""
+ match ...:
+ case _ | _ if condition():
+ pass
+ """)
+
+ def test_wildcard_makes_remaining_patterns_unreachable_3(self):
+ self.assert_syntax_error("""
+ match ...:
+ case _:
+ pass
+ case None:
+ pass
+ """)
+
+ def test_wildcard_makes_remaining_patterns_unreachable_4(self):
+ self.assert_syntax_error("""
+ match ...:
+ case (None | _) | _:
+ pass
+ """)
+
+ def test_wildcard_makes_remaining_patterns_unreachable_5(self):
+ self.assert_syntax_error("""
+ match ...:
+ case _ | (True | False):
+ pass
+ """)
+
+ @disable # validation will be added when class patterns are added
+ def test_mapping_pattern_duplicate_key(self):
+ self.assert_syntax_error("""
+ match ...:
+ case {"a": _, "a": _}:
+ pass
+ """)
+
+ @disable # validation will be added when class patterns are added
+ def test_mapping_pattern_duplicate_key_edge_case0(self):
+ self.assert_syntax_error("""
+ match ...:
+ case {0: _, False: _}:
+ pass
+ """)
+
+ @disable # validation will be added when class patterns are added
+ def test_mapping_pattern_duplicate_key_edge_case1(self):
+ self.assert_syntax_error("""
+ match ...:
+ case {0: _, 0.0: _}:
+ pass
+ """)
+
+ @disable # validation will be added when class patterns are added
+ def test_mapping_pattern_duplicate_key_edge_case2(self):
+ self.assert_syntax_error("""
+ match ...:
+ case {0: _, -0: _}:
+ pass
+ """)
+
+ @disable # validation will be added when class patterns are added
+ def test_mapping_pattern_duplicate_key_edge_case3(self):
+ self.assert_syntax_error("""
+ match ...:
+ case {0: _, 0j: _}:
+ pass
+ """)
+
+class TestTypeErrors(unittest.TestCase):
+
+ def test_accepts_positional_subpatterns_0(self):
+ return # disabled
+ class Class:
+ __match_args__ = ()
+ x = Class()
+ y = z = None
+ with self.assertRaises(TypeError):
+ match x:
+ case Class(y):
+ z = 0
+ self.assertIs(y, None)
+ self.assertIs(z, None)
+
+ def test_accepts_positional_subpatterns_1(self):
+ return # disabled
+ x = range(10)
+ y = None
+ with self.assertRaises(TypeError):
+ match x:
+ case range(10):
+ y = 0
+ self.assertEqual(x, range(10))
+ self.assertIs(y, None)
+
+ def test_got_multiple_subpatterns_for_attribute_0(self):
+ return # disabled
+ class Class:
+ __match_args__ = ("a", "a")
+ a = None
+ x = Class()
+ w = y = z = None
+ with self.assertRaises(TypeError):
+ match x:
+ case Class(y, z):
+ w = 0
+ self.assertIs(w, None)
+ self.assertIs(y, None)
+ self.assertIs(z, None)
+
+ def test_got_multiple_subpatterns_for_attribute_1(self):
+ return # disabled
+ class Class:
+ __match_args__ = ("a",)
+ a = None
+ x = Class()
+ w = y = z = None
+ with self.assertRaises(TypeError):
+ match x:
+ case Class(y, a=z):
+ w = 0
+ self.assertIs(w, None)
+ self.assertIs(y, None)
+ self.assertIs(z, None)
+
+ def test_match_args_elements_must_be_strings(self):
+ return # disabled
+ class Class:
+ __match_args__ = (None,)
+ x = Class()
+ y = z = None
+ with self.assertRaises(TypeError):
+ match x:
+ case Class(y):
+ z = 0
+ self.assertIs(y, None)
+ self.assertIs(z, None)
+
+ def test_match_args_must_be_a_tuple_0(self):
+ return # disabled
+ class Class:
+ __match_args__ = None
+ x = Class()
+ y = z = None
+ with self.assertRaises(TypeError):
+ match x:
+ case Class(y):
+ z = 0
+ self.assertIs(y, None)
+ self.assertIs(z, None)
+
+ def test_match_args_must_be_a_tuple_1(self):
+ return # disabled
+ class Class:
+ __match_args__ = "XYZ"
+ x = Class()
+ y = z = None
+ with self.assertRaises(TypeError):
+ match x:
+ case Class(y):
+ z = 0
+ self.assertIs(y, None)
+ self.assertIs(z, None)
+
+ def test_match_args_must_be_a_tuple_2(self):
+ return # disabled
+ class Class:
+ __match_args__ = ["spam", "eggs"]
+ spam = 0
+ eggs = 1
+ x = Class()
+ w = y = z = None
+ with self.assertRaises(TypeError):
+ match x:
+ case Class(y, z):
+ w = 0
+ self.assertIs(w, None)
+ self.assertIs(y, None)
+ self.assertIs(z, None)
+
+
+class TestValueErrors(unittest.TestCase):
+
+ def test_mapping_pattern_checks_duplicate_key_1(self):
+ return # disabled
+ class Keys:
+ KEY = "a"
+ x = {"a": 0, "b": 1}
+ w = y = z = None
+ with self.assertRaises(ValueError):
+ match x:
+ case {Keys.KEY: y, "a": z}:
+ w = 0
+ self.assertIs(w, None)
+ self.assertIs(y, None)
+ self.assertIs(z, None)
+
+
+if __name__ == "__main__":
+ """
+ # From inside environment using this Python, with pyperf installed:
+ sudo $(which pyperf) system tune && \
+ $(which python) -m test.test_patma --rigorous; \
+ sudo $(which pyperf) system reset
+ """
+ import pyperf
+
+
+ class PerfPatma(TestPatma):
+
+ def assertEqual(*_, **__):
+ pass
+
+ def assertIs(*_, **__):
+ pass
+
+ def assertRaises(*_, **__):
+ assert False, "this test should be a method of a different class!"
+
+ def run_perf(self, count):
+ tests = []
+ for attr in vars(TestPatma):
+ if attr.startswith("test_"):
+ tests.append(getattr(self, attr))
+ tests *= count
+ start = pyperf.perf_counter()
+ for test in tests:
+ test()
+ return pyperf.perf_counter() - start
+
+
+ runner = pyperf.Runner()
+ runner.bench_time_func("patma", PerfPatma().run_perf)