Skip to content

Commit e8b49be

Browse files
authored
Merge pull request #326 from scipp/protocols-in-typing
Protocols in typing
2 parents 8acb49d + ff7348f commit e8b49be

File tree

15 files changed

+181
-177
lines changed

15 files changed

+181
-177
lines changed

src/plopp/backends/matplotlib/tiled.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import numpy as np
99
from matplotlib import gridspec
1010

11-
from ..protocols import FigureLike
11+
from ...core.typing import FigureLike
1212
from .static import get_repr_maker
1313
from .utils import copy_figure, is_interactive_backend, make_figure
1414

@@ -73,9 +73,11 @@ def __init__(
7373
self.nrows = nrows
7474
self.ncols = ncols
7575
self.fig = make_figure(
76-
figsize=(min(6.0 * ncols, 15.0), min(4.0 * nrows, 15.0))
77-
if figsize is None
78-
else figsize,
76+
figsize=(
77+
(min(6.0 * ncols, 15.0), min(4.0 * nrows, 15.0))
78+
if figsize is None
79+
else figsize
80+
),
7981
layout='constrained',
8082
)
8183

src/plopp/backends/matplotlib/utils.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import matplotlib as mpl
88
from matplotlib.pyplot import Figure, _get_backend_mod
99

10-
from ..protocols import FigureLike
10+
from ...core.typing import FigureLike
1111

1212

1313
def fig_to_bytes(fig: Figure, form: Literal['png', 'svg'] = 'png') -> bytes:

src/plopp/backends/protocols.py

Lines changed: 0 additions & 154 deletions
This file was deleted.

src/plopp/core/typing.py

Lines changed: 153 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
# SPDX-License-Identifier: BSD-3-Clause
22
# Copyright (c) 2023 Scipp contributors (https://github.com/scipp)
33

4-
from typing import Dict, Union
4+
from __future__ import annotations
55

6+
from typing import Dict, Protocol, Tuple, Union
7+
8+
import scipp as sc
69
from numpy import ndarray
710
from scipp.typing import VariableLike
811

@@ -23,8 +26,156 @@ class VisibleDeprecationWarning(UserWarning):
2326

2427
PlottableMulti = Union[Plottable, Dict[str, Plottable]]
2528

29+
30+
class CanvasLike(Protocol):
31+
def autoscale(self) -> None:
32+
...
33+
34+
def draw(self) -> None:
35+
...
36+
37+
def save(self) -> None:
38+
...
39+
40+
@property
41+
def empty(self) -> bool:
42+
...
43+
44+
@property
45+
def title(self) -> str:
46+
...
47+
48+
@title.setter
49+
def title(self, title: str) -> None:
50+
...
51+
52+
@property
53+
def xlabel(self) -> str:
54+
...
55+
56+
@xlabel.setter
57+
def xlabel(self, xlabel: str) -> None:
58+
...
59+
60+
@property
61+
def ylabel(self) -> str:
62+
...
63+
64+
@ylabel.setter
65+
def ylabel(self, ylabel: str) -> None:
66+
...
67+
68+
@property
69+
def xscale(self) -> str:
70+
...
71+
72+
@xscale.setter
73+
def xscale(self, xscale: str) -> None:
74+
...
75+
76+
@property
77+
def yscale(self) -> str:
78+
...
79+
80+
@yscale.setter
81+
def yscale(self, yscale: str) -> None:
82+
...
83+
84+
@property
85+
def xmin(self) -> float:
86+
...
87+
88+
@xmin.setter
89+
def xmin(self, xmin: float) -> None:
90+
...
91+
92+
@property
93+
def xmax(self) -> float:
94+
...
95+
96+
@xmax.setter
97+
def xmax(self, xmax: float) -> None:
98+
...
99+
100+
@property
101+
def ymin(self) -> float:
102+
...
103+
104+
@ymin.setter
105+
def ymin(self, ymin: float) -> None:
106+
...
107+
108+
@property
109+
def ymax(self) -> float:
110+
...
111+
112+
@ymax.setter
113+
def ymax(self, ymax: float) -> None:
114+
...
115+
116+
@property
117+
def xrange(self) -> Tuple[float, float]:
118+
...
119+
120+
@xrange.setter
121+
def xrange(self, xrange: Tuple[float, float]) -> None:
122+
...
123+
124+
@property
125+
def yrange(self) -> Tuple[float, float]:
126+
...
127+
128+
@yrange.setter
129+
def yrange(self, yrange: Tuple[float, float]) -> None:
130+
...
131+
132+
def logx(self) -> None:
133+
...
134+
135+
def logy(self) -> None:
136+
...
137+
138+
139+
class ArtistLike(Protocol):
140+
def update(self, new_values: sc.DataArray) -> None:
141+
...
142+
143+
144+
class FigureLike(Protocol):
145+
@property
146+
def canvas(self) -> CanvasLike:
147+
...
148+
149+
@property
150+
def artists(self) -> Dict[str, ArtistLike]:
151+
...
152+
153+
@property
154+
def graph_nodes(self) -> Dict[str, Node]:
155+
...
156+
157+
@property
158+
def id(self) -> str:
159+
...
160+
161+
def save(self, filename: str, **kwargs) -> None:
162+
...
163+
164+
def update(self, *args, **kwargs) -> None:
165+
...
166+
167+
def notify_view(self, *args, **kwargs) -> None:
168+
...
169+
170+
def copy(self, **kwargs) -> FigureLike:
171+
...
172+
173+
26174
__all__ = [
175+
'VisibleDeprecationWarning',
27176
'Plottable',
28177
'PlottableMulti',
29-
'VisibleDeprecationWarning',
178+
'CanvasLike',
179+
'ArtistLike',
180+
'FigureLike',
30181
]

src/plopp/graphics/imageview.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import scipp as sc
77

88
from .. import backends
9+
from ..core.typing import FigureLike
910
from .colormapper import ColorMapper
1011
from .graphicalview import GraphicalView
1112

@@ -114,7 +115,7 @@ def make_artist(self, new_values):
114115
return backends.image(canvas=self.canvas, data=new_values, **self._kwargs)
115116

116117

117-
def imagefigure(*args, **kwargs):
118+
def imagefigure(*args, **kwargs) -> FigureLike:
118119
"""
119120
Create a figure to represent two-dimensional data from one or more graph node(s).
120121

src/plopp/graphics/lineview.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import scipp as sc
77

88
from .. import backends
9+
from ..core.typing import FigureLike
910
from .graphicalview import GraphicalView
1011

1112

@@ -117,7 +118,7 @@ def make_artist(self, new_values):
117118
)
118119

119120

120-
def linefigure(*args, **kwargs):
121+
def linefigure(*args, **kwargs) -> FigureLike:
121122
"""
122123
Create a figure to represent one-dimensional data from one or more graph node(s).
123124

0 commit comments

Comments
 (0)