Repository: matplotlib/matplotlib
File: lib/matplotlib/_afm.py (Python)

Code before:
        except ValueError:
            _log.error('Value error parsing header in AFM: %r, %r', key, val)
            continue
        if key == b'StartCharMetrics':
            break
    else:
        raise RuntimeError('Bad parse')
    return d


class CharMetrics(NamedTuple):
    """
    Represents the character metrics of a single character.

    Notes
    -----
    The fields do currently only describe a subset of character metrics
    information defined in the AFM standard.
    """

    width: float
    #: The character width (WX).
    name: str
    #: The character name (N).
    bbox: tuple[int, int, int, int]
    #: The bbox of the character (B) as a tuple (*llx*, *lly*, *urx*, *ury*).


def _parse_char_metrics(fh: BinaryIO) -> tuple[dict[int, CharMetrics],
                                               dict[str, CharMetrics]]:
    """
    Parse the given filehandle for character metrics information.

    It is assumed that the file cursor is on the line behind 'StartCharMetrics'.

    Returns
    -------
    ascii_d : dict
         A mapping "ASCII num of the character" to `.CharMetrics`.
    name_d : dict
         A mapping "character name" to `.CharMetrics`.

    Notes
    -----
    This function is incomplete per the standard, but thus far parses
    all the sample afm files tried.
    """
    required_keys = {'C', 'WX', 'N', 'B'}

    ascii_d: dict[int, CharMetrics] = {}
    name_d: dict[str, CharMetrics] = {}

Review comment (suggestion):
This does not include these comments as docstrings, which I think we would still prefer

I think we should be able to do the `__doc__` manual lines still for the attributes

Code after:
        except ValueError:
            _log.error('Value error parsing header in AFM: %r, %r', key, val)
            continue
        if key == b'StartCharMetrics':
            break
    else:
        raise RuntimeError('Bad parse')
    return d


class CharMetrics(NamedTuple):
    """
    Represents the character metrics of a single character.

    Notes
    -----
    The fields do currently only describe a subset of character metrics
    information defined in the AFM standard.
    """

    width: float
    name: str
    bbox: tuple[int, int, int, int]


CharMetrics.width.__doc__ = """The character width (WX)."""
CharMetrics.name.__doc__ = """The character name (N)."""
CharMetrics.bbox.__doc__ = """
    The bbox of the character (B) as a tuple (*llx*, *lly*, *urx*, *ury*)."""


def _parse_char_metrics(fh: BinaryIO) -> tuple[dict[int, CharMetrics],
                                               dict[str, CharMetrics]]:
    """
    Parse the given filehandle for character metrics information.

    It is assumed that the file cursor is on the line behind 'StartCharMetrics'.

    Returns
    -------
    ascii_d : dict
         A mapping "ASCII num of the character" to `.CharMetrics`.
    name_d : dict
         A mapping "character name" to `.CharMetrics`.

    Notes
    -----
    This function is incomplete per the standard, but thus far parses
    all the sample afm files tried.
    """
    required_keys = {'C', 'WX', 'N', 'B'}