From 1d7a295df8405489f565bcb008ce274dfa3cd479 Mon Sep 17 00:00:00 2001 From: prsabahrami Date: Wed, 15 May 2024 18:24:08 -0400 Subject: [PATCH] Fixing Coverage --- polarify/main.py | 16 +++++++++------- tests/functions.py | 27 ++++++++++++++++++++++++++- tests/functions_310.py | 25 ++++++++++++++++++------- 3 files changed, 53 insertions(+), 15 deletions(-) diff --git a/polarify/main.py b/polarify/main.py index d0e9998..5664d49 100644 --- a/polarify/main.py +++ b/polarify/main.py @@ -49,8 +49,7 @@ def __iter__(self): def build_polars_when_then_otherwise(body: Sequence[ResolvedCase], orelse: ast.expr) -> ast.Call: nodes: list[ast.Call] = [] - if not body: - raise ValueError("No cases provided") + assert body, "No when-then cases provided." for test, then in body: when_node = ast.Call( @@ -210,6 +209,8 @@ def translate_match( ops=[ast.Eq()], comparators=[stmt.value], ) + elif isinstance(stmt, ast.MatchValue) and isinstance(subj, ast.Tuple): + return self.translate_match(subj, ast.MatchSequence(patterns=[stmt])) elif isinstance(stmt, ast.MatchAs) and isinstance(subj, ast.Name): if stmt.name is not None: self.handle_assign( @@ -232,23 +233,24 @@ def translate_match( elif isinstance(stmt, ast.MatchSequence): if isinstance(stmt.patterns[-1], ast.MatchStar): raise ValueError("starred patterns are not supported.") - if isinstance(subj, ast.Tuple): while len(subj.elts) > len(stmt.patterns): stmt.patterns.append(ast.MatchValue(value=ast.Constant(value=None))) left = self.translate_match(subj.elts[0], stmt.patterns[0], guard) right = ( self.translate_match( - subj.elts[1:], + ast.Tuple(elts=subj.elts[1:]), ast.MatchSequence(patterns=stmt.patterns[1:]), ) if stmt.patterns[2:] else self.translate_match(subj.elts[1], stmt.patterns[1]) ) - if left is None or right is None: - return left or right - return ast.BinOp(left=left, op=ast.BitAnd(), right=right) + return ( + left or right + if left is None or right is None + else ast.BinOp(left=left, op=ast.BitAnd(), right=right) + ) raise ValueError("Matching lists is not supported.") else: raise ValueError( diff --git a/tests/functions.py b/tests/functions.py index 8194e59..9536d31 100644 --- a/tests/functions.py +++ b/tests/functions.py @@ -3,7 +3,7 @@ import sys if sys.version_info >= (3, 10): - from .functions_310 import functions_310, unsupported_functions_310 + from .functions_310 import functions_310, unsupported_functions_310, xfail_functions_310 else: functions_310 = [] unsupported_functions_310 = [] @@ -261,6 +261,27 @@ def list_assignments(x): return x + a + b +def different_type_assignments(x): + [a, b] = {1, 2} + return x + + +def unsupported_type_assignments(x): + [a, b] = 1, 2 + return x + + +def star_assignments(x): + b, *a = [1, 2] + return x + + +def global_variable(x): + global a + a = 1 + return x + a + + functions = [ signum, early_return, @@ -297,6 +318,9 @@ def list_assignments(x): return_constant, return_constant_2, return_constant_additional_assignments, + different_type_assignments, + star_assignments, + *xfail_functions_310, ] unsupported_functions = [ @@ -306,5 +330,6 @@ def list_assignments(x): (return_end, "return needs a value"), (no_return, "Not all branches return"), (return_nothing, "return needs a value"), + (global_variable, "Unsupported statement type: "), *unsupported_functions_310, ] diff --git a/tests/functions_310.py b/tests/functions_310.py index 59e1fa2..c039468 100644 --- a/tests/functions_310.py +++ b/tests/functions_310.py @@ -155,14 +155,25 @@ def match_with_guard_multiple_variable(x): def match_sequence_incomplete(x): y = 2 - match x, y: - case 0, 1: + z = 3 + match x, y, z: + case 0, 1, 2: return 0 - case 1: + case 1, 2: return 1 + case 2: + return 2 return x +def match_mapping(x): + match x: + case {1: 2}: + return 1 + case _: + return 2 + + functions_310 = [ nested_match, match_assignments_inside_branch, @@ -175,15 +186,15 @@ def match_sequence_incomplete(x): match_with_guard, match_with_guard_variable, match_with_guard_multiple_variable, + match_sequence_incomplete, ] +xfail_functions_310 = [ + match_mapping, +] unsupported_functions_310 = [ (match_sequence_star, "starred patterns are not supported."), (match_sequence, "Matching lists is not supported."), (match_sequence_with_brackets, "Matching lists is not supported."), - ( - match_sequence_incomplete, - "Incompatible match and subject types: and ", - ), ]