GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — use decorators to delay the application of `given` by keewis · Pull Request #7 · xarray-contrib/xarray-array-testing

Conversation

@keewis

@keewis keewis commented Aug 23, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member
  • works around the failing health check discussed in Cubed xarray tests #4 (comment)

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
keewis added 3 commits August 23, 2024 15:12
@keewis
define decorators to delay given
dfc9634
@keewis
use delayed_given in the existing tests
476e017
@keewis
use initialize_tests on the inherited classes
c9f23e5
@keewis keewis mentioned this pull request Aug 23, 2024
Cubed xarray tests #4
Open
@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

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

1 participant

@keewis
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.