Use class form of data classes #27415
Conversation
| labelcolor: str = 'black' | ||
| bgcolor: str = 'yellow' |
There was a problem hiding this comment.
Couldn't these be any matplotlib color spec type?
There was a problem hiding this comment.
True, though I'm not sure this example ever would.
| ], | ||
| namespace={ | ||
| '__doc__': """ | ||
| @dataclasses.dataclass(frozen=True) |
There was a problem hiding this comment.
Making this frozen breaks JSON decoding of FontManager. Either you have to leave this unfrozen, or you have to adapt FontManager._json_decode():
matplotlib/lib/matplotlib/font_manager.py
Lines 937 to 938 in 379989e
Maybe FontEntry(**o) will do? Or you have to go with object.__setattr__ similar to https://github.com/jsonpickle/jsonpickle/pull/397/files.
There was a problem hiding this comment.
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.
d1fd397 to
5d774d4
Compare
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.
5d774d4 to
5e5962f
Compare
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
FontEntryhas gained more methods, the functional form looks less clear.Also, use one in the menu example.
PR checklist