Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion plugins/python/python/phlex/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def __init__(
if v.default is inspect.Parameter.empty:
raise MissingAnnotation(k) from e

self.__annotations__['return'] = annotations.get('return', None)
self.__annotations__["return"] = annotations.get("return", None)

self.__name__ = name
self.__code__ = getattr(self.phlex_callable, "__code__", None)
Expand Down
25 changes: 22 additions & 3 deletions test/python/reducer.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,21 @@ def add(i: int, j: int) -> int:
return i + j


def add_sum01(sum0: int, sum1: int) -> int:
"""Add sum0 and sum1."""
return sum0 + sum1


def add_sum23(sum2: int, sum3: int) -> int:
"""Add sum2 and sum3."""
return sum2 + sum3


def add_final(sum01: int, sum23: int) -> int:
"""Add sum01 and sum23."""
return sum01 + sum23


def PHLEX_REGISTER_ALGORITHMS(m, config):
"""Register a series of `add` algorithm as transformations.

Expand All @@ -55,8 +70,12 @@ def PHLEX_REGISTER_ALGORITHMS(m, config):
)

# now reduce them pair-wise
m.transform(add, name="reduce01", input_family=["sum0", "sum1"], output_products=["sum01"])
m.transform(add, name="reduce23", input_family=["sum2", "sum3"], output_products=["sum23"])
m.transform(
add_sum01, name="reduce01", input_family=["sum0", "sum1"], output_products=["sum01"]
)
m.transform(
add_sum23, name="reduce23", input_family=["sum2", "sum3"], output_products=["sum23"]
)

# once more (and the configuration will add a verifier)
m.transform(add, name="reduce", input_family=["sum01", "sum23"], output_products=["sum"])
m.transform(add_final, name="reduce", input_family=["sum01", "sum23"], output_products=["sum"])
18 changes: 9 additions & 9 deletions test/python/test_callbacks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,39 @@


# 3-argument function to trigger py_callback<3>
def sum_three(a: int, b: int, c: int) -> int:
def sum_three(i: int, j: int, k: int) -> int:
"""Sum three integers."""
return a + b + c
return i + j + k


# Function that raises exception to test error handling
def raise_error(a: int) -> int:
def raise_error(i: int) -> int:
"""Raise a RuntimeError."""
raise RuntimeError("Intentional failure")


# Invalid bool return (2)
def bad_bool(a: int) -> bool:
def bad_bool(i: int) -> bool:
"""Return an invalid boolean value."""
return 2 # type: ignore


# Invalid long return (float)
def bad_long(a: int) -> "long": # type: ignore # noqa: F821
def bad_long(i: int) -> "long": # type: ignore # noqa: F821
"""Return a float instead of an int."""
return 1.5 # type: ignore


# Invalid uint return (negative)
def bad_uint(a: int) -> "unsigned int": # type: ignore # noqa: F722
def bad_uint(i: int) -> "unsigned int": # type: ignore # noqa: F722
"""Return a negative value for unsigned int."""
return -5 # type: ignore


# Function with mismatching annotation count vs config inputs
def two_args(a: int, b: int) -> int:
"""Sum two integers."""
return a + b
def two_args(i: int, j: int) -> int:
"""Sum two integers while config provides three inputs (tests parameter count mismatch)."""
return i + j


def PHLEX_REGISTER_ALGORITHMS(m, config):
Expand Down
8 changes: 4 additions & 4 deletions test/python/test_coverage.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ def collect_int(i: int) -> list[int]:
return [i]


def collect_float(f: float) -> list[float]:
def collect_float(f1: float) -> list[float]:
"""Collect a float into a list."""
return [f]
return [f1]


def collect_double(d: "double") -> "list[double]": # type: ignore
def collect_double(d1: "double") -> "list[double]": # type: ignore
"""Collect a double into a list."""
return [d]
return [d1]


def PHLEX_REGISTER_ALGORITHMS(m, config):
Expand Down
2 changes: 1 addition & 1 deletion test/python/test_mismatch.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Test mismatch between input labels and types."""


def mismatch_func(a: int, b: int):
def mismatch_func(a: int, b: int) -> int:
"""Add two integers."""
return a + b

Expand Down
52 changes: 26 additions & 26 deletions test/python/test_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,82 +14,82 @@ class double(float): # noqa: N801
pass


def add_float(i: float, j: float) -> float:
def add_float(f1: float, f2: float) -> float:
"""Add two floats.

Args:
i (float): First input.
j (float): Second input.
f1 (float): First input.
f2 (float): Second input.

Returns:
float: Sum of the two inputs.
"""
return i + j
return f1 + f2


def add_double(i: double, j: double) -> double:
def add_double(d1: double, d2: double) -> double:
"""Add two doubles.

Args:
i (float): First input.
j (float): Second input.
d1 (double): First input.
d2 (double): Second input.

Returns:
float: Sum of the two inputs.
double: Sum of the two inputs.
"""
return double(i + j)
return double(d1 + d2)


def add_unsigned(i: "unsigned int", j: "unsigned int") -> "unsigned int": # type: ignore # noqa: F722
def add_unsigned(u1: "unsigned int", u2: "unsigned int") -> "unsigned int": # type: ignore # noqa: F722
"""Add two unsigned integers.

Args:
i (int): First input.
j (int): Second input.
u1 (unsigned int): First input.
u2 (unsigned int): Second input.

Returns:
int: Sum of the two inputs.
unsigned int: Sum of the two inputs.
"""
return i + j
return u1 + u2


def collect_float(i: float, j: float) -> npt.NDArray[np.float32]:
def collect_float(f1: float, f2: float) -> npt.NDArray[np.float32]:
"""Combine floats into a numpy array.

Args:
i (float): First input.
j (float): Second input.
f1 (float): First input.
f2 (float): Second input.

Returns:
ndarray: Array of floats.
"""
return np.array([i, j], dtype=np.float32)
return np.array([f1, f2], dtype=np.float32)


def collect_double(i: double, j: double) -> npt.NDArray[np.float64]:
def collect_double(d1: double, d2: double) -> npt.NDArray[np.float64]:
"""Combine doubles into a numpy array.

Args:
i (float): First input.
j (float): Second input.
d1 (double): First input.
d2 (double): Second input.

Returns:
ndarray: Array of doubles.
"""
return np.array([i, j], dtype=np.float64)
return np.array([d1, d2], dtype=np.float64)


def and_bool(i: bool, j: bool) -> bool:
def and_bool(b1: bool, b2: bool) -> bool:
"""And two booleans.

Args:
i (bool): First input.
j (bool): Second input.
b1 (bool): First input.
b2 (bool): Second input.

Returns:
bool: Logical AND of the two inputs.
"""
return i and j
return b1 and b2


def PHLEX_REGISTER_ALGORITHMS(m, config):
Expand Down
13 changes: 10 additions & 3 deletions test/python/unit_test_variant.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ def example_func(a, b=1):
"""Example function for testing."""
return a + b


ann = {"a": int, "b": int, "return": int}


class TestVariant(unittest.TestCase):
"""Tests for Variant wrapper."""

Expand Down Expand Up @@ -48,7 +50,8 @@ def test_clone_shallow(self):

# Test valid copy logic with a mutable callable
class CallableObj:
def __call__(self): pass
def __call__(self):
pass

obj = CallableObj()
wrapper_obj = Variant(obj, {}, "obj_clone", clone=True)
Expand All @@ -57,9 +60,13 @@ def __call__(self): pass

def test_clone_deep(self):
"""Test deep cloning behavior."""

class Container:
def __init__(self): self.data = [1]
def __call__(self): return self.data[0]
def __init__(self):
self.data = [1]

def __call__(self):
return self.data[0]

c = Container()
wrapper = Variant(c, {}, "deep_clone", clone="deep")
Expand Down
48 changes: 24 additions & 24 deletions test/python/vectypes.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,21 +59,21 @@ def sum_array_int32(coll: npt.NDArray[np.int32]) -> int:


def collectify_uint32(
i: unsigned_int,
j: unsigned_int,
u1: unsigned_int,
u2: unsigned_int,
) -> npt.NDArray[np.uint32]:
"""Create a uint32 array from two integers."""
return np.array([i, j], dtype=np.uint32)
return np.array([u1, u2], dtype=np.uint32)


def sum_array_uint32(coll: npt.NDArray[np.uint32]) -> unsigned_int:
"""Sum a uint32 array."""
return unsigned_int(sum(int(x) for x in coll))


def collectify_int64(i: long, j: long) -> npt.NDArray[np.int64]:
def collectify_int64(l1: long, l2: long) -> npt.NDArray[np.int64]:
"""Create an int64 array from two integers."""
return np.array([i, j], dtype=np.int64)
return np.array([l1, l2], dtype=np.int64)


def sum_array_int64(coll: npt.NDArray[np.int64]) -> long:
Expand All @@ -82,41 +82,41 @@ def sum_array_int64(coll: npt.NDArray[np.int64]) -> long:


def collectify_uint64(
i: unsigned_long,
j: unsigned_long,
ul1: unsigned_long,
ul2: unsigned_long,
) -> npt.NDArray[np.uint64]:
"""Create a uint64 array from two integers."""
return np.array([i, j], dtype=np.uint64)
return np.array([ul1, ul2], dtype=np.uint64)


def sum_array_uint64(coll: npt.NDArray[np.uint64]) -> unsigned_long:
"""Sum a uint64 array."""
return unsigned_long(sum(int(x) for x in coll))


def collectify_float32(i: float, j: float) -> npt.NDArray[np.float32]:
def collectify_float32(f1: float, f2: float) -> npt.NDArray[np.float32]:
"""Create a float32 array from two floats."""
return np.array([i, j], dtype=np.float32)
return np.array([f1, f2], dtype=np.float32)


def sum_array_float32(coll: npt.NDArray[np.float32]) -> float:
"""Sum a float32 array."""
return float(sum(coll))


def collectify_float64(i: double, j: double) -> npt.NDArray[np.float64]:
def collectify_float64(d1: double, d2: double) -> npt.NDArray[np.float64]:
"""Create a float64 array from two floats."""
return np.array([i, j], dtype=np.float64)
return np.array([d1, d2], dtype=np.float64)


def collectify_float32_list(i: float, j: float) -> list[float]:
def collectify_float32_list(f1: float, f2: float) -> list[float]:
"""Create a float32 list from two floats."""
return [i, j]
return [f1, f2]


def collectify_float64_list(i: double, j: double) -> list["double"]:
def collectify_float64_list(d1: double, d2: double) -> list["double"]:
"""Create a float64 list from two floats."""
return [i, j]
return [d1, d2]


def sum_array_float64(coll: npt.NDArray[np.float64]) -> double:
Expand All @@ -130,24 +130,24 @@ def collectify_int32_list(i: int, j: int) -> list[int]:


def collectify_uint32_list(
i: unsigned_int,
j: unsigned_int,
u1: unsigned_int,
u2: unsigned_int,
) -> "list[unsigned int]": # type: ignore # noqa: F722
"""Create a uint32 list from two integers."""
return [unsigned_int(i), unsigned_int(j)]
return [unsigned_int(u1), unsigned_int(u2)]


def collectify_int64_list(i: long, j: long) -> "list[long]": # type: ignore # noqa: F722
def collectify_int64_list(l1: long, l2: long) -> "list[long]": # type: ignore # noqa: F722
"""Create an int64 list from two integers."""
return [long(i), long(j)]
return [long(l1), long(l2)]


def collectify_uint64_list(
i: unsigned_long,
j: unsigned_long,
ul1: unsigned_long,
ul2: unsigned_long,
) -> "list[unsigned long]": # type: ignore # noqa: F722
"""Create a uint64 list from two integers."""
return [unsigned_long(i), unsigned_long(j)]
return [unsigned_long(ul1), unsigned_long(ul2)]


def sum_list_int32(coll: list[int]) -> int:
Expand Down
Loading