8000 [Bug]: make_norm_from_scale should create picklable classes even when used in-line. · Issue #20755 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content
[Bug]: make_norm_from_scale should create picklable classes even when used in-line. #20755
@anntzer

Description

@anntzer

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Difficulty: Hardhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issuesDifficulty: Mediumhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issues

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0