-
Notifications
You must be signed in to change notification settings - Fork 48
Closed
python/mypy
#10550Labels
bugpython compatMypyc doesn't match CPython or documented semantics.Mypyc doesn't match CPython or documented semantics.
Description
With the following code (using the Pillow library):
from PIL import Image
def test(im: Image):
print(im.mode)
def run() -> None:
im = Image.open('test.png')
test(im)and the following config file:
[mypy-PIL.*]
ignore_missing_imports = Truemypy shows no errors, but compiling the code with mypyc and trying to run it produces the following traceback:
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "bug.py", line 1, in <module>
from PIL import Image
AttributeError: module 'PIL' has no attribute 'Image'
The problem is that the type annotations in the original code are incorrect, because PIL.Image is a module, and PIL.Image.open is a function that returns a separate PIL.Image.Image object, so the correct type annotations are:
from PIL import Image
from PIL.Image import Image as ImageType
def test(im: ImageType):
print(im.mode)
def run() -> None:
im = Image.open('test.png')
test(im)which fixes the mypyc error.
There are a couple of possible solutions that I can think of to this:
- Get mypy to realize the error in the original code, even when
ignore_missing_importsis set, by noticing that a class is being passed in when a module is expected. - Same as (1), but only emit an error if compiling with mypyc.
- Ignore any type annotations from untyped libraries when compiling with mypyc.
- Insert code into the compiled C extension to check if this happens and print/raise with a nicer error message.
I'd like to work on this, but I don't really know which (if any) of the solutions I mentioned would be the best way to fix this.
Metadata
Metadata
Assignees
Labels
bugpython compatMypyc doesn't match CPython or documented semantics.Mypyc doesn't match CPython or documented semantics.