Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Protocols in typing #326

Merged
merged 3 commits into from
Apr 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/plopp/backends/matplotlib/tiled.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import numpy as np
from matplotlib import gridspec

from ..protocols import FigureLike
from ...core.typing import FigureLike
from .static import get_repr_maker
from .utils import copy_figure, is_interactive_backend, make_figure

Expand Down Expand Up @@ -73,9 +73,11 @@ def __init__(
self.nrows = nrows
self.ncols = ncols
self.fig = make_figure(
figsize=(min(6.0 * ncols, 15.0), min(4.0 * nrows, 15.0))
if figsize is None
else figsize,
figsize=(
(min(6.0 * ncols, 15.0), min(4.0 * nrows, 15.0))
if figsize is None
else figsize
),
layout='constrained',
)

Expand Down
2 changes: 1 addition & 1 deletion src/plopp/backends/matplotlib/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import matplotlib as mpl
from matplotlib.pyplot import Figure, _get_backend_mod

from ..protocols import FigureLike
from ...core.typing import FigureLike


def fig_to_bytes(fig: Figure, form: Literal['png', 'svg'] = 'png') -> bytes:
Expand Down
154 changes: 0 additions & 154 deletions src/plopp/backends/protocols.py

This file was deleted.

155 changes: 153 additions & 2 deletions src/plopp/core/typing.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# SPDX-License-Identifier: BSD-3-Clause
# Copyright (c) 2023 Scipp contributors (https://github.com/scipp)

from typing import Dict, Union
from __future__ import annotations

from typing import Dict, Protocol, Tuple, Union

import scipp as sc
from numpy import ndarray
from scipp.typing import VariableLike

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

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


class CanvasLike(Protocol):
def autoscale(self) -> None:
...

def draw(self) -> None:
...

def save(self) -> None:
...

@property
def empty(self) -> bool:
...

@property
def title(self) -> str:
...

@title.setter
def title(self, title: str) -> None:
...

@property
def xlabel(self) -> str:
...

@xlabel.setter
def xlabel(self, xlabel: str) -> None:
...

@property
def ylabel(self) -> str:
...

@ylabel.setter
def ylabel(self, ylabel: str) -> None:
...

@property
def xscale(self) -> str:
...

@xscale.setter
def xscale(self, xscale: str) -> None:
...

@property
def yscale(self) -> str:
...

@yscale.setter
def yscale(self, yscale: str) -> None:
...

@property
def xmin(self) -> float:
...

@xmin.setter
def xmin(self, xmin: float) -> None:
...

@property
def xmax(self) -> float:
...

@xmax.setter
def xmax(self, xmax: float) -> None:
...

@property
def ymin(self) -> float:
...

@ymin.setter
def ymin(self, ymin: float) -> None:
...

@property
def ymax(self) -> float:
...

@ymax.setter
def ymax(self, ymax: float) -> None:
...

@property
def xrange(self) -> Tuple[float, float]:
...

@xrange.setter
def xrange(self, xrange: Tuple[float, float]) -> None:
...

@property
def yrange(self) -> Tuple[float, float]:
...

@yrange.setter
def yrange(self, yrange: Tuple[float, float]) -> None:
...

def logx(self) -> None:
...

def logy(self) -> None:
...


class ArtistLike(Protocol):
def update(self, new_values: sc.DataArray) -> None:
...


class FigureLike(Protocol):
@property
def canvas(self) -> CanvasLike:
...

@property
def artists(self) -> Dict[str, ArtistLike]:
...

@property
def graph_nodes(self) -> Dict[str, Node]:
...

@property
def id(self) -> str:
...

def save(self, filename: str, **kwargs) -> None:
...

def update(self, *args, **kwargs) -> None:
...

def notify_view(self, *args, **kwargs) -> None:
...

def copy(self, **kwargs) -> FigureLike:
...


__all__ = [
'VisibleDeprecationWarning',
'Plottable',
'PlottableMulti',
'VisibleDeprecationWarning',
'CanvasLike',
'ArtistLike',
'FigureLike',
]
3 changes: 2 additions & 1 deletion src/plopp/graphics/imageview.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import scipp as sc

from .. import backends
from ..core.typing import FigureLike
from .colormapper import ColorMapper
from .graphicalview import GraphicalView

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


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

Expand Down
3 changes: 2 additions & 1 deletion src/plopp/graphics/lineview.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import scipp as sc

from .. import backends
from ..core.typing import FigureLike
from .graphicalview import GraphicalView


Expand Down Expand Up @@ -117,7 +118,7 @@ def make_artist(self, new_values):
)


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

Expand Down
Loading