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

Skip to content

Commit 421911c

Browse files
timhoffmmeeseeksmachine
authored andcommitted
Backport PR #20148: FIX: MouseButton representation in boilerplate generated signatures
1 parent c56ebde commit 421911c

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

tools/boilerplate.py

Lines changed: 22 additions & 0 deletions
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)