Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 03031ee

Browse files
committed
Small TransformWrapper cleanups.
Move _init() and _set() into set(). _init was split out in 5478183 to improve the pickling of TransformWrappers but was made unnecessary in 08387d8 following improvements in pickling in recent Pythons. _set and set can also easily be combined. Make input_dims, output_dims straight properties similarly to is_affine & friends; and improve the error message for mismatched dimensions (to indicate the wrong values).
1 parent be03643 commit 03031ee

File tree

2 files changed

+29
-27
lines changed

2 files changed

+29
-27
lines changed

lib/matplotlib/tests/test_transforms.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -722,3 +722,11 @@ def test_deepcopy():
722722
b1.translate(3, 4)
723723
assert not s._invalid
724724
assert (s.get_matrix() == a.get_matrix()).all()
725+
726+
727+
def test_transformwrapper():
728+
t = mtransforms.TransformWrapper(mtransforms.Affine2D())
729+
with pytest.raises(ValueError, match=(
730+
r"The input and output dims of the new child \(1, 1\) "
731+
r"do not match those of current child \(2, 2\)")):
732+
t.set(scale.LogTransform(10))

lib/matplotlib/transforms.py

Lines changed: 21 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1699,15 +1699,8 @@ def __init__(self, child):
16991699
be replaced with :meth:`set`.
17001700
"""
17011701
_api.check_isinstance(Transform, child=child)
1702-
self._init(child)
1703-
self.set_children(child)
1704-
1705-
def _init(self, child):
1706-
Transform.__init__(self)
1707-
self.input_dims = child.input_dims
1708-
self.output_dims = child.output_dims
1709-
self._set(child)
1710-
self._invalid = 0
1702+
super().__init__()
1703+
self.set(child)
17111704

17121705
def __eq__(self, other):
17131706
return self._child.__eq__(other)
@@ -1718,8 +1711,24 @@ def frozen(self):
17181711
# docstring inherited
17191712
return self._child.frozen()
17201713

1721-
def _set(self, child):
1714+
def set(self, child):
1715+
"""
1716+
Replace the current child of this transform with another one.
1717+
1718+
The new child must have the same number of input and output
1719+
dimensions as the current child.
1720+
"""
1721+
if hasattr(self, "_child"): # Absent during init.
1722+
new_dims = (child.input_dims, child.output_dims)
1723+
old_dims = (self._child.input_dims, self._child.output_dims)
1724+
if new_dims != old_dims:
1725+
raise ValueError(
1726+
f"The input and output dims of the new child {new_dims} "
1727+
f"do not match those of current child {old_dims}")
1728+
self._child._parents.pop(id(self), None)
1729+
17221730
self._child = child
1731+
self.set_children(child)
17231732

17241733
self.transform = child.transform
17251734
self.transform_affine = child.transform_affine
@@ -1730,31 +1739,16 @@ def _set(self, child):
17301739
self.get_affine = child.get_affine
17311740
self.inverted = child.inverted
17321741
self.get_matrix = child.get_matrix
1733-
17341742
# note we do not wrap other properties here since the transform's
17351743
# child can be changed with WrappedTransform.set and so checking
17361744
# is_affine and other such properties may be dangerous.
17371745

1738-
def set(self, child):
1739-
"""
1740-
Replace the current child of this transform with another one.
1741-
1742-
The new child must have the same number of input and output
1743-
dimensions as the current child.
1744-
"""
1745-
if (child.input_dims != self.input_dims or
1746-
child.output_dims != self.output_dims):
1747-
raise ValueError(
1748-
"The new child must have the same number of input and output "
1749-
"dimensions as the current child")
1750-
1751-
self.set_children(child)
1752-
self._set(child)
1753-
17541746
self._invalid = 0
17551747
self.invalidate()
17561748
self._invalid = 0
17571749

1750+
input_dims = property(lambda self: self._child.input_dims)
1751+
output_dims = property(lambda self: self._child.output_dims)
17581752
is_affine = property(lambda self: self._child.is_affine)
17591753
is_separable = property(lambda self: self._child.is_separable)
17601754
has_inverse = property(lambda self: self._child.has_inverse)

0 commit comments

Comments
 (0)