Skip to content

Commit c0e1c1b

Browse files
committed
Properly deprecate access to second item returned by HandlerLine2D.
1 parent 3539f18 commit c0e1c1b

File tree

1 file changed

+24
-2
lines changed

1 file changed

+24
-2
lines changed

lib/matplotlib/legend_handler.py

+24-2
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,12 @@
2727
def legend_artist(self, legend, orig_handle, fontsize, handlebox)
2828
"""
2929

30+
from collections.abc import Sequence
3031
from itertools import cycle
3132

3233
import numpy as np
3334

34-
from matplotlib import cbook
35+
from matplotlib import _api, cbook
3536
from matplotlib.lines import Line2D
3637
from matplotlib.patches import Rectangle
3738
import matplotlib.collections as mcoll
@@ -119,6 +120,9 @@ def legend_artist(self, legend, orig_handle,
119120
xdescent, ydescent, width, height,
120121
fontsize, handlebox.get_transform())
121122

123+
if isinstance(artists, _Line2DHandleList):
124+
artists = [artists[0]]
125+
122126
# create_artists will return a list of artists.
123127
for a in artists:
124128
handlebox.add_artist(a)
@@ -254,6 +258,24 @@ def create_artists(self, legend, orig_handle,
254258
return [legline, legline_marker]
255259

256260

261+
class _Line2DHandleList(Sequence):
262+
def __init__(self, legline):
263+
self._legline = legline
264+
265+
def __len__(self):
266+
return 2
267+
268+
def __getitem__(self, index):
269+
if index != 0:
270+
# Make HandlerLine2D return [self._legline] directly after
271+
# deprecation elapses.
272+
_api.warn_deprecated(
273+
"3.5", message="Access to the second element returned by "
274+
"HandlerLine2D is deprecated since %(since)s; it will be "
275+
"removed %(removal)s.")
276+
return [self._legline, self._legline][index]
277+
278+
257279
class HandlerLine2D(HandlerNpoints):
258280
"""
259281
Handler for `.Line2D` instances.
@@ -304,7 +326,7 @@ def create_artists(self, legend, orig_handle,
304326

305327
legline.set_transform(trans)
306328

307-
return [legline]
329+
return _Line2DHandleList(legline)
308330

309331

310332
class HandlerPatch(HandlerBase):

0 commit comments

Comments
 (0)