Skip to content

Commit

Permalink
Fixing Coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
prsabahrami committed May 15, 2024
1 parent 56279e9 commit 1d7a295
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 15 deletions.
16 changes: 9 additions & 7 deletions polarify/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand Down
27 changes: 26 additions & 1 deletion tests/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 = [
Expand All @@ -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: <class 'ast.Global'>"),
*unsupported_functions_310,
]
25 changes: 18 additions & 7 deletions tests/functions_310.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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: <class 'ast.MatchValue'> and <class 'ast.Tuple'>",
),
]

0 comments on commit 1d7a295

Please sign in to comment.