Skip to content

Commit

Permalink
FIX: MouseButton representation in boilerplate generated signatures
Browse files Browse the repository at this point in the history
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
  • Loading branch information
tacaswell committed May 4, 2021
1 parent a4a84e9 commit 93973ab
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions tools/boilerplate.py
Original file line number Diff line number Diff line change
Expand Up @@ -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: 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
# 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 = (
Expand Down

0 comments on commit 93973ab

Please sign in to comment.