From 5ee8a742966efe9fd9e96722f65a109d3060b483 Mon Sep 17 00:00:00 2001 From: Kyle Sunden Date: Fri, 31 Mar 2023 20:07:31 -0500 Subject: [PATCH 1/2] Add (color, alpha) tuple as a valid ColorType Introduced by #24691, merged shortly before #24976 Noticed that one of the other type aliases was not documented, so fixed that while I was editing these two files --- .pre-commit-config.yaml | 1 + doc/api/typing_api.rst | 6 ++++++ lib/matplotlib/typing.py | 17 ++++++++++++++++- 3 files changed, 23 insertions(+), 1 deletion(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b15a4cfc7f7b..d1acef009e36 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -17,6 +17,7 @@ repos: hooks: - id: check-added-large-files - id: check-docstring-first + exclude: lib/matplotlib/typing.py # docstring used for attribute flagged by check - id: end-of-file-fixer exclude_types: [svg] - id: mixed-line-ending diff --git a/doc/api/typing_api.rst b/doc/api/typing_api.rst index 16bf69d546af..ca11fecf81b7 100644 --- a/doc/api/typing_api.rst +++ b/doc/api/typing_api.rst @@ -2,6 +2,10 @@ ``matplotlib.typing`` ********************* +.. autodata:: matplotlib.typing.RGBColorType +.. autodata:: matplotlib.typing.RGBColourType +.. autodata:: matplotlib.typing.RGBAColorType +.. autodata:: matplotlib.typing.RGBAColourType .. autodata:: matplotlib.typing.ColorType .. autodata:: matplotlib.typing.ColourType .. autodata:: matplotlib.typing.LineStyleType @@ -9,3 +13,5 @@ .. autodata:: matplotlib.typing.MarkEveryType .. autodata:: matplotlib.typing.FillStyleType .. autodata:: matplotlib.typing.RcStyleType +.. autodata:: matplotlib.typing.HashableList + :annotation: Nested list with Hashable values diff --git a/lib/matplotlib/typing.py b/lib/matplotlib/typing.py index 6dcf82494020..a6a63fa12fd5 100644 --- a/lib/matplotlib/typing.py +++ b/lib/matplotlib/typing.py @@ -19,7 +19,21 @@ # The following are type aliases. Once python 3.9 is dropped, they should be annotated # using ``typing.TypeAlias`` and Unions should be converted to using ``|`` syntax. -ColorType = Union[tuple[float, float, float], tuple[float, float, float, float], str] +RGBColorType = Union[tuple[float, float, float], tuple[float, float, float, float], str] +RGBAColorType = Union[ + str, # "none" or "#RRGGBBAA"/"#RGBA" hex strings + tuple[float, float, float, float], + # 2 tuple (color, alpha) representations, not infinitely recursive + # RGBColorType includes the (str, float) tuple, even for RGBA strings + tuple[RGBColorType, float], + # (4-tuple, float) is odd, but accepted as the outer float overriding A of 4-tuple + tuple[tuple[float, float, float, float], float] +] + +ColorType = Union[RGBColorType, RGBAColorType] + +RGBColourType = RGBColorType +RGBAColourType = RGBAColorType ColourType = ColorType LineStyleType = Union[str, tuple[float, Sequence[float]]] @@ -43,3 +57,4 @@ ] HashableList = list[Union[Hashable, "HashableList"]] +"""A nested list of Hashable values.""" From 77f6919761776aedf2891a738fd353c54bc533da Mon Sep 17 00:00:00 2001 From: Kyle Sunden Date: Wed, 19 Apr 2023 11:52:40 -0500 Subject: [PATCH 2/2] Remove 4-tuple from RGBColorType (only valid for RGBA) --- lib/matplotlib/typing.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/matplotlib/typing.py b/lib/matplotlib/typing.py index a6a63fa12fd5..d3237053a7bb 100644 --- a/lib/matplotlib/typing.py +++ b/lib/matplotlib/typing.py @@ -19,7 +19,7 @@ # The following are type aliases. Once python 3.9 is dropped, they should be annotated # using ``typing.TypeAlias`` and Unions should be converted to using ``|`` syntax. -RGBColorType = Union[tuple[float, float, float], tuple[float, float, float, float], str] +RGBColorType = Union[tuple[float, float, float], str] RGBAColorType = Union[ str, # "none" or "#RRGGBBAA"/"#RGBA" hex strings tuple[float, float, float, float],