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

Assign mode to fromarray #5707

Closed
Erhie opened this issue Sep 9, 2021 · 5 comments
Closed

Assign mode to fromarray #5707

Erhie opened this issue Sep 9, 2021 · 5 comments

Comments

@Erhie
Copy link

Erhie commented Sep 9, 2021

What did you do?

origPil = Image.open( filename )
origPil_mode = origPil.mode
img_array = np.asarray(origPil)

img_to_show = Image.fromarray( img_array , mode = origPil_mode )
from PIL import ImageShow
ImageShow.show(img_to_show)

What did you expect to happen?

that there is no warning belonging mode in Image.fromarray( img_array , mode = origPil_mode )

What actually happened?

Warning:

Argument of type "str" cannot be assigned to parameter 
"mode" of type
"Literal['1', 'CMYK', 'F', 'HSV', 'I', 'l', "LAB', 'P', 'RGB', 'RGBA', 'RGBX', 'YCbCr'] | None"
in function "fromarray"
   Type "str" cannot be assigned to type 
"Literal['1', "CMYK', 'F', 'HSV', 'I', 'L', 'LAB', 'P', 'RGB', 'RGBA', ''RGBX', 'YCbCr'] | None"
  • OS: Windows 10
  • Python: Python 3.8
  • Pillow: 8.3.2
@radarhere
Copy link
Member

Hi. I'm not able to replicate the warning, so here are a few questions.

  • What version of numpy are you using? np.__version__.
  • I understand that you haven't shown us the import statements and where you are setting filename, but that's it? You're able to reproduce this problem with just this code block in it's own file, without other code that you're written?
  • Are you running this from Visual Studio or something like that, rather than from the command line? If you run it from the command line outside of Visual Studio, does the warning go away? I'm not trying to dismiss your problem, just wanting to better understand its origin.

@radarhere radarhere changed the title assign mode to fromarray Assign mode to fromarray Sep 9, 2021
@Erhie
Copy link
Author

Erhie commented Sep 10, 2021

I use VS Code with extension Pylance
and with further debugging I see there is a prototype for fromarray
def fromarray(obj, mode: _Mode | None = ...) -> Image: ...
and _Mode is defined as
_Mode = Literal["1", "CMYK", "F", "HSV", "I", "L", "LAB", "P", "RGB", "RGBA", "RGBX", "YCbCr"]
Has Pylance generated the file Image.pyi itself?

And numpy has version 1.20.3

@radarhere
Copy link
Member

As you've likely gathered, we don't provide typing for fromarray -

def fromarray(obj, mode=None):

According to microsoft/pylance-release#659, Pylance comes bundled with .pyi files for PIL, and microsoft/pylance-release#994 was an issue where a user reports a typo in a PIL stub.

However, looking at https://github.com/microsoft/pylance-release/blob/main/CHANGELOG.md, I see that on July 6,

The bundled PIL stubs have been removed, as they have been merged into typeshed's Pillow stubs.

So I think that the problem actually lies in https://github.com/python/typeshed - def fromarray(obj, mode: _Mode | None = ...) -> Image: ...

@radarhere
Copy link
Member

@Erhie is that enough information for you to take it from here?

@Erhie
Copy link
Author

Erhie commented Sep 11, 2021

Thank You!

Now I had success by uninstall and newly installed Pillow and
purge in ....vscode\extensions\ms-python.vscode-pylance-2021.9.1\dist\typeshed-fallback\stubs\Pillow
and now there is no warning for this issue.

For further interests I asked also:
microsoft/pylance-release#1798
and the next step is to ask
https://github.com/python/typeshed

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

No branches or pull requests

2 participants