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

Skip to content

Commit f5ad81d

Browse files
committed
Fix % formatting and Transform equality.
Use `.format(key)` instead of `% key` formatting which fails when `key` is a tuple (one could also use `% (key,)` but we may as well use the more modern option). The `Transform` class doesn't need to override `__eq__` as user-defined classes default to using identity for equality. This also avoids having to add a `__hash__` to these classes to make them hashable (by default, user-defined classes are hashable, but become unhashable (in Py3) if they define a `__eq__` without defining a `__hash__`). A more complete PR should define `__hash__` for all `Transform` subclasses too.
1 parent c44556f commit f5ad81d

2 files changed

Lines changed: 7 additions & 8 deletions

File tree

lib/matplotlib/figure.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,18 @@ def add(self, key, a):
118118
# All the error checking may be unnecessary; but this method
119119
# is called so seldom that the overhead is negligible.
120120
if not isinstance(a, Axes):
121-
raise ValueError("second argument, %s, is not an Axes" % a)
121+
raise ValueError("second argument, {!r}, is not an Axes".format(a))
122122
try:
123123
hash(key)
124124
except TypeError:
125-
raise ValueError("first argument, %s, is not a valid key" % key)
125+
raise ValueError(
126+
"first argument, {!r}, is not a valid key".format(key))
126127

127128
a_existing = self.get(key)
128129
if a_existing is not None:
129130
Stack.remove(self, (key, a_existing))
130131
warnings.warn(
131-
"key %s already existed; Axes is being replaced" % key)
132+
"key {!r} already existed; Axes is being replaced".format(key))
132133
# I don't think the above should ever happen.
133134

134135
if a in self:

lib/matplotlib/transforms.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,11 +1156,9 @@ def __radd__(self, other):
11561156
raise TypeError(
11571157
"Can not add Transform to object of type '%s'" % type(other))
11581158

1159-
def __eq__(self, other):
1160-
# equality is based on transform object id. Hence:
1161-
# Transform() != Transform().
1162-
# Some classes, such as TransformWrapper & AffineBase, will override.
1163-
return self is other
1159+
# Equality is based on object identity for `Transform`s (so we don't
1160+
# override `__eq__`), but some subclasses, such as TransformWrapper &
1161+
# AffineBase, override this behavior.
11641162

11651163
def _iter_break_from_left_to_right(self):
11661164
"""

0 commit comments

Comments
 (0)