From f95e9b6d7cb264c67447f8bf699c71b3063bfd9c Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Sat, 6 May 2023 00:15:08 -0400 Subject: [PATCH 1/2] Accept strings for {Cap,Join}Style in MarkerStyle When these Enum classes were added in #18544, they were supposed to be for documentation only. To that end, #22055 was a followup that ensured that only the strings were exposed from the getter side. However, when user-supplied cap/join style were added in #20914, they were only for the Enum type instead of the string, so correctly allow strings here as well. Also, specifically type hint the return values as literals, as was done in #25719. --- lib/matplotlib/markers.py | 8 ++++---- lib/matplotlib/markers.pyi | 17 ++++++++--------- lib/matplotlib/tests/test_marker.py | 14 ++++++-------- 3 files changed, 18 insertions(+), 21 deletions(-) diff --git a/lib/matplotlib/markers.py b/lib/matplotlib/markers.py index 76a7c0d5af81..ff2fae62e19c 100644 --- a/lib/matplotlib/markers.py +++ b/lib/matplotlib/markers.py @@ -241,16 +241,16 @@ def __init__(self, marker, Transform that will be combined with the native transform of the marker. - capstyle : CapStyle, default: None + capstyle : `.CapStyle` or %(CapStyle)s, default: None Cap style that will override the default cap style of the marker. - joinstyle : JoinStyle, default: None + joinstyle : `.JoinStyle` or %(JoinStyle)s, default: None Join style that will override the default join style of the marker. """ self._marker_function = None self._user_transform = transform - self._user_capstyle = capstyle - self._user_joinstyle = joinstyle + self._user_capstyle = CapStyle(capstyle) if capstyle is not None else None + self._user_joinstyle = JoinStyle(joinstyle) if joinstyle is not None else None self._set_fillstyle(fillstyle) self._set_marker(marker) diff --git a/lib/matplotlib/markers.pyi b/lib/matplotlib/markers.pyi index f6cb3f919ccd..f8d46cdefa21 100644 --- a/lib/matplotlib/markers.pyi +++ b/lib/matplotlib/markers.pyi @@ -1,9 +1,10 @@ -from ._enums import CapStyle, JoinStyle +from typing import Literal + from .path import Path from .transforms import Affine2D, Transform from numpy.typing import ArrayLike -from .typing import FillStyleType +from .typing import CapStyleType, FillStyleType, JoinStyleType TICKLEFT: int TICKRIGHT: int @@ -28,16 +29,14 @@ class MarkerStyle: marker: str | ArrayLike | Path | MarkerStyle | None, fillstyle: FillStyleType | None = ..., transform: Transform | None = ..., - capstyle: CapStyle | None = ..., - joinstyle: JoinStyle | None = ..., + capstyle: CapStyleType | None = ..., + joinstyle: JoinStyleType | None = ..., ) -> None: ... def __bool__(self) -> bool: ... def is_filled(self) -> bool: ... - def get_fillstyle( - self, - ) -> FillStyleType: ... - def get_joinstyle(self) -> JoinStyle: ... - def get_capstyle(self) -> CapStyle: ... + def get_fillstyle(self) -> FillStyleType: ... + def get_joinstyle(self) -> Literal["miter", "round", "bevel"]: ... + def get_capstyle(self) -> Literal["butt", "projecting", "round"]: ... def get_marker(self) -> str | ArrayLike | Path | MarkerStyle | None: ... def get_path(self) -> Path: ... def get_transform(self) -> Transform: ... diff --git a/lib/matplotlib/tests/test_marker.py b/lib/matplotlib/tests/test_marker.py index 22d619d395fe..13f977dc38f1 100644 --- a/lib/matplotlib/tests/test_marker.py +++ b/lib/matplotlib/tests/test_marker.py @@ -207,18 +207,16 @@ def test_marker_init_transforms(): def test_marker_init_joinstyle(): marker = markers.MarkerStyle("*") - jstl = markers.JoinStyle.round - styled_marker = markers.MarkerStyle("*", joinstyle=jstl) - assert styled_marker.get_joinstyle() == jstl - assert marker.get_joinstyle() != jstl + styled_marker = markers.MarkerStyle("*", joinstyle="round") + assert styled_marker.get_joinstyle() == "round" + assert marker.get_joinstyle() != "round" def test_marker_init_captyle(): marker = markers.MarkerStyle("*") - capstl = markers.CapStyle.round - styled_marker = markers.MarkerStyle("*", capstyle=capstl) - assert styled_marker.get_capstyle() == capstl - assert marker.get_capstyle() != capstl + styled_marker = markers.MarkerStyle("*", capstyle="round") + assert styled_marker.get_capstyle() == "round" + assert marker.get_capstyle() != "round" @pytest.mark.parametrize("marker,transform,expected", [ From 1e28c2ea198714de0d2ca15cb1c59c5a1c4e3dd4 Mon Sep 17 00:00:00 2001 From: Elliott Sales de Andrade Date: Sat, 6 May 2023 00:34:53 -0400 Subject: [PATCH 2/2] Fix types for MarkerStyle It is not possible for `get_marker` to return another `MarkerStyle`; the `__init__` only takes one to make a full copy of it. And `get_alt_path` is only available for e.g., half-filled markers, but not all. --- lib/matplotlib/markers.py | 6 +++--- lib/matplotlib/markers.pyi | 4 ++-- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/matplotlib/markers.py b/lib/matplotlib/markers.py index ff2fae62e19c..eb689ef661c7 100644 --- a/lib/matplotlib/markers.py +++ b/lib/matplotlib/markers.py @@ -161,11 +161,11 @@ class MarkerStyle: Attributes ---------- - markers : list + markers : dict All known markers. - filled_markers : list + filled_markers : tuple All known filled markers. This is a subset of *markers*. - fillstyles : list + fillstyles : tuple The supported fillstyles. """ diff --git a/lib/matplotlib/markers.pyi b/lib/matplotlib/markers.pyi index f8d46cdefa21..3ee538838514 100644 --- a/lib/matplotlib/markers.pyi +++ b/lib/matplotlib/markers.pyi @@ -37,10 +37,10 @@ class MarkerStyle: def get_fillstyle(self) -> FillStyleType: ... def get_joinstyle(self) -> Literal["miter", "round", "bevel"]: ... def get_capstyle(self) -> Literal["butt", "projecting", "round"]: ... - def get_marker(self) -> str | ArrayLike | Path | MarkerStyle | None: ... + def get_marker(self) -> str | ArrayLike | Path | None: ... def get_path(self) -> Path: ... def get_transform(self) -> Transform: ... - def get_alt_path(self) -> Path: ... + def get_alt_path(self) -> Path | None: ... def get_alt_transform(self) -> Transform: ... def get_snap_threshold(self) -> float | None: ... def get_user_transform(self) -> Transform | None: ...