diff --git a/tools/boilerplate.py b/tools/boilerplate.py index 90f5d709ac17..cf013ef0db8d 100644 --- a/tools/boilerplate.py +++ b/tools/boilerplate.py @@ -24,9 +24,31 @@ import numpy as np from matplotlib import _api, mlab from matplotlib.axes import Axes +from matplotlib.backend_bases import MouseButton from matplotlib.figure import Figure +# we need to define a custom str because py310 change +# In Python 3.10 the repr and str representation of Enums changed from +# +# str: 'ClassName.NAME' -> 'NAME' +# repr: '' -> '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 +# https://github.com/python/cpython/pull/22392/ +def enum_str_back_compat_patch(self): + return f'{type(self).__name__}.{self.name}' + +# only monkey patch if we have to. +if str(MouseButton.LEFT) != 'MouseButton.Left': + MouseButton.__str__ = enum_str_back_compat_patch + + # This is the magic line that must exist in pyplot, after which the boilerplate # content will be appended. PYPLOT_MAGIC_HEADER = (