diff --git a/plugins/python/python/phlex/__init__.py b/plugins/python/python/phlex/__init__.py index 868ee4d20..b21384ff4 100644 --- a/plugins/python/python/phlex/__init__.py +++ b/plugins/python/python/phlex/__init__.py @@ -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) diff --git a/test/python/reducer.py b/test/python/reducer.py index 2ced48de7..855d35313 100644 --- a/test/python/reducer.py +++ b/test/python/reducer.py @@ -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. @@ -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"]) diff --git a/test/python/test_callbacks.py b/test/python/test_callbacks.py index b43e41931..b9e92c5c5 100644 --- a/test/python/test_callbacks.py +++ b/test/python/test_callbacks.py @@ -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): diff --git a/test/python/test_coverage.py b/test/python/test_coverage.py index 04dc3cf5d..40f890899 100644 --- a/test/python/test_coverage.py +++ b/test/python/test_coverage.py @@ -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): diff --git a/test/python/test_mismatch.py b/test/python/test_mismatch.py index e8f2f769a..d84f27e8c 100644 --- a/test/python/test_mismatch.py +++ b/test/python/test_mismatch.py @@ -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 diff --git a/test/python/test_types.py b/test/python/test_types.py index bacd3a098..d2189a709 100644 --- a/test/python/test_types.py +++ b/test/python/test_types.py @@ -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): diff --git a/test/python/unit_test_variant.py b/test/python/unit_test_variant.py index 2f2357fdc..a2cd27dea 100644 --- a/test/python/unit_test_variant.py +++ b/test/python/unit_test_variant.py @@ -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.""" @@ -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) @@ -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") diff --git a/test/python/vectypes.py b/test/python/vectypes.py index 624f7f12c..4763621b7 100644 --- a/test/python/vectypes.py +++ b/test/python/vectypes.py @@ -59,11 +59,11 @@ 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: @@ -71,9 +71,9 @@ def sum_array_uint32(coll: npt.NDArray[np.uint32]) -> unsigned_int: 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: @@ -82,11 +82,11 @@ 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: @@ -94,9 +94,9 @@ def sum_array_uint64(coll: npt.NDArray[np.uint64]) -> unsigned_long: 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: @@ -104,19 +104,19 @@ def sum_array_float32(coll: npt.NDArray[np.float32]) -> float: 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: @@ -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: diff --git a/test/python/verify.py b/test/python/verify.py index 5fb46c9c8..82b7c68db 100644 --- a/test/python/verify.py +++ b/test/python/verify.py @@ -36,22 +36,56 @@ def __init__(self, sum_total: int): """ self._sum_total = sum_total - def __call__(self, value: int) -> None: - """Verify a the `value`. + def __call__(self, sum: int) -> None: + """Verify the `sum`. - Check that `value` matches the pre-registered value. + Check that `sum` matches the pre-registered value. Args: - value (int): The value to verify. + sum (int): The value to verify. Raises: - AssertionError: if the provided value does not matches the - pre-registed value. + AssertionError: if the provided value does not match the + pre-registered value. Returns: None """ - assert value == self._sum_total + assert sum == self._sum_total + + +class VerifierSumIjk: + """A callable class that can assert an expected value for sum_ijk input.""" + + __name__ = "verifier_sum_ijk" + + def __init__(self, sum_total: int): + """Create a verifier object. + + Args: + sum_total (int): The expected value. + + Returns: + None + """ + self._sum_total = sum_total + + def __call__(self, sum_ijk: int) -> None: + """Verify the `sum_ijk`. + + Check that `sum_ijk` matches the pre-registered value. + + Args: + sum_ijk (int): The value to verify. + + Raises: + AssertionError: if the provided value does not match the + pre-registered value. + + Returns: + None + """ + assert sum_ijk == self._sum_total class BoolVerifier: @@ -63,9 +97,9 @@ def __init__(self, expected: bool): """Create a boolean verifier.""" self._expected = expected - def __call__(self, value: bool) -> None: + def __call__(self, out_bool: bool) -> None: """Verify the boolean value.""" - assert value == self._expected + assert out_bool == self._expected def PHLEX_REGISTER_ALGORITHMS(m, config): @@ -87,8 +121,12 @@ def PHLEX_REGISTER_ALGORITHMS(m, config): v = BoolVerifier(expected) m.observe(v, input_family=config["input"]) return - except Exception: + except KeyError: pass - assert_sum = Verifier(config["sum_total"]) + # Check if this is for sum_ijk (from callback3 test) + if config["input"] == ["sum_ijk"]: + assert_sum = VerifierSumIjk(config["sum_total"]) + else: + assert_sum = Verifier(config["sum_total"]) m.observe(assert_sum, input_family=config["input"])