Skip to content

Commit 609ca97

Browse files
committed
TYP: Add overloads for MathTextParser return types
1 parent c45c771 commit 609ca97

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

lib/matplotlib/backends/backend_agg.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ def __init__(self, width, height, dpi):
7171
self._filter_renderers = []
7272

7373
self._update_methods()
74-
self.mathtext_parser = MathTextParser('Agg')
74+
self.mathtext_parser = MathTextParser('agg')
7575

7676
self.bbox = Bbox.from_bounds(0, 0, self.width, self.height)
7777

lib/matplotlib/mathtext.pyi

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
import os
2+
from typing import Generic, IO, Literal, TypeVar, overload
3+
24
from matplotlib.font_manager import FontProperties
5+
from matplotlib.typing import ColorType
36

47
# Re-exported API from _mathtext.
58
from ._mathtext import (
@@ -8,14 +11,16 @@ from ._mathtext import (
811
get_unicode_index as get_unicode_index,
912
)
1013

11-
from typing import IO, Literal
12-
from matplotlib.typing import ColorType
14+
_ParseType = TypeVar("_ParseType", RasterParse, VectorParse)
1315

14-
class MathTextParser:
15-
def __init__(self, output: Literal["path", "agg", "raster", "macosx"]) -> None: ...
16+
class MathTextParser(Generic[_ParseType]):
17+
@overload
18+
def __init__(self: MathTextParser[VectorParse], output: Literal["path"]) -> None: ...
19+
@overload
20+
def __init__(self: MathTextParser[RasterParse], output: Literal["agg", "raster", "macosx"]) -> None: ...
1621
def parse(
1722
self, s: str, dpi: float = ..., prop: FontProperties | None = ..., *, antialiased: bool | None = ...
18-
) -> RasterParse | VectorParse: ...
23+
) -> _ParseType: ...
1924

2025
def math_to_image(
2126
s: str,

lib/matplotlib/textpath.pyi

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
from matplotlib.font_manager import FontProperties
22
from matplotlib.ft2font import FT2Font
3-
from matplotlib.mathtext import MathTextParser
3+
from matplotlib.mathtext import MathTextParser, VectorParse
44
from matplotlib.path import Path
55

66
import numpy as np
@@ -10,7 +10,7 @@ from typing import Literal
1010
class TextToPath:
1111
FONT_SCALE: float
1212
DPI: float
13-
mathtext_parser: MathTextParser
13+
mathtext_parser: MathTextParser[VectorParse]
1414
def __init__(self) -> None: ...
1515
def get_text_width_height_descent(
1616
self, s: str, prop: FontProperties, ismath: bool | Literal["TeX"]

0 commit comments

Comments
 (0)