Skip to content

Commit db6f788

Browse files
committed
Tighten Self annotations on ggplot and Compose operators
Promote return types from ggplot/Compose to Self where the body preserves the runtime class — ggplot's __deepcopy__, __add__ overloads, __or__/__truediv__/__sub__/__rrshift__, and Compose's __and__/__mul__. The other Compose operators (__or__, __truediv__, __sub__, __add__) keep Compose as their return type because they cross-construct (e.g. Stack | x → Beside) and would be unsound as Self. Also type the rhs of plot_spacer's __add__/__iadd__ explicitly so the override is visible to pyright.
1 parent 429a37a commit db6f788

2 files changed

Lines changed: 12 additions & 11 deletions

File tree

plotnine/composition/_compose.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
from typing import Iterator
2525

2626
from matplotlib.figure import Figure
27+
from typing_extensions import Self
2728

2829
from plotnine._mpl.gridspec import p9GridSpec
2930
from plotnine._mpl.layout_manager._composition_side_space import (
@@ -278,7 +279,7 @@ def __sub__(self, rhs: ggplot | Compose) -> Compose:
278279

279280
return Beside([self, rhs])
280281

281-
def __and__(self, rhs: PlotAddable) -> Compose:
282+
def __and__(self, rhs: PlotAddable) -> Self:
282283
"""
283284
Add rhs to all plots in the composition
284285
@@ -302,7 +303,7 @@ def __and__(self, rhs: PlotAddable) -> Compose:
302303

303304
return self
304305

305-
def __mul__(self, rhs: PlotAddable) -> Compose:
306+
def __mul__(self, rhs: PlotAddable) -> Self:
306307
"""
307308
Add rhs to the outermost nesting level of the composition
308309

plotnine/ggplot.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ def show(self):
222222
else:
223223
self.draw(show=True)
224224

225-
def __deepcopy__(self, memo: dict[Any, Any]) -> ggplot:
225+
def __deepcopy__(self, memo: dict[Any, Any]) -> Self:
226226
"""
227227
Deep copy without copying the dataframe and environment
228228
"""
@@ -264,15 +264,15 @@ def __iadd__(self, other: PlotAddable | list[PlotAddable] | None) -> Self:
264264
def __add__(
265265
self,
266266
rhs: PlotAddable | list[PlotAddable] | None,
267-
) -> ggplot: ...
267+
) -> Self: ...
268268

269269
@overload
270-
def __add__(self, rhs: ggplot) -> Compose: ...
270+
def __add__(self, rhs: Self) -> Compose: ...
271271

272272
def __add__(
273273
self,
274-
rhs: PlotAddable | list[PlotAddable] | None | ggplot,
275-
) -> ggplot | Compose:
274+
rhs: PlotAddable | list[PlotAddable] | None | Self,
275+
) -> Self | Compose:
276276
"""
277277
Add to ggplot
278278
@@ -293,31 +293,31 @@ def __add__(
293293

294294
return self.__iadd__(rhs)
295295

296-
def __or__(self, rhs: ggplot | Compose) -> Compose:
296+
def __or__(self, rhs: Self | Compose) -> Compose:
297297
"""
298298
Compose 2 plots columnwise
299299
"""
300300
from .composition import Beside
301301

302302
return Beside([self, rhs])
303303

304-
def __truediv__(self, rhs: ggplot | Compose) -> Compose:
304+
def __truediv__(self, rhs: Self | Compose) -> Compose:
305305
"""
306306
Compose 2 plots rowwise
307307
"""
308308
from .composition import Stack
309309

310310
return Stack([self, rhs])
311311

312-
def __sub__(self, rhs: ggplot | Compose) -> Compose:
312+
def __sub__(self, rhs: Self | Compose) -> Compose:
313313
"""
314314
Compose 2 plots columnwise
315315
"""
316316
from .composition import Beside
317317

318318
return Beside([self, rhs])
319319

320-
def __rrshift__(self, other: DataLike) -> ggplot:
320+
def __rrshift__(self, other: DataLike) -> Self:
321321
"""
322322
Overload the >> operator to receive a dataframe
323323
"""

0 commit comments

Comments
 (0)