Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
story645 merged 2 commits intomatplotlib:mainfromksunden:colortype
Apr 20, 2023

Conversation

ksunden
Copy link
Member

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:

frommatplotlib.typingimportColorTypea: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 (andpytest passes)
  • Documentation is sphinx and numpydoc compliant (the docs shouldbuild 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 indoc/users/next_whats_new/
  • API changes are marked with a.. versionchanged:: directive in the docstring and documented indoc/api/next_api_changes/
  • Release notes conform with instructions innext_whats_new/README.rst ornext_api_changes/README.rst

@timhoffm
Copy link
Member

Maybe SolidColorType instead if RawColorType?

@ksunden
Copy link
MemberAuthor

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

@rcomer
Copy link
Member

BaseColorType?

story645 reacted with thumbs up emoji

@tacaswelltacaswell added this to thev3.8.0 milestoneApr 1, 2023
@ksunden
Copy link
MemberAuthor

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 nottoo 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 useUnion[..., ...] 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 bymatplotlib#24691, merged shortly beforematplotlib#24976Noticed 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 betweentuple[RGBColorType, float] andtuple[tuple[float, float, float, float], float] given you've gottuple[float, float, float, float] here?

Copy link
MemberAuthor

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

@story645story645 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
MemberAuthor

There is no place where we actually use the RGB/RGBA specific types, but string is valid for either, so wecould specify if it were relevant.

Essentially I neededa 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 reacted with thumbs up emoji

@story645story645 merged commitddb3163 intomatplotlib:mainApr 20, 2023
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers

@story645story645story645 approved these changes

@timhoffmtimhoffmtimhoffm approved these changes

Assignees
No one assigned
Projects
None yet
Milestone
v3.8.0
Development

Successfully merging this pull request may close these issues.

6 participants
@ksunden@timhoffm@rcomer@story645@tacaswell@QuLogic

[8]ページ先頭

©2009-2025 Movatter.jp