Skip to content

Commit

Permalink
small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelzw committed May 24, 2024
1 parent 5779202 commit 10c9753
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 23 deletions.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,17 @@ polarIFy is still in an early stage of development and doesn't support the full
- assignments (like `x = 1`)
- polars expressions (like `pl.col("x")`, TODO)
- side-effect free functions that return a polars expression (can be generated by `@polarify`) (TODO)
- `match` statements

### Unsupported operations

- `for` loops
- `while` loops
- `break` statements
- `:=` walrus operator
- `match ... case` statements (TODO)
- dictionary mappings in `match` statements
- list matching in `match` statements
- star patterns in `match statements
- functions with side-effects (`print`, `pl.write_csv`, ...)

## 🚀 Benchmarks
Expand Down
11 changes: 4 additions & 7 deletions polarify/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,6 @@ def build_polars_when_then_otherwise(body: Sequence[ResolvedCase], orelse: ast.e

assert body or orelse, "No when-then cases provided."

if not body:
"""
When a match statement has no valid cases (i.e., all cases except catch-all pattern are ignored),
we return the orelse expression but the test setup does not work with literal expressions.
"""
raise ValueError("No valid cases provided.")

for test, then in body:
when_node = ast.Call(
func=ast.Attribute(
Expand Down Expand Up @@ -375,6 +368,10 @@ def transform_tree_into_expr(node: State) -> ast.expr:
if isinstance(node.node, ReturnState):
return node.node.expr
elif isinstance(node.node, ConditionalState):
if not node.node.body:
# this happens if none of the cases will ever match or exist
# in these cases we just need to return the orelse body
return transform_tree_into_expr(node.node.orelse)
return build_polars_when_then_otherwise(
[
ResolvedCase(case.test, transform_tree_into_expr(case.state))
Expand Down
4 changes: 1 addition & 3 deletions tests/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@
import sys

if sys.version_info >= (3, 10):
from .functions_310 import functions_310, unsupported_functions_310, xfail_functions_310
from .functions_310 import functions_310, unsupported_functions_310
else:
functions_310 = []
unsupported_functions_310 = []
xfail_functions_310 = []


def signum(x):
Expand Down Expand Up @@ -317,7 +316,6 @@ def global_variable(x):
different_type_assignments,
star_assignments,
global_variable,
*xfail_functions_310,
]

unsupported_functions = [
Expand Down
34 changes: 22 additions & 12 deletions tests/functions_310.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,28 +233,40 @@ def match_guarded_match_as(x):
return 3


def match_sequence_padded_length_no_case(x):
def match_sequence_unmatchable_case_smaller(x):
y = 2
z = None

match x, y, z:
case 1, 2:
return 1
case _:
return -1
return x


def match_sequence_unmatchable_case_larger(x):
y = 2
z = None

match x, y:
case 1, 2, 3:
return 1
case _:
return x * 2


def match_sequence_padded_length_return(x):
def match_sequence_unmatchable_case_smaller_return(x):
y = 1
z = 2

match x, y, z:
case 1, 2:
x = 4
return 1
return -1
return x


def match_sequence_padded_length(x):
def match_sequence_unmatchable_case(x):
y = 1
z = 2

Expand Down Expand Up @@ -294,17 +306,15 @@ def match_guard_no_assignation(x):
match_with_assignment_hard,
match_complex_subject,
match_guarded_match_as,
match_sequence_padded_length,
match_guard_no_assignation,
]

xfail_functions_310 = [
match_mapping,
match_sequence_padded_length_no_case,
match_sequence_padded_length_return,
match_sequence_unmatchable_case,
match_sequence_unmatchable_case_smaller,
match_sequence_unmatchable_case_smaller_return,
match_sequence_unmatchable_case_larger,
]

unsupported_functions_310 = [
(match_mapping, "ast.MatchMapping"),
(match_sequence_star, "starred patterns are not supported."),
(match_sequence, "Matching lists is not supported."),
(match_sequence_with_brackets, "Matching lists is not supported."),
Expand Down

0 comments on commit 10c9753

Please sign in to comment.