Skip to content

fix: keep isinstance(fig, go.Figure) working after register_plotly_resampler - #372

Open
raashish1601 wants to merge 1 commit into
predict-idlab:mainfrom
raashish1601:fix/364-isinstance-figure
Open

raashish1601 wants to merge 1 commit into
predict-idlab:mainfrom
raashish1601:fix/364-isinstance-figure

Conversation

@raashish1601

Copy link
Copy Markdown

Fixes #364

register_plotly_resampler() replaced go.Figure and go.FigureWidget with plain functions. That made isinstance(fig, go.Figure) raise TypeError: isinstance() arg 2 must be a type..., which breaks any downstream code that checks figure types.

The wrappers are now small classes instead of functions. Calling one still returns a FigureResampler / FigureWidgetResampler exactly as before (__new__ builds it, so __init__ never runs). Their metaclass forwards isinstance, issubclass and class attribute lookups to the original plotly class. So after registering, isinstance(fig, go.Figure) is True, issubclass(FigureResampler, go.Figure) is True, and go.Figure.<attr> still works like it did with the functools.wraps function. Unregistering and _get_plotly_constr work the same way through __wrapped__.

Tests: added test_isinstance_when_registered to tests/test_registering.py. It covers figure and widget mode, issubclass, and checks after unregistering. The existing tests asserted that the registered constructors are functions (inspect.isfunction), so they now use _already_wrapped instead. test_registering.py, test_composability.py and test_plotly_express.py pass. In test_serialization.py, the three FigureWidget copy tests fail locally both with and without this change, because my env has ipywidgets 8 (Widgets cannot be copied) and the project pins 7. ruff check and black --check are clean.

I also added a line to the "Latest" section of the changelog.

…sampler (predict-idlab#364)

register_plotly_resampler replaced go.Figure and go.FigureWidget with
plain functions, so isinstance(fig, go.Figure) raised a TypeError.
Replace them with classes whose metaclass forwards isinstance,
issubclass and attribute lookups to the original plotly class.

Fixes predict-idlab#364

This branch has not been deployed

No deployments
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.

[BUG] isinstance(fig, go.Figure) raises TypeError after activating plotly-resampler

1 participant