Skip to content

Commit

Permalink
Test different polars versions (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
pavelzw authored Jul 30, 2023
1 parent 1b9ce8e commit dbaaa55
Show file tree
Hide file tree
Showing 8 changed files with 239 additions and 210 deletions.
17 changes: 12 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,38 @@ defaults:

jobs:
linux-unittests:
name: Unit tests Linux - Python ${{ matrix.PYTHON_VERSION }}
name: Unit tests Linux - ${{ matrix.PYTHON_VERSION }} ${{ matrix.POLARS_VERSION }}
timeout-minutes: 15
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
PYTHON_VERSION: ['3.9', '3.10', '3.11']
include:
- { PYTHON_VERSION: 'python=3.9', POLARS_VERSION: 'polars=0.14.28' }
- { PYTHON_VERSION: 'python=3.9', POLARS_VERSION: 'polars=0.15' }
- { PYTHON_VERSION: 'python=3.9', POLARS_VERSION: 'polars=0.16' }
- { PYTHON_VERSION: 'python=3.9', POLARS_VERSION: 'polars=0.17' }
- { PYTHON_VERSION: 'python=3.9', POLARS_VERSION: 'polars=0.18' }
- { PYTHON_VERSION: 'python=3.10', POLARS_VERSION: '' }
- { PYTHON_VERSION: 'python=3.11', POLARS_VERSION: '' }
steps:
- uses: actions/checkout@v3
# TODO: move to action once it is available
- name: Set up pixi
run: |
curl -fsSL https://raw.githubusercontent.com/prefix-dev/pixi/main/install/install.sh | bash
- name: Install dependencies
# TODO: make prettier once pixi supports it
# TODO: make prettier once there are feature flags
# https://github.com/prefix-dev/pixi/issues/239
run: |
pixi add python=${{ matrix.PYTHON_VERSION }}
pixi add ${{ matrix.PYTHON_VERSION }} ${{ matrix.POLARS_VERSION }}
pixi install
pixi run postinstall
- name: Run unittests
uses: pavelzw/pytest-action@v2
with:
custom-pytest: pixi run pytest
report-title: Unit tests Linux - Python ${{ matrix.PYTHON_VERSION }}
report-title: Unit tests Linux - ${{ matrix.PYTHON_VERSION }} ${{ matrix.POLARS_VERSION }}

pre-commit-checks:
# TODO: switch to pixi once there is a good way
Expand Down
24 changes: 12 additions & 12 deletions pixi.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,20 @@ channels = ["conda-forge"]
platforms = ["linux-64", "osx-arm64", "osx-64", "win-64"]

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

[dependencies]
python = ">= 3.9"
"pip" = "*"
"polars" = "0.18.8"
python = ">=3.9"
pip = "*"
polars = ">=0.14.24,<0.19"
# build
"hatchling" = "*"
hatchling = "*"
# test
"pytest" = "*"
"pytest-md" = "*"
"pytest-emoji" = "*"
"hypothesis" = "*"
pytest = "*"
pytest-md = "*"
pytest-emoji = "*"
hypothesis = "*"
# linting
"pre-commit" = "*"
pre-commit = "*"
7 changes: 6 additions & 1 deletion polarify/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ def transform_func_to_new_source(func) -> str:
expr = parse_body(func_def.body)

# Replace the body of the function with the parsed expr
func_def.body = [ast.Return(expr)]
# Also import polars as pl since this is used in the generated code
# We don't want to rely on the user having imported polars as pl
func_def.body = [
ast.Import(names=[ast.alias(name="polars", asname="pl")]),
ast.Return(value=expr),
]
# TODO: make this prettier
func_def.decorator_list = []
func_def.name += "_polarified"
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ classifiers = [
"Programming Language :: Python :: 3.11",
]
dependencies = [
"polars == 0.18.8",
"polars >=0.14.24,<0.19",
]

[project.urls]
Expand Down
Empty file added tests/__init__.py
Empty file.
179 changes: 179 additions & 0 deletions tests/functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
# ruff: noqa
# ruff must not change the AST of the test functions, even if they are semantically equivalent.


def signum(x):
s = 0
if x > 0:
s = 1
elif x < 0:
s = -1
return s


def signum_no_default(x):
if x > 0:
return 1
elif x < 0:
return -1
return 0


def early_return(x):
if x > 0:
return 1
return 0


def assign_both_branches(x):
if x > 0:
s = 1
else:
s = -1
return s


def unary_expr(x):
s = -x
return s


def call_target_identity(x):
return x


def call_expr(x):
k = x * 2
s = call_target_identity(k + 3)
return s


def if_expr(x):
s = 1 if x > 0 else -1
return s


def if_expr2(x):
s = 1 + (x if x > 0 else -1)
return s


def if_expr3(x):
s = 1 + ((3 if x < 10 else 5) if x > 0 else -1)
return s


def compare_expr(x):
if (0 < x) & (x < 10):
s = 1
else:
s = 2
return s


def chained_compare_expr(x):
if 0 < x < 10:
s = 1
else:
s = 2
return s


def walrus_expr(x):
if (y := x + 1) > 0:
s = 1
else:
s = -1
return s * y


def multiple_if_else(x):
if x > 0:
s = 1
elif x < 0:
s = -1
else:
s = 0
return s


def nested_if_else(x):
if x > 0:
if x > 1:
s = 2
else:
s = 1
elif x < 0:
s = -1
else:
s = 0
return s


def nested_if_else_expr(x):
if x > 0:
s = 2 if x > 1 else 1
elif x < 0:
s = -1
else:
s = 0
return s


def assignments_inside_branch(x):
if x > 0:
s = 1
s = s + 1
s = x * s
elif x < 0:
s = -1
s = s - 1
s = x
else:
s = 0
return s


def override_default(x):
s = 0
if x > 0:
s = 10
return x * s


def no_if_else(x):
s = x * 10
k = x - 3
k = k * 2
return s * k


def two_if_expr(x):
a = 1 if x > 0 else 5
b = 2 if x < 0 else 2
return a + b


functions = [
signum,
early_return,
assign_both_branches,
unary_expr,
call_expr,
if_expr,
if_expr2,
if_expr3,
compare_expr,
multiple_if_else,
nested_if_else,
nested_if_else_expr,
assignments_inside_branch,
override_default,
no_if_else,
two_if_expr,
]

xfail_functions = [
walrus_expr,
signum_no_default,
]
10 changes: 10 additions & 0 deletions tests/test_error_handling.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import pytest

from polarify import polarify

from .functions import chained_compare_expr


def test_chained_compare_fail():
with pytest.raises(ValueError):
polarify(chained_compare_expr)
Loading

0 comments on commit dbaaa55

Please sign in to comment.