Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use decorators to delay the application of given #7

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

keewis
Copy link
Contributor

@keewis keewis commented Aug 23, 2024

The idea is to use the less complicated form of metaprogramming in python (decorators) to delay the call to hypothesis.given. This is done by applying the function decorator delayed_given, which stores strategies and partial strategy functions passed to it in a new property on the test function.

Then when a test class is created, we apply the class decorator initialize_tests that iterates over the test methods, accesses the property we just defined, evaluates any partial strategy functions with a fixed set of parameters, passes the result to given and finally replaces the test function with its decorated version.

As an example, this would amount to something similar to:

class TestNumpyReduction(ReductionTests, NumpyTestMixin):
    ...
    
    given_args, given_kwargs = instantiate_given(
        test_variable.__hypothesis_given__,
        array_strategy_fn=array_strategy_fn,
    )
    test_variable = given(*given_args, **given_kwargs)(test_variable)
    
    ...

Assuming this works properly (I'll still have to write tests), we can define test classes like this:

class ReductionTests(DuckArrayTestMixin):
    @pytest.mark.parametrize("op", ["cumsum", "cumprod"])
    @delayed_given(partial(xrst.variables))
    def test_variable_cumulative(self, op, variable):
        ...

and they'd be used like this:

@initialize_tests
class TestReductionNumpy(ReductionTests, NumpyTestMixin):
    pass

This is obviously a bit more complex than normal code, but the end result does not look too magical to me. What do you think?

cc @TomNicholas, @Zac-HD

@keewis keewis mentioned this pull request Aug 23, 2024
@keewis keewis marked this pull request as draft August 23, 2024 13:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant