8000 MNT: make signature of GridSpec.update explicit by timhoffm · Pull Request #29956 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

MNT: make signature of GridSpec.update explicit #29956

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 1 commit into from
Apr 22, 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
9 changes: 9 additions & 0 deletions lib/matplotlib/_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,15 @@
MatplotlibDeprecationWarning)


# A sentinel value for optional arguments, when None cannot be used as
# default because we need to distinguish between None passed explicitly
# and parameter not given. Usage: def foo(arg=_api.UNSET):
class _Unset:
def __repr__(self):
return "<UNSET>"
UNSET = _Unset()


class classproperty:
"""
Like `property`, but also triggers on access via the class, and it is the
Expand Down
2 changes: 2 additions & 0 deletions lib/matplotlib/_api/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ from .deprecation import ( # noqa: F401, re-exported API

_T = TypeVar("_T")

class _Unset: ...

class classproperty(Any):
def __init__(
self,
Expand Down
8 changes: 1 addition & 7 deletions lib/matplotlib/artist.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,12 +107,6 @@ def _stale_axes_callback(self, val):
_XYPair = namedtuple("_XYPair", "x y")


class _Unset:
def __repr__(self):
return "<UNSET>"
_UNSET = _Unset()


class Artist:
"""
Abstract base class for objects that render into a FigureCanvas.
Expand Down Expand Up @@ -166,7 +160,7 @@ def _update_set_signature_and_docstring(cls):
"""
cls.set.__signature__ = Signature(
[Parameter("self", Parameter.POSITIONAL_OR_KEYWORD),
*[Parameter(prop, Parameter.KEYWORD_ONLY, default=_UNSET)
*[Parameter(prop, Parameter.KEYWORD_ONLY, default=_api.UNSET)
for prop in ArtistInspector(cls).get_setters()
if prop not in Artist._PROPERTIES_EXCLUDED_FROM_SET]])
cls.set._autogenerated_signature = True
Expand Down
2 changes: 0 additions & 2 deletions lib/matplotlib/artist.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@ class _XYPair(NamedTuple):
x: ArrayLike
y: ArrayLike

class _Unset: ...

class Artist:
zorder: float
stale_callback: Callable[[Artist, bool], None] | None
Expand Down
24 changes: 17 additions & 7 deletions lib/matplotlib/gridspec.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

import matplotlib as mpl
from matplotlib import _api, _pylab_helpers, _tight_layout
from matplotlib._api import UNSET as _UNSET
Comment on lines 20 to +21
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have _api imported above, though I guess it makes the signature too long?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the idea is to make the signature more readable, but no strong preference.

from matplotlib.transforms import Bbox

_log = logging.getLogger(__name__)
Expand Down Expand Up @@ -366,7 +367,8 @@

_AllowedKeys = ["left", "bottom", "right", "top", "wspace", "hspace"]

def update(self, **kwargs):
def update(self, *, left=_UNSET, bottom=_UNSET, right=_UNSET, top=_UNSET,
wspace=_UNSET, hspace=_UNSET):
"""
Update the subplot parameters of the grid.

Expand All @@ -377,15 +379,23 @@
----------
left, right, top, bottom : float or None, optional
Extent of the subplots as a fraction of figure width or height.
wspace, hspace : float, optional
wspace, hspace : float or None, optional
Spacing between the subplots as a fraction of the average subplot
width / height.
"""
for k, v in kwargs.items():
if k in self._AllowedKeys:
setattr(self, k, v)
else:
raise AttributeError(f"{k} is an unknown keyword")
if left is not _UNSET:
self.left = left
if bottom is not _UNSET:
self.bottom = bottom
if right is not _UNSET:
self.right = right
if top is not _UNSET:
self.top = top
if wspace is not _UNSET:
self.wspace = wspace

Check warning on line 395 in lib/matplotlib/gridspec.py

View check run for this annotation

Codecov / codecov/patch

lib/matplotlib/gridspec.py#L395

Added line #L395 was not covered by tests
if hspace is not _UNSET:
self.hspace = hspace

for figmanager in _pylab_helpers.Gcf.figs.values():
for ax in figmanager.canvas.figure.axes:
if ax.get_subplotspec() is not None:
Expand Down
12 changes: 11 additions & 1 deletion lib/matplotlib/gridspec.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ from typing import Any, Literal, overload
from numpy.typing import ArrayLike
import numpy as np

from matplotlib._api import _Unset
from matplotlib.axes import Axes
from matplotlib.backend_bases import RendererBase
from matplotlib.figure import Figure
Expand Down Expand Up @@ -78,7 +79,16 @@ class GridSpec(GridSpecBase):
width_ratios: ArrayLike | None = ...,
height_ratios: ArrayLike | None = ...,
) -> None: ...
def update(self, **kwargs: float | None) -> None: ...
def update(
self,
*,
left: float | None | _Unset = ...,
bottom: float | None | _Unset = ...,
right: float | None | _Unset = ...,
top: float | None | _Unset = ...,
wspace: float | None | _Unset = ...,
hspace: float | None | _Unset = ...,
) -> None: ...
def locally_modified_subplot_params(self) -> list[str]: ...
def tight_layout(
self,
Expand Down
Loading
0