Skip to content

Commit dbaaa55

Browse files
authored
Test different polars versions (#9)
1 parent 1b9ce8e commit dbaaa55

File tree

8 files changed

+239
-210
lines changed

8 files changed

+239
-210
lines changed

.github/workflows/ci.yml

+12-5
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,38 @@ defaults:
1414

1515
jobs:
1616
linux-unittests:
17-
name: Unit tests Linux - Python ${{ matrix.PYTHON_VERSION }}
17+
name: Unit tests Linux - ${{ matrix.PYTHON_VERSION }} ${{ matrix.POLARS_VERSION }}
1818
timeout-minutes: 15
1919
runs-on: ubuntu-latest
2020
strategy:
2121
fail-fast: false
2222
matrix:
23-
PYTHON_VERSION: ['3.9', '3.10', '3.11']
23+
include:
24+
- { PYTHON_VERSION: 'python=3.9', POLARS_VERSION: 'polars=0.14.28' }
25+
- { PYTHON_VERSION: 'python=3.9', POLARS_VERSION: 'polars=0.15' }
26+
- { PYTHON_VERSION: 'python=3.9', POLARS_VERSION: 'polars=0.16' }
27+
- { PYTHON_VERSION: 'python=3.9', POLARS_VERSION: 'polars=0.17' }
28+
- { PYTHON_VERSION: 'python=3.9', POLARS_VERSION: 'polars=0.18' }
29+
- { PYTHON_VERSION: 'python=3.10', POLARS_VERSION: '' }
30+
- { PYTHON_VERSION: 'python=3.11', POLARS_VERSION: '' }
2431
steps:
2532
- uses: actions/checkout@v3
2633
# TODO: move to action once it is available
2734
- name: Set up pixi
2835
run: |
2936
curl -fsSL https://raw.githubusercontent.com/prefix-dev/pixi/main/install/install.sh | bash
3037
- name: Install dependencies
31-
# TODO: make prettier once pixi supports it
38+
# TODO: make prettier once there are feature flags
3239
# https://github.com/prefix-dev/pixi/issues/239
3340
run: |
34-
pixi add python=${{ matrix.PYTHON_VERSION }}
41+
pixi add ${{ matrix.PYTHON_VERSION }} ${{ matrix.POLARS_VERSION }}
3542
pixi install
3643
pixi run postinstall
3744
- name: Run unittests
3845
uses: pavelzw/pytest-action@v2
3946
with:
4047
custom-pytest: pixi run pytest
41-
report-title: Unit tests Linux - Python ${{ matrix.PYTHON_VERSION }}
48+
report-title: Unit tests Linux - ${{ matrix.PYTHON_VERSION }} ${{ matrix.POLARS_VERSION }}
4249

4350
pre-commit-checks:
4451
# TODO: switch to pixi once there is a good way

pixi.toml

+12-12
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,20 @@ channels = ["conda-forge"]
99
platforms = ["linux-64", "osx-arm64", "osx-64", "win-64"]
1010

1111
[tasks]
12-
"postinstall" = "pip install --no-build-isolation --no-deps --disable-pip-version-check -e ."
13-
"test" = "pytest"
14-
"lint" = "pre-commit run --all"
12+
postinstall = "pip install --no-build-isolation --no-deps --disable-pip-version-check -e ."
13+
test = "pytest"
14+
lint = "pre-commit run --all"
1515

1616
[dependencies]
17-
python = ">= 3.9"
18-
"pip" = "*"
19-
"polars" = "0.18.8"
17+
python = ">=3.9"
18+
pip = "*"
19+
polars = ">=0.14.24,<0.19"
2020
# build
21-
"hatchling" = "*"
21+
hatchling = "*"
2222
# test
23-
"pytest" = "*"
24-
"pytest-md" = "*"
25-
"pytest-emoji" = "*"
26-
"hypothesis" = "*"
23+
pytest = "*"
24+
pytest-md = "*"
25+
pytest-emoji = "*"
26+
hypothesis = "*"
2727
# linting
28-
"pre-commit" = "*"
28+
pre-commit = "*"

polarify/__init__.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,12 @@ def transform_func_to_new_source(func) -> str:
1212
expr = parse_body(func_def.body)
1313

1414
# Replace the body of the function with the parsed expr
15-
func_def.body = [ast.Return(expr)]
15+
# Also import polars as pl since this is used in the generated code
16+
# We don't want to rely on the user having imported polars as pl
17+
func_def.body = [
18+
ast.Import(names=[ast.alias(name="polars", asname="pl")]),
19+
ast.Return(value=expr),
20+
]
1621
# TODO: make this prettier
1722
func_def.decorator_list = []
1823
func_def.name += "_polarified"

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ classifiers = [
2020
"Programming Language :: Python :: 3.11",
2121
]
2222
dependencies = [
23-
"polars == 0.18.8",
23+
"polars >=0.14.24,<0.19",
2424
]
2525

2626
[project.urls]

tests/__init__.py

Whitespace-only changes.

tests/functions.py

+179
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
1+
# ruff: noqa
2+
# ruff must not change the AST of the test functions, even if they are semantically equivalent.
3+
4+
5+
def signum(x):
6+
s = 0
7+
if x > 0:
8+
s = 1
9+
elif x < 0:
10+
s = -1
11+
return s
12+
13+
14+
def signum_no_default(x):
15+
if x > 0:
16+
return 1
17+
elif x < 0:
18+
return -1
19+
return 0
20+
21+
22+
def early_return(x):
23+
if x > 0:
24+
return 1
25+
return 0
26+
27+
28+
def assign_both_branches(x):
29+
if x > 0:
30+
s = 1
31+
else:
32+
s = -1
33+
return s
34+
35+
36+
def unary_expr(x):
37+
s = -x
38+
return s
39+
40+
41+
def call_target_identity(x):
42+
return x
43+
44+
45+
def call_expr(x):
46+
k = x * 2
47+
s = call_target_identity(k + 3)
48+
return s
49+
50+
51+
def if_expr(x):
52+
s = 1 if x > 0 else -1
53+
return s
54+
55+
56+
def if_expr2(x):
57+
s = 1 + (x if x > 0 else -1)
58+
return s
59+
60+
61+
def if_expr3(x):
62+
s = 1 + ((3 if x < 10 else 5) if x > 0 else -1)
63+
return s
64+
65+
66+
def compare_expr(x):
67+
if (0 < x) & (x < 10):
68+
s = 1
69+
else:
70+
s = 2
71+
return s
72+
73+
74+
def chained_compare_expr(x):
75+
if 0 < x < 10:
76+
s = 1
77+
else:
78+
s = 2
79+
return s
80+
81+
82+
def walrus_expr(x):
83+
if (y := x + 1) > 0:
84+
s = 1
85+
else:
86+
s = -1
87+
return s * y
88+
89+
90+
def multiple_if_else(x):
91+
if x > 0:
92+
s = 1
93+
elif x < 0:
94+
s = -1
95+
else:
96+
s = 0
97+
return s
98+
99+
100+
def nested_if_else(x):
101+
if x > 0:
102+
if x > 1:
103+
s = 2
104+
else:
105+
s = 1
106+
elif x < 0:
107+
s = -1
108+
else:
109+
s = 0
110+
return s
111+
112+
113+
def nested_if_else_expr(x):
114+
if x > 0:
115+
s = 2 if x > 1 else 1
116+
elif x < 0:
117+
s = -1
118+
else:
119+
s = 0
120+
return s
121+
122+
123+
def assignments_inside_branch(x):
124+
if x > 0:
125+
s = 1
126+
s = s + 1
127+
s = x * s
128+
elif x < 0:
129+
s = -1
130+
s = s - 1
131+
s = x
132+
else:
133+
s = 0
134+
return s
135+
136+
137+
def override_default(x):
138+
s = 0
139+
if x > 0:
140+
s = 10
141+
return x * s
142+
143+
144+
def no_if_else(x):
145+
s = x * 10
146+
k = x - 3
147+
k = k * 2
148+
return s * k
149+
150+
151+
def two_if_expr(x):
152+
a = 1 if x > 0 else 5
153+
b = 2 if x < 0 else 2
154+
return a + b
155+
156+
157+
functions = [
158+
signum,
159+
early_return,
160+
assign_both_branches,
161+
unary_expr,
162+
call_expr,
163+
if_expr,
164+
if_expr2,
165+
if_expr3,
166+
compare_expr,
167+
multiple_if_else,
168+
nested_if_else,
169+
nested_if_else_expr,
170+
assignments_inside_branch,
171+
override_default,
172+
no_if_else,
173+
two_if_expr,
174+
]
175+
176+
xfail_functions = [
177+
walrus_expr,
178+
signum_no_default,
179+
]

tests/test_error_handling.py

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import pytest
2+
3+
from polarify import polarify
4+
5+
from .functions import chained_compare_expr
6+
7+
8+
def test_chained_compare_fail():
9+
with pytest.raises(ValueError):
10+
polarify(chained_compare_expr)

0 commit comments

Comments
 (0)