Skip to content

Commit cd3685f

Browse files
authored
Create RCKeyType (matplotlib#30316)
* Create RCKeyType * Create RCGroupKeyType * Rename types for consistent naming * Replace str with RcKeyType on RcParams * Replace str with RcKeyType or RcGroupKeyType on matplotlib * Accord pyplot and matplotlib type hints * Correct RcParams dict key type * Correct mypy errors
1 parent 73ef257 commit cd3685f

File tree

4 files changed

+402
-12
lines changed

4 files changed

+402
-12
lines changed

lib/matplotlib/__init__.pyi

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import contextlib
3939
from packaging.version import Version
4040

4141
from matplotlib._api import MatplotlibDeprecationWarning
42+
from matplotlib.typing import RcKeyType, RcGroupKeyType
4243
from typing import Any, Literal, NamedTuple, overload
4344
from matplotlib.typing import LogLevel
4445

@@ -69,18 +70,18 @@ def get_cachedir() -> str: ...
6970
def get_data_path() -> str: ...
7071
def matplotlib_fname() -> str: ...
7172

72-
class RcParams(dict[str, Any]):
73+
class RcParams(dict[RcKeyType, Any]):
7374
validate: dict[str, Callable]
7475
def __init__(self, *args, **kwargs) -> None: ...
75-
def _set(self, key: str, val: Any) -> None: ...
76-
def _get(self, key: str) -> Any: ...
76+
def _set(self, key: RcKeyType, val: Any) -> None: ...
77+
def _get(self, key: RcKeyType) -> Any: ...
7778

7879
def _update_raw(self, other_params: dict | RcParams) -> None: ...
7980

8081
def _ensure_has_backend(self) -> None: ...
81-
def __setitem__(self, key: str, val: Any) -> None: ...
82-
def __getitem__(self, key: str) -> Any: ...
83-
def __iter__(self) -> Generator[str, None, None]: ...
82+
def __setitem__(self, key: RcKeyType, val: Any) -> None: ...
83+
def __getitem__(self, key: RcKeyType) -> Any: ...
84+
def __iter__(self) -> Generator[RcKeyType, None, None]: ...
8485
def __len__(self) -> int: ...
8586
def find_all(self, pattern: str) -> RcParams: ...
8687
def copy(self) -> RcParams: ...
@@ -95,17 +96,17 @@ def rc_params_from_file(
9596
rcParamsDefault: RcParams
9697
rcParams: RcParams
9798
rcParamsOrig: RcParams
98-
defaultParams: dict[str, Any]
99+
defaultParams: dict[RcKeyType, Any]
99100

100-
def rc(group: str, **kwargs) -> None: ...
101+
def rc(group: RcGroupKeyType, **kwargs) -> None: ...
101102
def rcdefaults() -> None: ...
102103
def rc_file_defaults() -> None: ...
103104
def rc_file(
104105
fname: str | Path | os.PathLike, *, use_default_template: bool = ...
105106
) -> None: ...
106107
@contextlib.contextmanager
107108
def rc_context(
108-
rc: dict[str, Any] | None = ..., fname: str | Path | os.PathLike | None = ...
109+
rc: dict[RcKeyType, Any] | None = ..., fname: str | Path | os.PathLike | None = ...
109110
) -> Generator[None, None, None]: ...
110111
def use(backend: str, *, force: bool = ...) -> None: ...
111112
@overload

lib/matplotlib/_mathtext.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,7 +570,7 @@ def __init__(self, default_font_prop: FontProperties, load_glyph_flags: LoadFlag
570570

571571
super().__init__(default_font_prop, load_glyph_flags)
572572
for texfont in "cal rm tt it bf sf bfit".split():
573-
prop = mpl.rcParams['mathtext.' + texfont]
573+
prop = mpl.rcParams['mathtext.' + texfont] # type: ignore[index]
574574
font = findfont(prop)
575575
self.fontmap[texfont] = font
576576
prop = FontProperties('cmex10')

lib/matplotlib/pyplot.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@
146146
MarkerType,
147147
MouseEventType,
148148
PickEventType,
149+
RcGroupKeyType,
150+
RcKeyType,
149151
ResizeEventType,
150152
LogLevel
151153
)
@@ -790,13 +792,13 @@ def pause(interval: float) -> None:
790792

791793

792794
@_copy_docstring_and_deprecators(matplotlib.rc)
793-
def rc(group: str, **kwargs) -> None:
795+
def rc(group: RcGroupKeyType, **kwargs) -> None:
794796
matplotlib.rc(group, **kwargs)
795797

796798

797799
@_copy_docstring_and_deprecators(matplotlib.rc_context)
798800
def rc_context(
799-
rc: dict[str, Any] | None = None,
801+
rc: dict[RcKeyType, Any] | None = None,
800802
fname: str | pathlib.Path | os.PathLike | None = None,
801803
) -> AbstractContextManager[None]:
802804
return matplotlib.rc_context(rc, fname)

0 commit comments

Comments
 (0)