Skip to content

Commit 80965f2

Browse files
committed
ggplot.add and ggplot.iadd both accept sequences
Triggered by the requirements for type-checking, these two have the same signature. And it just makes sense! REF: python/mypy#6225 #648
1 parent b0f7973 commit 80965f2

File tree

3 files changed

+42
-5
lines changed

3 files changed

+42
-5
lines changed

doc/changelog.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@ v0.10.2
55
-------
66
(Not yet released)
77

8+
API Changes
9+
***********
10+
11+
- :method:`~plotnine.ggplot.add` and :method:`~plotnine.ggplot.iadd` now
12+
accept the same types of objects. In this change
13+
:method:`~plotnine.ggplot.iadd` has gained the ability to accept a list
14+
of objects.
15+
816
Bug Fixes
917
*********
1018

plotnine/ggplot.py

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import sys
2+
from collections.abc import Sequence
23
from copy import deepcopy
34
from itertools import chain
45
from pathlib import Path
@@ -125,27 +126,38 @@ def __deepcopy__(self, memo):
125126
def __iadd__(self, other):
126127
"""
127128
Add other to ggplot object
129+
130+
Parameters
131+
----------
132+
other : object or Sequence
133+
Either an object that knows how to "radd"
134+
itself to a ggplot, or a list of such objects.
128135
"""
136+
if isinstance(other, Sequence):
137+
for item in other:
138+
item.__radd__(self, inplace=True)
139+
return self
140+
elif other is None:
141+
return self
142+
129143
try:
130144
return other.__radd__(self, inplace=True)
131145
except TypeError:
132146
return other.__radd__(self)
133147
except AttributeError as err:
134-
if other is None:
135-
return self.__add__(other)
136148
raise err
137149

138150
def __add__(self, other):
139151
"""
140-
Add to ggitems from a list
152+
Add to ggplot from a list
141153
142154
Parameters
143155
----------
144-
other : object or list
156+
other : object or Sequence
145157
Either an object that knows how to "radd"
146158
itself to a ggplot, or a list of such objects.
147159
"""
148-
if isinstance(other, list):
160+
if isinstance(other, Sequence):
149161
self = deepcopy(self)
150162
for item in other:
151163
self += item

tests/test_ggplot_internals.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -305,6 +305,23 @@ def test_adding_list_ggplot():
305305
assert isinstance(g.coordinates, coord_trans)
306306

307307

308+
def test_iadding_list_ggplot():
309+
lst = [
310+
geom_point(),
311+
geom_point(aes('x+1', 'y+1')),
312+
xlab('x-label'),
313+
coord_trans()
314+
]
315+
g = ggplot()
316+
id_before = id(g)
317+
g += lst
318+
id_after = id(g)
319+
assert id_before == id_after
320+
assert len(g.layers) == 2
321+
assert g.labels['x'] == 'x-label'
322+
assert isinstance(g.coordinates, coord_trans)
323+
324+
308325
def test_adding_None():
309326
p = ggplot(df, aes('x', 'y')) + geom_point()
310327
p2 = p + None

0 commit comments

Comments
 (0)