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

Fix param.ipython auto-import #859

Merged
merged 2 commits into from
Oct 3, 2023
Merged

Fix param.ipython auto-import #859

merged 2 commits into from
Oct 3, 2023

Conversation

maximlt
Copy link
Member

@maximlt maximlt commented Oct 2, 2023

We noticed that import param wasn't enough to get the IPython display hook set up automatically while it should be given the code, instead the reactive user guide had to run import param.ipython for the hook to be set up.

There were two bugs, the first one being that error raised when trying to import the ipython.py module from parameterized.py (cannot import name 'depends' from partially initialized module 'param.depends' (most likely due to a circular import) (/Users/mliquet/dev/param/param/depends.py)) and the second one being catching the error with a bare try/except.

To fix this I had to move register_display_accessor to another module, that I called display.py but maybe there's a better name like hooks.py? I also inlined the imports required by the IPythonDisplay class.

To test this I had to refactor a little parameterized.py so it relies on a helper to find out whether we're withing an IPython context. My intention was to add a test to mock the IPython context, I managed to write a test that worked on its own but didn't manage in a reasonable amount of time to get it to work with the other tests. Posting that test here in case it's useful in the future:

import param
from param.display import _display_accessors

def test_ipython_autoloaded():
    with patch('param._utils._in_ipython') as mock_in_python:
        mock_in_python.return_value = True
        importlib.reload(param.parameterized)
        assert param.parameterized.param_pager is not None
        assert '_ipython_display_' in _display_accessors

Even if I have submitted this fix, I'm not a big fan of having the IPython display hook automatically set up on import param and would rather have Param users explicitly call a function to register it.

@maximlt maximlt requested a review from philippjfr October 2, 2023 07:00
@maximlt maximlt added this to the 2.0 milestone Oct 2, 2023
@philippjfr philippjfr merged commit 3e405b8 into main Oct 3, 2023
10 checks passed
@philippjfr philippjfr deleted the fix_import_ipython branch October 3, 2023 12:22
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.

2 participants