From 1ac55d4bd6aa2933ed54159efbc100752b571072 Mon Sep 17 00:00:00 2001 From: Hanjin Liu Date: Wed, 25 Sep 2024 23:45:26 +0900 Subject: [PATCH] fix is_array check --- tests/conftest.py | 4 ++++ whitecanvas/layers/_mixin.py | 16 ++++++++-------- whitecanvas/layers/_primitive/bars.py | 8 ++++---- whitecanvas/layers/group/graph.py | 2 +- whitecanvas/layers/group/labeled.py | 2 +- whitecanvas/utils/type_check.py | 4 +++- 6 files changed, 21 insertions(+), 15 deletions(-) diff --git a/tests/conftest.py b/tests/conftest.py index 65f36de..2115797 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -19,11 +19,15 @@ def backend(request: pytest.FixtureRequest): request.param.param = backend else: if getattr(request.function, "failed", None) == "mock": + _cleanup(request) pytest.fail("Failed in mock backend") yield request.param if request.node.rep_call.failed: request.function.failed = request.param + _cleanup(request) + +def _cleanup(request: pytest.FixtureRequest): if request.param == "matplotlib": plt.close("all") elif request.param == "pyqtgraph": diff --git a/whitecanvas/layers/_mixin.py b/whitecanvas/layers/_mixin.py index ad58b51..f99d214 100644 --- a/whitecanvas/layers/_mixin.py +++ b/whitecanvas/layers/_mixin.py @@ -653,7 +653,7 @@ def with_face( self, color: ColorType | _Void = _void, hatch: Hatch | str = Hatch.SOLID, - alpha: float = 1, + alpha: float | _Void = _void, ) -> Self: """Update the face properties.""" if is_array(hatch) or is_array(alpha): @@ -670,7 +670,7 @@ def with_edge( color: ColorType | None = None, width: float = 1.0, style: LineStyle | str = LineStyle.SOLID, - alpha: float = 1, + alpha: float | _Void = _void, ) -> Self: """Update the edge properties.""" if is_array(width) or is_array(style) or is_array(alpha): @@ -690,7 +690,7 @@ def with_face_multi( self, color: ColorType | Sequence[ColorType] | _Void = _void, hatch: str | Hatch | Sequence[str | Hatch] | _Void = _void, - alpha: float = 1, + alpha: float | _Void = _void, ) -> Self: if not isinstance(self._face_namespace, MultiFace): self._face_namespace.events.disconnect() @@ -704,7 +704,7 @@ def with_edge_multi( color: ColorType | Sequence[ColorType] | None = None, width: float | Sequence[float] = 1, style: str | LineStyle | list[str | LineStyle] = LineStyle.SOLID, - alpha: float = 1, + alpha: float | _Void = _void, ) -> Self: """Update the edge properties.""" if color is None: @@ -1051,7 +1051,7 @@ def with_face( *, color: ColorType | _Void = _void, hatch: Hatch | str = Hatch.SOLID, - alpha: float = 1, + alpha: float | _Void = _void, ) -> Self: """Update the face properties.""" if is_array(hatch) or is_array(alpha): @@ -1069,7 +1069,7 @@ def with_edge( color: ColorType | None = None, width: float = 1.0, style: LineStyle | str = LineStyle.SOLID, - alpha: float = 1, + alpha: float | _Void = _void, ) -> Self: """Update the edge properties.""" if is_array(width) or is_array(style) or is_array(alpha): @@ -1090,7 +1090,7 @@ def with_face_multi( *, color: ColorType | Sequence[ColorType] | _Void = _void, hatch: str | Hatch | Sequence[str | Hatch] = Hatch.SOLID, - alpha: float = 1, + alpha: float | _Void = _void, ) -> Self: if not isinstance(self._face_namespace, MultiFace): self._face_namespace.events.disconnect() @@ -1105,7 +1105,7 @@ def with_edge_multi( color: ColorType | Sequence[ColorType] | None = None, width: float | Sequence[float] = 1, style: str | LineStyle | list[str | LineStyle] = LineStyle.SOLID, - alpha: float = 1, + alpha: float | _Void = _void, ) -> Self: """Update the edge properties.""" if color is None: diff --git a/whitecanvas/layers/_primitive/bars.py b/whitecanvas/layers/_primitive/bars.py index 62ff2c0..b12e213 100644 --- a/whitecanvas/layers/_primitive/bars.py +++ b/whitecanvas/layers/_primitive/bars.py @@ -385,7 +385,7 @@ def with_face( *, color: ColorType | _Void = _void, hatch: Hatch | str = Hatch.SOLID, - alpha: float = 1, + alpha: float | _Void = _void, ) -> Bars[ConstFace, _Edge]: return super().with_face(color=color, hatch=hatch, alpha=alpha) @@ -394,7 +394,7 @@ def with_face_multi( *, color: ColorType | Sequence[ColorType] | _Void = _void, hatch: str | Hatch | Sequence[str | Hatch] | _Void = _void, - alpha: float = 1, + alpha: float | _Void = _void, ) -> Bars[MultiFace, _Edge]: return super().with_face_multi(color=color, hatch=hatch, alpha=alpha) @@ -404,7 +404,7 @@ def with_edge( color: ColorType | None = None, width: float = 1, style: LineStyle | str = LineStyle.SOLID, - alpha: float = 1, + alpha: float | _Void = _void, ) -> Bars[_Face, ConstEdge]: return super().with_edge(color=color, width=width, style=style, alpha=alpha) @@ -414,7 +414,7 @@ def with_edge_multi( color: ColorType | Sequence[ColorType] | None = None, width: float | Sequence[float] = 1, style: str | LineStyle | list[str | LineStyle] = LineStyle.SOLID, - alpha: float = 1, + alpha: float | _Void = _void, ) -> Bars[_Face, MultiEdge]: return super().with_edge_multi( color=color, width=width, style=style, alpha=alpha diff --git a/whitecanvas/layers/group/graph.py b/whitecanvas/layers/group/graph.py index 56a172f..dcd051b 100644 --- a/whitecanvas/layers/group/graph.py +++ b/whitecanvas/layers/group/graph.py @@ -108,7 +108,7 @@ def with_text( @classmethod def from_dict(cls, d: dict[str, Any], backend: Backend | str | None = None) -> Self: children = construct_layers(d["children"], backend=backend) - if (offset := d.get("offset")) is not None: + if isinstance(offset := d.get("offset"), dict): offset = parse_offset_dict(offset) return cls(*children, name=d.get("name"), offset=offset) diff --git a/whitecanvas/layers/group/labeled.py b/whitecanvas/layers/group/labeled.py index 82e0229..185014e 100644 --- a/whitecanvas/layers/group/labeled.py +++ b/whitecanvas/layers/group/labeled.py @@ -78,7 +78,7 @@ def _default_ordering(self, n: int) -> list[int]: @classmethod def from_dict(cls, d: dict[str, Any], backend: Backend | str | None = None) -> Self: children = construct_layers(d["children"], backend=backend) - if (offset := d.get("offset")) is not None: + if isinstance(offset := d.get("offset"), dict): offset = parse_offset_dict(offset) return cls(*children, name=d.get("name"), offset=offset) diff --git a/whitecanvas/utils/type_check.py b/whitecanvas/utils/type_check.py index affd760..05df3d1 100644 --- a/whitecanvas/utils/type_check.py +++ b/whitecanvas/utils/type_check.py @@ -7,6 +7,8 @@ import numpy as np +from whitecanvas.types import _Void + if TYPE_CHECKING: import pandas as pd import polars as pl @@ -15,7 +17,7 @@ def is_array(x) -> bool: """True if x is an array that needs multi-face/edge.""" - return not is_not_array(x) + return not (is_not_array(x) or isinstance(x, _Void)) def is_not_array(x) -> bool: