Add (color, alpha) tuple as a valid ColorType in typing.py#25597
Add (color, alpha) tuple as a valid ColorType in typing.py#25597story645 merged 2 commits intomatplotlib:mainfrom
Conversation
|
Maybe SolidColorType instead if RawColorType? |
|
"Solid" would imply to me that there is no alpha, but an RGBA 4-tuple is valid there |
|
|
|
The other option that occurs to me, though it is slightly more complicated, but perhaps in a useful way: RGBColorType = str | tuple[float, float, float]
RGBAColorType = str | tuple[float, float, float, float] | tuple[RGBColorType, float] | tuple[tuple[float, float, float, float], float]
ColorType = RGBColorType | RGBAColorTypeNot so happy about the last leg of RGBA needing to be added (not as clean as I would like, but not too bad. Both need str, not only for named colors (which would actually almost exclusively be the RGB, not RGBA), but also for hex colors ( (The exception for named colors that have non-opaque alpha is (I wrote it out here with |
Introduced by matplotlib#24691, merged shortly before matplotlib#24976 Noticed that one of the other type aliases was not documented, so fixed that while I was editing these two files
lib/matplotlib/typing.py
Outdated
| # 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] |
There was a problem hiding this comment.
What's the difference between tuple[RGBColorType, float] and tuple[tuple[float, float, float, float], float] given you've got tuple[float, float, float, float] here?
There was a problem hiding this comment.
Ahh, oops, the four tuple should not be included in RGB, as that is only valid for RGBA...
story645
left a comment
There was a problem hiding this comment.
Though I wonder if string should be moved down to generic color type since there's no distinction of which strings are valid for which color type (are there areas of the code that only take RGB or RGBA?)
|
There is no place where we actually use the RGB/RGBA specific types, but string is valid for either, so we could specify if it were relevant. Essentially I needed a name to organize the thought of "2-tuple of this type plus float alpha" The original called it "RawColorType" but I didn't like that name and chose to go with something at least semantically meaningful as a distinction. |
PR Summary
Introduced by #24691, merged shortly before #24976
Not totally sold on the name 'RawColorType', but couldn't think of something I like better
Noticed that one of the other type aliases was not documented, so fixed that while I was editing these two files
Tested using:
Prior to PR d,e,f are flagged as invalid, but they pass the typechecker after this PR:
Raises the valid point of including tests like these in the repo/ci, but probably requires a bit more thought than this particular PR is ready to add initially.
PR Checklist
Documentation and Tests
pytestpasses)Release Notes
.. versionadded::directive in the docstring and documented indoc/users/next_whats_new/.. versionchanged::directive in the docstring and documented indoc/api/next_api_changes/next_whats_new/README.rstornext_api_changes/README.rst