Skip to content

Commit b5a206e

Browse files
authored
Merge pull request matplotlib#20118 from QuLogic/fe-dc
MNT: Convert FontEntry to a data class
2 parents 86d6a0d + 8e075a1 commit b5a206e

File tree

3 files changed

+23
-41
lines changed

3 files changed

+23
-41
lines changed

.flake8

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ per-file-ignores =
5858
lib/matplotlib/backends/qt_editor/formlayout.py: F401, F403
5959
lib/matplotlib/cbook/__init__.py: F401
6060
lib/matplotlib/cbook/deprecation.py: F401
61-
lib/matplotlib/font_manager.py: E221, E251, E501
61+
lib/matplotlib/font_manager.py: E501
6262
lib/matplotlib/image.py: F401, F403
6363
lib/matplotlib/lines.py: F401
6464
lib/matplotlib/mathtext.py: E221, E251

doc/api/font_manager_api.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@
77
:undoc-members:
88
:show-inheritance:
99

10-
11-
10+
.. autoclass:: FontEntry
11+
:no-undoc-members:

lib/matplotlib/font_manager.py

Lines changed: 20 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
# - setWeights function needs improvement
2424
# - 'light' is an invalid weight value, remove it.
2525

26+
import dataclasses
2627
from functools import lru_cache
2728
import json
2829
import logging
@@ -342,35 +343,22 @@ def findSystemFonts(fontpaths=None, fontext='ttf'):
342343
return [fname for fname in fontfiles if os.path.exists(fname)]
343344

344345

345-
class FontEntry:
346-
"""
347-
A class for storing Font properties. It is used when populating
348-
the font lookup dictionary.
349-
"""
350-
def __init__(self,
351-
fname ='',
352-
name ='',
353-
style ='normal',
354-
variant='normal',
355-
weight ='normal',
356-
stretch='normal',
357-
size ='medium',
358-
):
359-
self.fname = fname
360-
self.name = name
361-
self.style = style
362-
self.variant = variant
363-
self.weight = weight
364-
self.stretch = stretch
365-
try:
366-
self.size = str(float(size))
367-
except ValueError:
368-
self.size = size
346+
FontEntry = dataclasses.make_dataclass(
347+
'FontEntry', [
348+
('fname', str, dataclasses.field(default='')),
349+
('name', str, dataclasses.field(default='')),
350+
('style', str, dataclasses.field(default='normal')),
351+
('variant', str, dataclasses.field(default='normal')),
352+
('weight', str, dataclasses.field(default='normal')),
353+
('stretch', str, dataclasses.field(default='normal')),
354+
('size', str, dataclasses.field(default='medium')),
355+
],
356+
namespace={
357+
'__doc__': """
358+
A class for storing Font properties.
369359
370-
def __repr__(self):
371-
return "<Font '%s' (%s) %s %s %s %s>" % (
372-
self.name, os.path.basename(self.fname), self.style, self.variant,
373-
self.weight, self.stretch)
360+
It is used when populating the font lookup dictionary.
361+
"""})
374362

375363

376364
def ttfFontProperty(font):
@@ -630,16 +618,10 @@ class FontProperties:
630618
fontconfig.
631619
"""
632620

633-
def __init__(self,
634-
family = None,
635-
style = None,
636-
variant= None,
637-
weight = None,
638-
stretch= None,
639-
size = None,
640-
fname = None, # if set, it's a hardcoded filename to use
641-
math_fontfamily = None,
642-
):
621+
def __init__(self, family=None, style=None, variant=None, weight=None,
622+
stretch=None, size=None,
623+
fname=None, # if set, it's a hardcoded filename to use
624+
math_fontfamily=None):
643625
self._family = _normalize_font_family(rcParams['font.family'])
644626
self._slant = rcParams['font.style']
645627
self._variant = rcParams['font.variant']

0 commit comments

Comments
 (0)