Skip to content

Commit 97d07b8

Browse files
committed
FIX: MouseButton representation in boilerplate generated signatures
In Python 3.10 the repr and str representation of Enums changed from str: 'ClassName.NAME' -> 'NAME' repr: '<ClassName.NAME: value>' -> 'ClassName.NAME' which is more consistent with what str/repr should do, however this breaks boilerplate which needs to get the ClassName.NAME version in all versions of Python. Thus, we locally monkey patch our preferred str representation in here. bpo-40066 python/cpython#22392
1 parent a4a84e9 commit 97d07b8

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

tools/boilerplate.py

+22
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,28 @@
2525
from matplotlib import _api, mlab
2626
from matplotlib.axes import Axes
2727
from matplotlib.figure import Figure
28+
from matplotlib.backend_bases import MouseButton
29+
30+
31+
# we need to define a custom str because py310 change
32+
# In Python 3.10 the repr and str representation of Enums changed from
33+
#
34+
# str: 'ClassName.NAME' -> 'NAME'
35+
# repr: '<ClassName.NAME: value>' -> 'ClassName.NAME'
36+
#
37+
# which is more consistent with what str/repr should do, however this breaks
38+
# boilerplate which needs to get the ClassName.NAME version in all versions of
39+
# Python. Thus, we locally monkey patch our preferred str representation in
40+
# here.
41+
#
42+
# bpo-40066
43+
# https://github.com/python/cpython/pull/22392/
44+
def enum_str_back_compat_patch(self):
45+
return f'{type(self).__name__}.{self.name}'
46+
47+
# only monkey patch if we have to.
48+
if str(MouseButton.LEFT) != 'MouseButton.Left':
49+
MouseButton.__str__ = enum_str_back_compat_patch
2850

2951

3052
# This is the magic line that must exist in pyplot, after which the boilerplate

0 commit comments

Comments
 (0)