From 97bb360f2b7f23350fd10dcde6c3751121544445 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Fri, 4 Sep 2026 14:57:53 -0400 Subject: [PATCH] Reduce API surface of internal dataclasses In almost all cases, these are not modified, so they can be marked `frozen` and with `slots` to make them immutable. We can also drop the `__eq__` method for _really_ internal things that are just data. --- galleries/examples/widgets/menu.py | 2 +- lib/matplotlib/dviread.py | 2 +- lib/matplotlib/dviread.pyi | 2 +- lib/matplotlib/font_manager.py | 4 ++-- lib/matplotlib/font_manager.pyi | 2 +- lib/matplotlib/rcsetup.py | 6 +++--- lib/mpl_toolkits/mplot3d/axis3d.py | 10 +++++----- 7 files changed, 14 insertions(+), 14 deletions(-) diff --git a/galleries/examples/widgets/menu.py b/galleries/examples/widgets/menu.py index 0591b40a29ef..acf12f3b1765 100644 --- a/galleries/examples/widgets/menu.py +++ b/galleries/examples/widgets/menu.py @@ -15,7 +15,7 @@ from matplotlib.typing import ColorType -@dataclass +@dataclass(frozen=True, kw_only=True, slots=True) class ItemProperties: fontsize: float = 14 labelcolor: ColorType = 'black' diff --git a/lib/matplotlib/dviread.py b/lib/matplotlib/dviread.py index 979744d1ef5c..521f75ed7eab 100644 --- a/lib/matplotlib/dviread.py +++ b/lib/matplotlib/dviread.py @@ -953,7 +953,7 @@ def _mul1220(num1, num2): return (num1*num2) >> 20 -@dataclasses.dataclass(frozen=True, kw_only=True) +@dataclasses.dataclass(frozen=True, kw_only=True, slots=True) class TexMetrics: """ Metrics of a glyph, with TeX semantics. diff --git a/lib/matplotlib/dviread.pyi b/lib/matplotlib/dviread.pyi index b1039fb45b41..334c43f3f52b 100644 --- a/lib/matplotlib/dviread.pyi +++ b/lib/matplotlib/dviread.pyi @@ -89,7 +89,7 @@ class Vf(Dvi): def __init__(self, filename: str | os.PathLike) -> None: ... def __getitem__(self, code: int) -> Page: ... -@dataclasses.dataclass(frozen=True, kw_only=True) +@dataclasses.dataclass(frozen=True, kw_only=True, slots=True) class TexMetrics: tex_width: int tex_height: int diff --git a/lib/matplotlib/font_manager.py b/lib/matplotlib/font_manager.py index acae3cfd15ea..515fd8bd3856 100644 --- a/lib/matplotlib/font_manager.py +++ b/lib/matplotlib/font_manager.py @@ -386,7 +386,7 @@ def __repr__(self): return f'FontPath{self._as_tuple()}' -@dataclasses.dataclass(frozen=True) +@dataclasses.dataclass(frozen=True, slots=True) class FontEntry: """ A class for storing Font properties. @@ -1076,7 +1076,7 @@ def default(self, o): if isinstance(o, FontManager): return dict(o.__dict__, __class__='FontManager') elif isinstance(o, FontEntry): - d = dict(o.__dict__, __class__='FontEntry') + d = dict(dataclasses.asdict(o), __class__='FontEntry') try: # Cache paths of fonts shipped with Matplotlib relative to the # Matplotlib data path, which helps in the presence of venvs. diff --git a/lib/matplotlib/font_manager.pyi b/lib/matplotlib/font_manager.pyi index 2e9b2fcf99f0..b58d0a56e4c0 100644 --- a/lib/matplotlib/font_manager.pyi +++ b/lib/matplotlib/font_manager.pyi @@ -46,7 +46,7 @@ class FontPath(str): def __hash__(self) -> int: ... def __repr__(self) -> str: ... -@dataclass +@dataclass(frozen=True, slots=True) class FontEntry: fname: str = ... index: int = ... diff --git a/lib/matplotlib/rcsetup.py b/lib/matplotlib/rcsetup.py index a0323bd6fd01..8c0b304c9bc0 100644 --- a/lib/matplotlib/rcsetup.py +++ b/lib/matplotlib/rcsetup.py @@ -1519,7 +1519,7 @@ def _convert_validator_spec(key, conv): type _MarkerType = int | str -@dataclass +@dataclass(eq=False, frozen=True, slots=True) class _Param: name: str default: Any @@ -1535,13 +1535,13 @@ class _Param: description: str | None = None -@dataclass +@dataclass(eq=False, frozen=True, slots=True) class _Section: title: str description: str | None = None -@dataclass +@dataclass(eq=False, frozen=True, slots=True) class _Subsection: title: str description: str | None = None diff --git a/lib/mpl_toolkits/mplot3d/axis3d.py b/lib/mpl_toolkits/mplot3d/axis3d.py index fc62b045e367..362f7fe46bfc 100644 --- a/lib/mpl_toolkits/mplot3d/axis3d.py +++ b/lib/mpl_toolkits/mplot3d/axis3d.py @@ -36,7 +36,7 @@ def _tick_update_position(tick, tickxs, tickys, labelpos): tick.gridline.set_data([0], [0]) -@dataclass +@dataclass(eq=False, frozen=True, slots=True) class _UpdatedArtists: """ Class to supply artists that have been updated ready to draw. @@ -44,10 +44,10 @@ class _UpdatedArtists: We track the types of artists individually, because they need different handling in the tightbox calculation. """ - line: mlines.Line2D = None - ticks: list = None - offset_text: mtext.Text = None - label: mtext.Text = None + line: mlines.Line2D | None = None + ticks: list | None = None + offset_text: mtext.Text | None = None + label: mtext.Text | None = None def __iter__(self): """Yield all updated artists in the correct order for drawing"""