Skip to content

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

Merged
merged 2 commits into from
Apr 20, 2023

Conversation

ksunden
Copy link
Member

@ksunden ksunden commented Apr 1, 2023

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:

from matplotlib.typing import ColorType

a: ColorType = "blue"
b: ColorType = (.5, .2, .1)
c: ColorType = (.5, .2, .1, .7)

d: ColorType = ("blue", .8)
e: ColorType = ((.5, .2, .1), .7)
f: ColorType = ((.5, .2, .1, .7), .5)

Prior to PR d,e,f are flagged as invalid, but they pass the typechecker after this PR:

kyle@plutonium py@edge play $ mypy mypy_mpl.py 
mypy_mpl.py:7: error: Incompatible types in assignment (expression has type "Tuple[str, float]", variable has type "Union[Tuple[float, float, float], Tuple[float, float, float, float], str]")  [assignment]
mypy_mpl.py:8: error: Incompatible types in assignment (expression has type "Tuple[Tuple[float, float, float], float]", variable has type "Union[Tuple[float, float, float], Tuple[float, float, float, float], str]")  [assignment]
mypy_mpl.py:9: error: Incompatible types in assignment (expression has type "Tuple[Tuple[float, float, float, float], float]", variable has type "Union[Tuple[float, float, float], Tuple[float, float, float, float], str]")  [assignment]
Found 3 errors in 1 file (checked 1 source file)
kyle@plutonium py@edge play $ mypy mypy_mpl.py 
Success: no issues found in 1 source file

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

  • Has pytest style unit tests (and pytest passes)
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • New plotting related features are documented with examples.

Release Notes

  • New features are marked with a .. versionadded:: directive in the docstring and documented in doc/users/next_whats_new/
  • API changes are marked with a .. versionchanged:: directive in the docstring and documented in doc/api/next_api_changes/
  • Release notes conform with instructions in next_whats_new/README.rst or next_api_changes/README.rst

@timhoffm
Copy link
Member

timhoffm commented Apr 1, 2023

Maybe SolidColorType instead if RawColorType?

@ksunden
Copy link
Member Author

ksunden commented Apr 1, 2023

"Solid" would imply to me that there is no alpha, but an RGBA 4-tuple is valid there

@rcomer
Copy link
Member

rcomer commented Apr 1, 2023

BaseColorType?

@tacaswell tacaswell added this to the v3.8.0 milestone Apr 1, 2023
@ksunden
Copy link
Member Author

ksunden commented Apr 3, 2023

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 ("#RRGGBB(AA)") and while in some sense those are a different "type", we pass them as strings and don't have a good way of telling the typechecker the difference satisfactorily.

(The exception for named colors that have non-opaque alpha is "none", I'm pretty sure all others have no alpha component inherently)

(I wrote it out here with | notation, which is py3.10+ but more readable, would have to use Union[..., ...] in this context since the type alias is actually potentially evaluated at runtime (not imported by default, but importable; unlike type annotations which are not directly evaluated))

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
@@ -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]
Copy link
Member

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?

Copy link
Member Author

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...

Copy link
Member

@story645 story645 left a 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?)

@ksunden
Copy link
Member Author

ksunden commented Apr 20, 2023

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.

@story645 story645 merged commit ddb3163 into matplotlib:main Apr 20, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants