Skip to content

Conversation

@pantheraleo-7
Copy link
Contributor

@pantheraleo-7 pantheraleo-7 commented Dec 12, 2024

Currently the return types are annotated as PIL.Image (the module) and not the intended (?) PIL.Image.Image (the class). This PR rectifies that.

@pantheraleo-7 pantheraleo-7 changed the title Correctly import PIL Image Correctly annotate PIL Image(s) Dec 12, 2024
@pantheraleo-7 pantheraleo-7 changed the base branch from main to next December 16, 2024 19:15
@pantheraleo-7
Copy link
Contributor Author

@davidplowman is this PR missing something?

@davidplowman
Copy link
Collaborator

@davidplowman is this PR missing something?

Hi again, no don't think so. Can you just explain what was wrong about it previously, is it causing errors somehow? (sorry, I haven't quite understood)

@pantheraleo-7
Copy link
Contributor Author

Yes, it is causing type checker errors.

PIL is a library (analogous to a folder)
PIL.Image is a module (an Image.py file in the PIL folder)
PIL.Image.Image is a class (this Image class represents all the PIL images, it is in the Image.py file of the PIL folder)

Currently when we return PIL images, we annotate it as PIL.Image, so the type checker assumes we are returning the module itself instead of the PIL Image. This causes all kinds of type errors.

@pantheraleo-7
Copy link
Contributor Author

pantheraleo-7 commented Dec 17, 2024

The type checker even warns us that a module cannot be annotated as a return type, but we do anyway. And this in turn causes many type checker errors in the downstream usage.

image

@pantheraleo-7
Copy link
Contributor Author

This can be solved in two ways:

  • we directly import the Image class using from PIL.Image import Image
  • we import the module from PIL import Image (the convention) and annotate everything with the class Image.Image

@pantheraleo-7
Copy link
Contributor Author

pantheraleo-7 commented Dec 17, 2024

Before:
Screenshot 2024-12-17 at 8 55 05 PM

After:
image

@davidplowman
Copy link
Collaborator

OK, thanks for the explanation!

@davidplowman davidplowman merged commit 31e381f into raspberrypi:next Dec 17, 2024
4 checks passed
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.

2 participants