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

Use class form of data classes #27415

Merged
merged 2 commits into from
Jan 15, 2024
Merged

Conversation

QuLogic
Copy link
Member

@QuLogic QuLogic commented Dec 1, 2023

PR summary

When these were added in #20118, we had no type annotations, so it made sense to use the functional form. Now that we do, there's no reason not to use the class form.

Also, as FontEntry has gained more methods, the functional form looks less clear.

Also, use one in the menu example.

PR checklist

@QuLogic QuLogic added this to the v3.9.0 milestone Dec 1, 2023
Comment on lines 20 to 21
labelcolor: str = 'black'
bgcolor: str = 'yellow'
Copy link
Member

Choose a reason for hiding this comment

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

Couldn't these be any matplotlib color spec type?

Copy link
Member Author

Choose a reason for hiding this comment

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

True, though I'm not sure this example ever would.

],
namespace={
'__doc__': """
@dataclasses.dataclass(frozen=True)
Copy link
Member

Choose a reason for hiding this comment

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

Making this frozen breaks JSON decoding of FontManager. Either you have to leave this unfrozen, or you have to adapt FontManager._json_decode():

r = FontEntry.__new__(FontEntry)
r.__dict__.update(o)

Maybe FontEntry(**o) will do? Or you have to go with object.__setattr__ similar to https://github.com/jsonpickle/jsonpickle/pull/397/files.

Copy link
Member Author

Choose a reason for hiding this comment

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

We just need to avoid changing the element after creation, which we can do by modifying the input dictionary beforehand.

I'm not sure why we were doing __new__ and __dict__.update instead of FontEntry(**o), but I've moved to the latter as suggested.

When these were added in matplotlib#20118, we had no type annotations, so it made
sense to use the functional form. Now that we do, there's no reason not
to use the class form.

Also, as `FontEntry` has gained more methods, the functional form looks
less clear.
@ksunden ksunden merged commit c447b60 into matplotlib:main Jan 15, 2024
39 of 41 checks passed
@QuLogic QuLogic deleted the dataclass-class branch January 16, 2024 00:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend: pdf Documentation: API files in lib/ and doc/api Documentation: examples files in galleries/examples Maintenance
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants