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

Allow Parameterized.param.update to be used as a context manager for temporary updates #779

Merged
merged 12 commits into from
Jul 11, 2023

Conversation

philippjfr
Copy link
Member

Implements #685

@philippjfr philippjfr requested review from jbednar and maximlt June 30, 2023 11:11
@maximlt
Copy link
Member

maximlt commented Jul 3, 2023

I should have chimed in #685 back in February, or maybe at the time it was not so obvious to me, but I think that .param.set and .param.update aren't distinguishable enough naming-wise, that will just cause confusion (constant/readonly kind of confusion comes to mind, since we talked about it recently with Jean-Luc).

How about modifying .param.update so that it can also be used as a context manager? Here's a prototyped implementation where calling .update returns an instance of _Update while previously (as in Param now on the main branch) it returns None. I think that's okay?

class _Update:
    
    def __init__(self, instance):
        self.instance = instance
    
    def __call__(self, **kwargs):
        self.restore = self.instance.kwargs
        self.instance._update(**kwargs)
        return self
    
    def __enter__(self):
        print('ENTER')
    
    def __exit__(self, *exc_details):
        print('EXIT')
        self.instance._update(**self.restore)
        self.restore.clear()


class Parameters:
    
    def __init__(self):
        self.kwargs = dict(a=1)
    
    def update(self, **kwargs):
        return _Update(instance=self)(**kwargs)

    def _update(self, **kwargs):
        self.kwargs = kwargs

image

If we don't go down that route, we should try to align .set and .update more (i.e. same signatures, work on both classes and instances).

@maximlt
Copy link
Member

maximlt commented Jul 10, 2023

What do you think about that @philippjfr ?

@philippjfr
Copy link
Member Author

I'll play around with it.

@philippjfr
Copy link
Member Author

Made the changes you requested, ready to review.

@maximlt maximlt changed the title Add Parameterized.param.set context manager Allow Parameterized.param.update to be used as a context manager for temporary updates Jul 11, 2023
Copy link
Member

@maximlt maximlt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This PR looked good to me overall, I completed it with:

  • more tests
  • updated docs
  • making ParametersRestore private

Thanks!

@maximlt maximlt merged commit 6b50168 into main Jul 11, 2023
@maximlt maximlt deleted the set_context branch July 11, 2023 16:20
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.

3 participants