Skip to content

Commit 0e430b9

Browse files
ZPyrolinkCorenthinZQuLogic
authored
Add explicit signatures for pyplot.{polar,savefig,set_loglevel} (#30200)
* Create tests (cherry picked from commit 608b51f) * Update test_pyplot.py to include type on signature (cherry picked from commit 4ea0ff8) * Update polar and set_loglevel signature on pyplot.py (cherry picked from commit 41b701b) * Update savefig signature on pyplot.py (cherry picked from commit b863ba2) * Add type hint on polar and set_loglevel on pyplot.py. Correct polar content (cherry picked from commit 92dc045) * Format with ruff (cherry picked from commit 64e7921) * Revert polar on pyplot.py and remove corresponding test * Remove extra work when stub file doesn't exists Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com> * Replace assert_signatures_identical (check return_annotation and full parameters) with assert_same_signature (only check len(parameters), names and kinds) * Remove unused import * Renaming assert_signature arguments * Correct typo and ruff error --------- Co-authored-by: Corenthin ZOZOR <corenthin.zozor@gmail.com> Co-authored-by: Elliott Sales de Andrade <quantum.analyst@gmail.com>
1 parent 9c184c1 commit 0e430b9

File tree

2 files changed

+29
-5
lines changed

2 files changed

+29
-5
lines changed

lib/matplotlib/pyplot.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050
import sys
5151
import threading
5252
import time
53-
from typing import TYPE_CHECKING, cast, overload
53+
from typing import IO, TYPE_CHECKING, cast, overload
5454

5555
from cycler import cycler # noqa: F401
5656
import matplotlib
@@ -338,8 +338,8 @@ def uninstall_repl_displayhook() -> None:
338338

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

344344

345345
@_copy_docstring_and_deprecators(Artist.findobj)
@@ -1259,11 +1259,11 @@ def draw() -> None:
12591259

12601260

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

lib/matplotlib/tests/test_pyplot.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import difflib
2+
import inspect
23

34
import numpy as np
45
import sys
@@ -484,3 +485,26 @@ def test_matshow():
484485

485486
# Smoke test that matshow does not ask for a new figsize on the existing figure
486487
plt.matshow(arr, fignum=fig.number)
488+
489+
490+
def assert_same_signature(func1, func2):
491+
"""
492+
Assert that `func1` and `func2` have the same arguments,
493+
i.e. same parameter count, names and kinds.
494+
495+
:param func1: First function to check
496+
:param func2: Second function to check
497+
"""
498+
params1 = inspect.signature(func1).parameters
499+
params2 = inspect.signature(func2).parameters
500+
501+
assert len(params1) == len(params2)
502+
assert all([
503+
params1[p].name == params2[p].name and
504+
params1[p].kind == params2[p].kind
505+
for p in params1
506+
])
507+
508+
509+
def test_setloglevel_signature():
510+
assert_same_signature(plt.set_loglevel, mpl.set_loglevel)

0 commit comments

Comments
 (0)