Skip to content

Add explicit signatures for pyplot.{polar,savefig,set_loglevel} #30200

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 13 commits into from
Jun 30, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions lib/matplotlib/pyplot.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
import sys
import threading
import time
from typing import TYPE_CHECKING, cast, overload
from typing import IO, TYPE_CHECKING, cast, overload

from cycler import cycler # noqa: F401
import matplotlib
Expand Down Expand Up @@ -338,8 +338,8 @@

# Ensure this appears in the pyplot docs.
@_copy_docstring_and_deprecators(matplotlib.set_loglevel)
def set_loglevel(*args, **kwargs) -> None:
return matplotlib.set_loglevel(*args, **kwargs)
def set_loglevel(level: str) -> None:
return matplotlib.set_loglevel(level)

Check warning on line 342 in lib/matplotlib/pyplot.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/pyplot.py#L342

Added line #L342 was not covered by tests


@_copy_docstring_and_deprecators(Artist.findobj)
Expand Down Expand Up @@ -1251,11 +1251,11 @@


@_copy_docstring_and_deprecators(Figure.savefig)
def savefig(*args, **kwargs) -> None:
def savefig(fname: str | os.PathLike | IO, **kwargs) -> None:
fig = gcf()
# savefig default implementation has no return, so mypy is unhappy
# presumably this is here because subclasses can return?
res = fig.savefig(*args, **kwargs) # type: ignore[func-returns-value]
res = fig.savefig(fname, **kwargs) # type: ignore[func-returns-value]
fig.canvas.draw_idle() # Need this if 'transparent=True', to reset colors.
return res

Expand Down
24 changes: 24 additions & 0 deletions lib/matplotlib/tests/test_pyplot.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import difflib
import inspect

import numpy as np
import sys
Expand Down Expand Up @@ -484,3 +485,26 @@ def test_matshow():

# Smoke test that matshow does not ask for a new figsize on the existing figure
plt.matshow(arr, fignum=fig.number)


def assert_same_signature(func1, func2):
"""
Assert that `func1` and `func2` have the same arguments,
i.e. same parameter count, names and kinds.

:param func1: First function to check
:param func2: Second function to check
"""
params1 = inspect.signature(func1).parameters
params2 = inspect.signature(func2).parameters

assert len(params1) == len(params2)
assert all([
params1[p].name == params2[p].name and
params1[p].kind == params2[p].kind
for p in params1
])


def test_setloglevel_signature():
assert_same_signature(plt.set_loglevel, mpl.set_loglevel)
Loading