From 0555206d4793a6210ce0ef83ef98ce055a65f234 Mon Sep 17 00:00:00 2001 From: N R Navaneet Date: Sun, 13 Jul 2025 04:57:06 +0530 Subject: [PATCH] Add Literal-based LogLevel type alias and use it in set_loglevel for stricter typing. --- lib/matplotlib/__init__.pyi | 4 +++- lib/matplotlib/pyplot.py | 3 ++- lib/matplotlib/typing.py | 3 +++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/matplotlib/__init__.pyi b/lib/matplotlib/__init__.pyi index 07019109f406..b20ee184149d 100644 --- a/lib/matplotlib/__init__.pyi +++ b/lib/matplotlib/__init__.pyi @@ -40,6 +40,8 @@ from packaging.version import Version from matplotlib._api import MatplotlibDeprecationWarning from typing import Any, Literal, NamedTuple, overload +from matplotlib.typing import LogLevel + class _VersionInfo(NamedTuple): major: int @@ -52,7 +54,7 @@ __bibtex__: str __version__: str __version_info__: _VersionInfo -def set_loglevel(level: str) -> None: ... +def set_loglevel(level: LogLevel) -> None: ... class _ExecInfo(NamedTuple): executable: str diff --git a/lib/matplotlib/pyplot.py b/lib/matplotlib/pyplot.py index 1f9c4606af27..e916d57f8871 100644 --- a/lib/matplotlib/pyplot.py +++ b/lib/matplotlib/pyplot.py @@ -144,6 +144,7 @@ MouseEventType, PickEventType, ResizeEventType, + LogLevel ) from matplotlib.widgets import SubplotTool @@ -351,7 +352,7 @@ def uninstall_repl_displayhook() -> None: # Ensure this appears in the pyplot docs. @_copy_docstring_and_deprecators(matplotlib.set_loglevel) -def set_loglevel(level: str) -> None: +def set_loglevel(level: LogLevel) -> None: return matplotlib.set_loglevel(level) diff --git a/lib/matplotlib/typing.py b/lib/matplotlib/typing.py index e3719235cdb8..879e37a095d0 100644 --- a/lib/matplotlib/typing.py +++ b/lib/matplotlib/typing.py @@ -83,6 +83,9 @@ CapStyleType: TypeAlias = CapStyle | Literal["butt", "projecting", "round"] """Line cap styles. See :doc:`/gallery/lines_bars_and_markers/capstyle`.""" +LogLevel: TypeAlias = Literal["notset", "debug", "info", "warning", "error", "critical"] +"""Literal type for valid logging levels accepted by `set_loglevel()`.""" + CoordsBaseType = Union[ str, Artist,