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

[Bug]: make_norm_from_scale should create picklable classes even when used in-line. #20755

Open
anntzer opened this issue Jul 28, 2021 · 0 comments
Open

Comments

@anntzer
Copy link
Contributor

@anntzer anntzer commented Jul 28, 2021

Bug summary

The new matplotlib.colors.make_norm_from_scale helper dynamically generates a norm class from a scale class. Currently, in the codebase, it is only used as a decorator to create "toplevel" classes (e.g., it is used to generate LogNorm from LogScale, etc.), but it can also be used within other functions to dynamically generate a norm class based on a user-given arbitrary scale (see #20752 for an example of application, which is however not necessary to understand for what follows). In the latter case, the dynamically generate class is currently not picklable (because pickling of classes relies on the existence of global names, see e.g. "classes are pickled by named reference"). It would be generally useful to get rid of this restriction, which can be done by implementing the __reduce__ protocol; there's already other examples in the codebase of dynamically generated classes that use the same mechanism).

I'm tagging this as "good first issue" because there's no API design and the eng goal is clear, but medium (perhaps hard) difficulty because it requires somewhat sophisticated understanding of the details of the pickling process.

Code for reproduction

pickle.dumps(matplotlib.colors.make_norm_from_scale(matplotlib.scale.LogitScale, matplotlib.colors.Normalize))

Actual outcome

Can't pickle <class 'matplotlib.colors.Normalize'>: it's not the same object as matplotlib.colors.Normalize

(Note the additional confusion here: there's two classes that are both named matplotlib.colors.Normalize -- the original one and the dynamically generated one -- but they are different.)

Expected outcome

A correct round-trippable pickle.

Operating system

No response

Matplotlib Version

master (unreleased, pre 3.5)

Matplotlib Backend

No response

Python version

No response

Jupyter version

No response

Other libraries

No response

Installation

source

Conda channel

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
1 participant