-
-
Notifications
You must be signed in to change notification settings - Fork 7.8k
Add (color, alpha) tuple as a valid ColorType in typing.py #25597
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
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 | RGBAColorType Not 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
@@ -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] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ahh, oops, the four tuple should not be included in RGB, as that is only valid for RGBA...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
pytest
passes)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.rst
ornext_api_changes/README.rst