diff --git a/.flake8 b/.flake8 index 0afde30b4a0e..715729f93b63 100644 --- a/.flake8 +++ b/.flake8 @@ -58,7 +58,7 @@ per-file-ignores = lib/matplotlib/backends/qt_editor/formlayout.py: F401, F403 lib/matplotlib/cbook/__init__.py: F401 lib/matplotlib/cbook/deprecation.py: F401 - lib/matplotlib/font_manager.py: E221, E251, E501 + lib/matplotlib/font_manager.py: E501 lib/matplotlib/image.py: F401, F403 lib/matplotlib/lines.py: F401 lib/matplotlib/mathtext.py: E221, E251 diff --git a/doc/api/font_manager_api.rst b/doc/api/font_manager_api.rst index 24bfefe00d32..8b698bacf0fe 100644 --- a/doc/api/font_manager_api.rst +++ b/doc/api/font_manager_api.rst @@ -7,5 +7,5 @@ :undoc-members: :show-inheritance: - - +.. autoclass:: FontEntry + :no-undoc-members: diff --git a/lib/matplotlib/font_manager.py b/lib/matplotlib/font_manager.py index 5cef1ce57c9d..5a9ed7953581 100644 --- a/lib/matplotlib/font_manager.py +++ b/lib/matplotlib/font_manager.py @@ -23,6 +23,7 @@ # - setWeights function needs improvement # - 'light' is an invalid weight value, remove it. +import dataclasses from functools import lru_cache import json import logging @@ -342,35 +343,22 @@ def findSystemFonts(fontpaths=None, fontext='ttf'): return [fname for fname in fontfiles if os.path.exists(fname)] -class FontEntry: - """ - A class for storing Font properties. It is used when populating - the font lookup dictionary. - """ - def __init__(self, - fname ='', - name ='', - style ='normal', - variant='normal', - weight ='normal', - stretch='normal', - size ='medium', - ): - self.fname = fname - self.name = name - self.style = style - self.variant = variant - self.weight = weight - self.stretch = stretch - try: - self.size = str(float(size)) - except ValueError: - self.size = size +FontEntry = dataclasses.make_dataclass( + 'FontEntry', [ + ('fname', str, dataclasses.field(default='')), + ('name', str, dataclasses.field(default='')), + ('style', str, dataclasses.field(default='normal')), + ('variant', str, dataclasses.field(default='normal')), + ('weight', str, dataclasses.field(default='normal')), + ('stretch', str, dataclasses.field(default='normal')), + ('size', str, dataclasses.field(default='medium')), + ], + namespace={ + '__doc__': """ + A class for storing Font properties. - def __repr__(self): - return "" % ( - self.name, os.path.basename(self.fname), self.style, self.variant, - self.weight, self.stretch) + It is used when populating the font lookup dictionary. + """}) def ttfFontProperty(font): @@ -630,16 +618,10 @@ class FontProperties: fontconfig. """ - def __init__(self, - family = None, - style = None, - variant= None, - weight = None, - stretch= None, - size = None, - fname = None, # if set, it's a hardcoded filename to use - math_fontfamily = None, - ): + def __init__(self, family=None, style=None, variant=None, weight=None, + stretch=None, size=None, + fname=None, # if set, it's a hardcoded filename to use + math_fontfamily=None): self._family = _normalize_font_family(rcParams['font.family']) self._slant = rcParams['font.style'] self._variant = rcParams['font.variant']