8000 Update type hints for font manager and extension · matplotlib/matplotlib@115fab3 · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 115fab3

Browse files
committed
Update type hints for font manager and extension
1 parent 00afcc0 commit 115fab3

File tree

6 files changed

+133
-89
lines changed

6 files changed

+133
-89
lines changed

ci/mypy-stubtest-allowlist.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ matplotlib.pylab.*
55
matplotlib._.*
66
matplotlib.rcsetup._listify_validator
77
matplotlib.rcsetup._validate_linestyle
8-
matplotlib.ft2font.*
98
matplotlib.testing.*
109
matplotlib.sphinxext.*
1110

lib/matplotlib/font_manager.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ def _fontentry_helper_repr_html(fontent):
315315
('name', str, dataclasses.field(default='')),
316316
('style', str, dataclasses.field(default='normal')),
317317
('variant', str, dataclasses.field(default='normal')),
318-
('weight', str, dataclasses.field(default='normal')),
318+
('weight', str | int, dataclasses.field(default='normal')),
319319
('stretch', str, dataclasses.field(default='normal')),
320320
('size', str, dataclasses.field(default='medium')),
321321
],
@@ -464,6 +464,8 @@ def afmFontProperty(fontpath, font):
464464
465465
Parameters
466466
----------
467+
fontpath : str
468+
The filename corresponding to *font*.
467469
font : AFM
468470
The AFM font file from which information will be extracted.
469471

lib/matplotlib/font_manager.pyi

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,19 @@ X11FontDirectories: list[str]
2020
OSXFontDirectories: list[str]
2121

2222
def get_fontext_synonyms(fontext: str) -> list[str]: ...
23-
def list_fonts(directory: str, extensions: Iterable[str]): ...
23+
def list_fonts(directory: str, extensions: Iterable[str]) -> list[str]: ...
2424
def win32FontDirectory() -> str: ...
2525
def _get_fontconfig_fonts() -> list[Path]: ...
2626
def findSystemFonts(
27-
fontpaths: Iterable[str] | None = ..., fontext: str = ...
27+
fontpaths: Iterable[str | os.PathLike | Path] | None = ..., fontext: str = ...
2828
) -> list[str]: ...
2929
@dataclass
3030
class FontEntry:
3131
fname: str = ...
3232
name: str = ...
3333
style: str = ...
3434
variant: str = ...
35-
weight: str = ...
35+
weight: str | int = ...
3636
stretch: str = ...
3737
size: str = ...
3838

@@ -42,41 +42,44 @@ def afmFontProperty(fontpath: str, font: AFM) -> FontEntry: ...
4242
class FontProperties:
4343
def __init__(
4444
self,
45-
family: str | None = ...,
45+
family: str | Iterable[str] | None = ...,
4646
style: Literal["normal", "italic", "oblique"] | None = ...,
4747
variant: Literal["normal", "small-caps"] | None = ...,
4848
weight: int | str | None = ...,
4949
stretch: int | str | None = ...,
5050
size: float | str | None = ...,
51-
fname: str | None = ...,
51+
fname: str | os.PathLike | Path | None = ...,
5252
math_fontfamily: str | None = ...,
5353
) -> None: ...
5454
def __hash__(self) -> int: ...
5555
def __eq__(self, other: object) -> bool: ...
56-
def get_family(self) -> str: ...
56+
def get_family(self) -> list[str]: ...
5757
def get_name(self) -> str: ...
5858
def get_style(self) -> Literal["normal", "italic", "oblique"]: ...
5959
def get_variant(self) -> Literal["normal", "small-caps"]: ...
6060
def get_weight(self) -> int | str: ...
6161
def get_stretch(self) -> int | str: ...
6262
def get_size(self) -> float: ...
63-
def get_file(self) -> str: ...
63+
def get_file(self) -> str | bytes | None: ...
6464
def get_fontconfig_pattern(self) -> dict[str, list[Any]]: ...
65-
def set_family(self, family: str | Iterable[str]) -> None: ...
66-
def set_style(self, style: Literal["normal", "italic", "oblique"]) -> None: ...
67-
def set_variant(self, variant: Literal["normal", "small-caps"]) -> None: ...
68-
def set_weight(self, weight: int | str) -> None: ...
69-
def set_stretch(self, stretch: int | str) -> None: ...
70-
def set_size(self, size: float | str) -> None: ...
65+
def set_family(self, family: str | Iterable[str] | None) -> None: ...
66+
def set_style(
67+
self, style: Literal["normal", "italic", "oblique"] | None
68+
) -> None: ...
69+
def set_variant(self, variant: Literal["normal", "small-caps"] | None) -> None: ...
70+
def set_weight(self, weight: int | str | None) -> None: ...
71+
def set_stretch(self, stretch: int | str | None) -> None: ...
72+
def set_size(self, size: float | str | None) -> None: ...
7173
def set_file(self, file: str | os.PathLike | Path | None) -> None: ...
7274
def set_fontconfig_pattern(self, pattern: str) -> None: ...
7375
def get_math_fontfamily(self) -> str: ...
7476
def set_math_fontfamily(self, fontfamily: str | None) -> None: ...
7577
def copy(self) -> FontProperties: ...
76-
def set_name(self, family: str) -> None: ...
77-
def get_slant(self) -> Literal["normal", "italic", "oblique"]: ...
78-
def set_slant(self, style: Literal["normal", "italic", "oblique"]) -> None: ...
79-
def get_size_in_points(self) -> float: ...
78+
# Aliases
79+
set_name = set_family
80+
get_slant = get_style
81+
set_slant = set_style
82+
get_size_in_points = get_size
8083

8184
def json_dump(data: FontManager, filename: str | Path | os.PathLike) -> None: ...
8285
def json_load(filename: str | Path | os.PathLike) -> FontManager: ...

lib/matplotlib/ft2font.pyi

Lines changed: 86 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,11 @@
11
# This is generated from a compiled module, and as such is very generic
22
# This could be more specific. Docstrings for this module are light
33

4-
from typing import Any
4+
from typing import BinaryIO
5+
from typing_extensions import Buffer # < Py3.12
6+
7+
import numpy as np
8+
from numpy.typing import NDArray
59

610
BOLD: int
711
EXTERNAL_STREAM: in F438 t
@@ -41,54 +45,85 @@ SFNT: int
4145
VERTICAL: int
4246

4347
class FT2Font:
44-
ascender: Any
45-
bbox: Any
46-
descender: Any
47-
face_flags: Any
48-
family_name: Any
49-
fname: Any
50-
height: Any
51-
max_advance_height: Any
52-
max_advance_width: Any
53-
num_charmaps: Any
54-
num_faces: Any
55-
num_fixed_sizes: Any
56-
num_glyphs: Any
57-
postscript_name: Any
58-
scalable: Any
59-
style_flags: Any
60-
style_name: Any
61-
underline_position: Any
62-
underline_thickness: Any
63-
units_per_EM: Any
64-
def __init__(self, *args, **kwargs) -> None: ...
65-
def _get_fontmap(self, *args, **kwargs) -> Any: ...
66-
def clear(self, *args, **kwargs) -> Any: ...
67-
def draw_glyph_to_bitmap(self, *args, **kwargs) -> Any: ...
68-
def draw_glyphs_to_bitmap(self, *args, **kwargs) -> Any: ...
69-
def get_bitmap_offset(self, *args, **kwargs) -> Any: ...
70-
def get_char_index(self, *args, **kwargs) -> Any: ...
71-
def get_charmap(self, *args, **kwargs) -> Any: ...
72-
def get_descent(self, *args, **kwargs) -> Any: ...
73-
def get_glyph_name(self, *args, **kwargs) -> Any: ...
74-
def get_image(self, *args, **kwargs) -> Any: ...
75-
def get_kerning(self, *args, **kwargs) -> Any: ...
76-
def get_name_index(self, *args, **kwargs) -> Any: ...
77-
def get_num_glyphs(self, *args, **kwargs) -> Any: ...
78-
def get_path(self, *args, **kwargs) -> Any: ...
79-
def get_ps_font_info(self, *args, **kwargs) -> 10000 Any: ...
80-
def get_sfnt(self, *args, **kwargs) -> Any: ...
81-
def get_sfnt_table(self, *args, **kwargs) -> Any: ...
82-
def get_width_height(self, *args, **kwargs) -> Any: ...
83-
def get_xys(self, *args, **kwargs) -> Any: ...
84-
def load_char(self, *args, **kwargs) -> Any: ...
85-
def load_glyph(self, *args, **kwargs) -> Any: ...
86-
def select_charmap(self, *args, **kwargs) -> Any: ...
87-
def set_charmap(self, *args, **kwargs) -> Any: ...
88-
def set_size(self, *args, **kwargs) -> Any: ...
89-
def set_text(self, *args, **kwargs) -> Any: ...
48+
ascender: int
49+
bbox: tuple[int, int, int, int]
50+
descender: int
51+
face_flags: int
52+
family_name: str
53+
fname: str
54+
height: int
55+
max_advance_height: int
56+
max_advance_width: int
57+
num_charmaps: int
58+
num_faces: int
59+
num_fixed_sizes: int
60+
num_glyphs: int
61+
postscript_name: str
62+
scalable: bool
63+
style_flags: int
64+
style_name: str
65+
underline_position: int
66+
underline_thickness: int
67+
units_per_EM: int
68+
69+
def __init__(
70+
self,
71+
filename: str | BinaryIO,
72+
hinting_factor: int = ...,
73+
*,
74+
_fallback_list: list[FT2Font] | None,
75+
_kerning_factor: int = ...
76+
) -> None: ...
77+
def _get_fontmap(self, string: str) -> dict[str, FT2Font]: ...
78+
def clear(self) -> None: ...
79+
def draw_glyph_to_bitmap(
80+
self, image: FT2Image, x: float, y: float, glyph: Glyph, antialiased: bool = ...
81+
) -> None: ...
82+
def draw_glyphs_to_bitmap(self, antialiased: bool = ...) -> None: ...
83+
def get_bitmap_offset(self) -> tuple[int, int]: ...
84+
def get_char_index(self, codepoint: int) -> int: ...
85+
def get_charmap(self) -> dict[int, int]: ...
86+
def get_descent(self) -> int: ...
87+
def get_glyph_name(self, index: int) -> str: ...
88+
def get_image(self) -> NDArray[np.uint8]: ...
89+
def get_kerning(self, left: int, right: int, mode: int) -> int: ...
90+
def get_name_index(self, name: str) -> int: ...
91+
def get_num_glyphs(self) -> int: ...
92+
def get_path(self) -> tuple[NDArray[np.float64], NDArray[np.int8]]: ...
93+
def get_ps_font_info(
94+
self,
95+
) -> tuple[str, str, str, str, str, int, int, int, int]: ...
96+
def get_sfnt(self) -> dict[tuple[int, int, int, int], bytes]: ...
97+
def get_sfnt_table(
98+
self, name: str
99+
) -> dict[
100+
str, tuple[int, int, int, int] | tuple[int, int] | int | bytes
101+
] | None: ...
102+
def get_width_height(self) -> tuple[int, int]: ...
103+
def get_xys(self, antialiased: bool = ...) -> NDArray[np.float64]: ...
104+
def load_char(self, charcode: int, flags: int = ...) -> Glyph: ...
105+
def load_glyph(self, glyphindex: int, flags: int = ...) -> Glyph: ...
106+
def select_charmap(self, i: int) -> None: ...
107+
def set_charmap(self, i: int) -> None: ...
108+
def set_size(self, ptsize: float, dpi: float) -> None: ...
109+
def set_text(
110+
self, string: str, angle: float = ..., flags: int = ...
111+
) -> NDArray[np.float64]: ...
112+
113+
class FT2Image(Buffer):
114+
def __init__(self, width: float, height: float) -> None: ...
115+
def draw_rect(self, x0: float, y0: float, x1: float, y1: float) -> None: ...
116+
def draw_rect_filled(self, x0: float, y0: float, x1: float, y1: float) -> None: ...
117+
118+
class Glyph:
119+
width: int
120+
height: int
121+
horiBearingX: int
122+
horiBearingY: int
123+
horiAdvance: int
124+
linearHoriAdvance: int
125+
vertBearingX: int
126+
vertBearingY: int
127+
vertAdvance: int
90128

91-
class FT2Image:
92-
def __init__(self, *args, **kwargs) -> None: ...
93-
def draw_rect(self, *args, **kwargs) -> Any: ...
94-
def draw_rect_filled(self, *args, **kwargs) -> Any: ...
129+
def bbox(self) -> tuple[int, int, int, int]: ...

requirements/testing/mypy.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Extra pip requirements for the GitHub Actions mypy build
22

33
mypy==1.1.1
4-
typing-extensions
4+
typing-extensions>=4.6,<5
55

66
# Extra stubs distributed separately from the main pypi package
77
pandas-stubs

src/ft2font_wrapper.cpp

Lines changed: 23 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -331,46 +331,51 @@ const char *PyFT2Font_init__doc__ =
331331
"Parameters\n"
332332
"----------\n"
333333
"filename : str or file-like\n"
334-
" The source of the font data in a format (ttf or ttc) that FreeType can read\n\n"
334+
" The source of the font data in a format (ttf or ttc) that FreeType can read\n"
335+
"\n"
335336
"hinting_factor : int, optional\n"
336337
" Must be positive. Used to scale the hinting in the x-direction\n"
337338
"_fallback_list : list of FT2Font, optional\n"
338-
" A list of FT2Font objects used to find missing glyphs.\n\n"
339+
" A list of FT2Font objects used to find missing glyphs.\n"
340+
"\n"
339341
" .. warning::\n"
340-
" This API is both private and provisional: do not use it directly\n\n"
342+
" This API is both private and provisional: do not use it directly\n"
343+
"\n"
341344
"_kerning_factor : int, optional\n"
342-
" Used to adjust the degree of kerning.\n\n"
345+
" Used to adjust the degree of kerning.\n"
346+
"\n"
343347
" .. warning::\n"
344-
" This API is private: do not use it directly\n\n"
348+
" This API is private: do not use it directly\n"
349+
"\n"
345350
"Attributes\n"
346351
"----------\n"
347-
"num_faces\n"
352+
"num_faces : int\n"
348353
" Number of faces in file.\n"
349354
"face_flags, style_flags : int\n"
350355
" Face and style flags; see the ft2font constants.\n"
351-
"num_glyphs\n"
356+
"num_glyphs : int\n"
352357
" Number of glyphs in the face.\n"
353-
"family_name, style_name\n"
358+
"family_name, style_name : str\n"
354359
" Face family and style name.\n"
355-
"num_fixed_sizes\n"
360+
"num_fixed_sizes : int\n"
356361
" Number of bitmap in the face.\n"
357-
"scalable\n"
362+
"scalable : bool\n"
358363
" Whether face is scalable; attributes after this one are only\n"
359364
" defined for scalable faces.\n"
360-
"bbox\n"
365+
"bbox : tuple[int, int, int, int]\n"
361366
" Face global bounding box (xmin, ymin, xmax, ymax).\n"
362-
"units_per_EM\n"
367+
"units_per_EM : int\n"
363368
" Number of font units covered by the EM.\n"
364-
"ascender, descender\n"
369+
"ascender, descender : int\n"
365370
" Ascender and descender in 26.6 units.\n"
366-
"height\n"
371+
"height : int\n"
367372
" Height in 26.6 units; used to compute a default line spacing\n"
368373
" (baseline-to-baseline distance).\n"
369-
"max_advance_width, max_advance_height\n"
374+
"max_advance_width, max_advance_height : int\n"
370375
" Maximum horizontal and vertical cursor advance for all glyphs.\n"
371-
"underline_position, underline_thickness\n"
376+
"underline_position, underline_thickness : int\n"
372377
" Vertical position and thickness of the underline bar.\n"
373-
"postscript_name\n"
378+
"postscript_name : str\n"
374379
" PostScript name of the font.\n";
375380

376381
static int PyFT2Font_init(PyFT2Font *self, PyObject *args, PyObject *kwds)
@@ -627,7 +632,7 @@ static PyObject *PyFT2Font_get_fontmap(PyFT2Font *self, PyObject *args, PyObject
627632

628633

629634
const char *PyFT2Font_set_text__doc__ =
630-
"set_text(self, string, angle, flags=32)\n"
635+
"set_text(self, string, angle=0.0, flags=32)\n"
631636
"--\n\n"
632637
"Set the text *string* and *angle*.\n"
633638
"*flags* can be a bitwise-or of the LOAD_XXX constants;\n"

0 commit comments

Comments
 (0)
0