From f47d5614215eb4f801232e04b319d594c7993463 Mon Sep 17 00:00:00 2001 From: Oscar Gustafsson Date: Thu, 14 Dec 2023 10:40:26 +0100 Subject: [PATCH 1/3] Improve check for bbox --- lib/matplotlib/transforms.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/matplotlib/transforms.py b/lib/matplotlib/transforms.py index 5a7fd125a29b..c0cb0f12159b 100644 --- a/lib/matplotlib/transforms.py +++ b/lib/matplotlib/transforms.py @@ -1101,7 +1101,7 @@ def __init__(self, bbox, transform, **kwargs): bbox : `Bbox` transform : `Transform` """ - if not bbox.is_bbox: + if not getattr(bbox, 'is_bbox', False): raise ValueError("'bbox' is not a bbox") _api.check_isinstance(Transform, transform=transform) if transform.input_dims != 2 or transform.output_dims != 2: @@ -1190,7 +1190,7 @@ def __init__(self, bbox, x0=None, y0=None, x1=None, y1=None, **kwargs): The locked value for y1, or None to leave unlocked. """ - if not bbox.is_bbox: + if not getattr(bbox, 'is_bbox', False): raise ValueError("'bbox' is not a bbox") super().__init__(**kwargs) @@ -2547,7 +2547,8 @@ def __init__(self, boxin, boxout, **kwargs): Create a new `BboxTransform` that linearly transforms points from *boxin* to *boxout*. """ - if not boxin.is_bbox or not boxout.is_bbox: + if (not getattr(boxin, 'is_bbox', False) or + not getattr(boxout, 'is_bbox', False)): raise ValueError("'boxin' and 'boxout' must be bbox") super().__init__(**kwargs) @@ -2591,7 +2592,7 @@ def __init__(self, boxout, **kwargs): Create a new `BboxTransformTo` that linearly transforms points from the unit bounding box to *boxout*. """ - if not boxout.is_bbox: + if not getattr(boxout, 'is_bbox', False): raise ValueError("'boxout' must be bbox") super().__init__(**kwargs) @@ -2645,7 +2646,7 @@ class BboxTransformFrom(Affine2DBase): is_separable = True def __init__(self, boxin, **kwargs): - if not boxin.is_bbox: + if not getattr(boxin, 'is_bbox', False): raise ValueError("'boxin' must be bbox") super().__init__(**kwargs) From 19aa67c6a1a9cc33fa7f57f8bb7ffcee4724604f Mon Sep 17 00:00:00 2001 From: Oscar Gustafsson Date: Fri, 15 Dec 2023 07:13:15 +0100 Subject: [PATCH 2/3] Deprecate is_bbox --- .../next_api_changes/behavior/27514-OG.rst | 5 +++++ .../deprecations/27514-OG.rst | 4 ++++ lib/matplotlib/transforms.py | 22 +++++++------------ 3 files changed, 17 insertions(+), 14 deletions(-) create mode 100644 doc/api/next_api_changes/behavior/27514-OG.rst create mode 100644 doc/api/next_api_changes/deprecations/27514-OG.rst diff --git a/doc/api/next_api_changes/behavior/27514-OG.rst b/doc/api/next_api_changes/behavior/27514-OG.rst new file mode 100644 index 000000000000..8b2a7ab9ef2e --- /dev/null +++ b/doc/api/next_api_changes/behavior/27514-OG.rst @@ -0,0 +1,5 @@ +Exception when not passing a Bbox to BboxTransform*-classes +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + +The exception when not passing a Bbox to BboxTransform*-classes that expect one, e.g., +`~matplotlib.transforms.BboxTransform` has changed from ``ValueError`` to ``TypeError``. diff --git a/doc/api/next_api_changes/deprecations/27514-OG.rst b/doc/api/next_api_changes/deprecations/27514-OG.rst new file mode 100644 index 000000000000..f318ec8aa4bb --- /dev/null +++ b/doc/api/next_api_changes/deprecations/27514-OG.rst @@ -0,0 +1,4 @@ +``TransformNode.is_bbox`` +~~~~~~~~~~~~~~~~~~~~~~~~~ + +... is deprecated. Instead check the object using ``isinstance(..., BboxBase)``. diff --git a/lib/matplotlib/transforms.py b/lib/matplotlib/transforms.py index c0cb0f12159b..1ee1b41dac76 100644 --- a/lib/matplotlib/transforms.py +++ b/lib/matplotlib/transforms.py @@ -102,7 +102,7 @@ class TransformNode: # Some metadata about the transform, used to determine whether an # invalidation is affine-only is_affine = False - is_bbox = False + is_bbox = _api.deprecated("3.9")(_api.classproperty(lambda cls: False)) pass_through = False """ @@ -220,7 +220,7 @@ class BboxBase(TransformNode): and height, but these are not stored explicitly. """ - is_bbox = True + is_bbox = _api.deprecated("3.9")(_api.classproperty(lambda cls: True)) is_affine = True if DEBUG: @@ -1101,8 +1101,7 @@ def __init__(self, bbox, transform, **kwargs): bbox : `Bbox` transform : `Transform` """ - if not getattr(bbox, 'is_bbox', False): - raise ValueError("'bbox' is not a bbox") + _api.check_isinstance(BboxBase, bbox=bbox) _api.check_isinstance(Transform, transform=transform) if transform.input_dims != 2 or transform.output_dims != 2: raise ValueError( @@ -1190,9 +1189,7 @@ def __init__(self, bbox, x0=None, y0=None, x1=None, y1=None, **kwargs): The locked value for y1, or None to leave unlocked. """ - if not getattr(bbox, 'is_bbox', False): - raise ValueError("'bbox' is not a bbox") - + _api.check_isinstance(BboxBase, bbox=bbox) super().__init__(**kwargs) self._bbox = bbox self.set_children(bbox) @@ -2547,9 +2544,8 @@ def __init__(self, boxin, boxout, **kwargs): Create a new `BboxTransform` that linearly transforms points from *boxin* to *boxout*. """ - if (not getattr(boxin, 'is_bbox', False) or - not getattr(boxout, 'is_bbox', False)): - raise ValueError("'boxin' and 'boxout' must be bbox") + _api.check_isinstance(BboxBase, boxin=boxin) + _api.check_isinstance(BboxBase, boxout=boxout) super().__init__(**kwargs) self._boxin = boxin @@ -2592,8 +2588,7 @@ def __init__(self, boxout, **kwargs): Create a new `BboxTransformTo` that linearly transforms points from the unit bounding box to *boxout*. """ - if not getattr(boxout, 'is_bbox', False): - raise ValueError("'boxout' must be bbox") + _api.check_isinstance(BboxBase, boxout=boxout) super().__init__(**kwargs) self._boxout = boxout @@ -2646,8 +2641,7 @@ class BboxTransformFrom(Affine2DBase): is_separable = True def __init__(self, boxin, **kwargs): - if not getattr(boxin, 'is_bbox', False): - raise ValueError("'boxin' must be bbox") + _api.check_isinstance(BboxBase, boxin=boxin) super().__init__(**kwargs) self._boxin = boxin From 24a74680c294d35d36abea319cb5b5968991acb6 Mon Sep 17 00:00:00 2001 From: Oscar Gustafsson Date: Fri, 15 Dec 2023 09:06:35 +0100 Subject: [PATCH 3/3] Update lib/matplotlib/transforms.py Co-authored-by: Elliott Sales de Andrade --- lib/matplotlib/transforms.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/matplotlib/transforms.py b/lib/matplotlib/transforms.py index 1ee1b41dac76..1a68ff12025f 100644 --- a/lib/matplotlib/transforms.py +++ b/lib/matplotlib/transforms.py @@ -2544,8 +2544,7 @@ def __init__(self, boxin, boxout, **kwargs): Create a new `BboxTransform` that linearly transforms points from *boxin* to *boxout*. """ - _api.check_isinstance(BboxBase, boxin=boxin) - _api.check_isinstance(BboxBase, boxout=boxout) + _api.check_isinstance(BboxBase, boxin=boxin, boxout=boxout) super().__init__(**kwargs) self._boxin = boxin