Skip to content
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

fix: _Color type alias of PIL.Image #8210

Merged
merged 5 commits into from
Jul 4, 2022
Merged

fix: _Color type alias of PIL.Image #8210

merged 5 commits into from
Jul 4, 2022

Conversation

Antyos
Copy link
Contributor

@Antyos Antyos commented Jun 30, 2022

The _Color type for Pillow's Image is defined as an integer, not a float.

from PIL import Image
>>> Image.new("RGB", (100, 100), 0.0)
TypeError: color must be int or tuple
>>> Image.new("RGB", (100, 100), (0.0, 0.0, 0.0))  
TypeError: 'float' object cannot be interpreted as an integer

Colors will also throw an error if they are not 1-, 3-, or 4- tuples, and strings can be interpolated as colors.

from PIL import Image
>>> Image.new("RGB", (100, 100), (0, 0))
TypeError: color must be int, or tuple of one, three or four elements
>>> Image.new("RGB", (100, 100), "black")  # ok
>>> Image.new("RGB", (100, 100), "#000000")  # ok

Also, I think #8090 may have missed references to _Box on lines 155 and 178, though I could be wrong. Would that be better as its own PR?

When executing `Image.new("RGB", (100,100), 255.0)`:
`TypeError: color must be int or tuple`
Colors will also throw an error if they are not 1-, 3-, or 4- tuples, and strings are interpolated as colors
@github-actions

This comment has been minimized.

@AlexWaygood
Copy link
Member

You've hit a known mypy bug with PEP 604 type aliases: python/mypy#11098

The solution is to use an old-style Union here.

@github-actions

This comment has been minimized.

@eggplants
Copy link
Contributor

eggplants commented Jul 2, 2022

@Antyos

I think #8090 may have missed references to _Box on lines 155 and 178.

I only noticed the mistake and corrected it while using paste, but did not actually check or verify Pillow's source for the others.

Lines 155 and 178 are suspicious, but you have to make sure their type actually is wrong.

@AlexWaygood
Copy link
Member

Also, I think #8090 may have missed references to _Box on lines 155 and 178, though I could be wrong. Would that be better as its own PR?

Yeah, let's fix one problem at a time.

@AlexWaygood AlexWaygood self-requested a review July 2, 2022 17:22
Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, but this isn't quite correct at the moment: see my comment below.

Copy link
Contributor Author

@Antyos Antyos left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add tuple[float] to _Color

@github-actions

This comment has been minimized.

Copy link
Member

@AlexWaygood AlexWaygood left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@github-actions
Copy link
Contributor

github-actions bot commented Jul 4, 2022

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@AlexWaygood AlexWaygood merged commit 0367fc9 into python:master Jul 4, 2022
@Antyos Antyos deleted the fix-pillow-color branch July 5, 2022 14:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants