From 93973abba42593e4227e036e70e4ca2a0cf5aef7 Mon Sep 17 00:00:00 2001 From: Thomas A Caswell Date: Mon, 3 May 2021 20:58:55 -0400 Subject: [PATCH] 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' 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/ --- tools/boilerplate.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) 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 = (