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

Typing difficulty because Image is a module and also a class #6676

Closed
scarf005 opened this issue Oct 23, 2022 · 5 comments
Closed

Typing difficulty because Image is a module and also a class #6676

scarf005 opened this issue Oct 23, 2022 · 5 comments

Comments

@scarf005
Copy link

What did you do?

from PIL import Image

def foo(img: Image) -> None:
	...

and got Module "PIL.Image" is not valid as a typemypy(error).

What did you expect to happen?

functions such as Image.open are classmethod of class Image.

What actually happened?

Image was actually an module, so for typing you need to type Image.Image, such as

from PIL import Image

def foo(img: Image.Image) -> None:
	...

What are your OS, Python and Pillow versions?

  • OS: Ubuntu 22.04
  • Python: Python 3.11.0rc2
  • Pillow: Name: Pillow-SIMD 9.0.0.post1
@Yay295
Copy link
Contributor

Yay295 commented Oct 23, 2022

related #6614

@radarhere radarhere changed the title typing difficulty because Image is a module and also a class Typing difficulty because Image is a module and also a class Oct 23, 2022
@radarhere
Copy link
Member

While this might have been an unfortunate choice from days long ago, is there really any way to improve this situation without breaking almost all code that currently uses Pillow?

@madeddy
Copy link

madeddy commented Nov 27, 2022

  • Rename one of them
  • Alias the old to the new name
  • Deprecate it timely
  • Output a obtrusive deprecation warning

I don't think anybody can do much more with such a double bind. But something should be done.

@radarhere
Copy link
Member

I've created PR #7639 as a possible solution to this, adding ImageType as an alias for Image.Image.

This is not intended to in any way mark Image.Image for future removal though. It is just an addition to allow for simpler type hinting.

@radarhere
Copy link
Member

Further thought has decided against #7639.

Image.Image vs ImageType is adding abstraction on top of something which isn't particularly complex in the first place, only save two chars, at the cost of another layer of indirection.

Internal discussion has agreed that we shouldn't make a breaking change, for the sake of backwards compatibility, so there is nothing to be done here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants