Skip to content

Commit 93973ab

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 93973ab

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
@@ -24,9 +24,31 @@
2424
import numpy as np
2525
from matplotlib import _api, mlab
2626
from matplotlib.axes import Axes
27+
from matplotlib.backend_bases import MouseButton
2728
from matplotlib.figure import Figure
2829

2930

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
50+
51+
3052
# This is the magic line that must exist in pyplot, after which the boilerplate
3153
# content will be appended.
3254
PYPLOT_MAGIC_HEADER = (

0 commit comments

Comments
 (0)