microsoft / pylance-release

Documentation and issues for Pylance
Creative Commons Attribution 4.0 International
1.72k stars 766 forks source link

pillow has no code complements #4168

Closed Byxs20 closed 1 year ago

Byxs20 commented 1 year ago

你好,很抱歉打扰到您,我想要问一个问题。

我这边使用 Image.open() 或者 Image.new 打开图像都不能自动识别到类型,识别不到类型就没有对应的智能补全了。

code --version: 1.72.2 d045a5eda657f4d7b676dedbfa7aab8207f8a075 x64 Python version: 我从3.8.8换到3.8.10再换到3.9.13 Pillow version: 9.4.0 vscode-Python version: v2022.16.1 vscode-Pylance version: v2023.1.10

image

bschnurr commented 1 year ago

Sorry I couldn't repo. please update to the latest Pylance v2023.3.40 and try again

Byxs20 commented 1 year ago

image

code version: 1.77.0 7f329fe6c66b0f86ae1574c2911b681ad5a45d63 x64

vscode-Python: v2023.6.0 vscode-Pylance: v2023.3.40 python version: 3.8.10

It's still the same as before. May I ask you what else I can do to find the core of the problem?

rchiodo commented 1 year ago

Sorry that it's still broken.

This is what I get: image

The logs as described here would help us diagnose the problem: https://github.com/microsoft/pylance-release/blob/main/TROUBLESHOOTING.md#filing-an-issue

rchiodo commented 1 year ago

Additionally, what happens when you use 'Goto definition' on the PIL import? Where does it go?

It should go one of two places:

If you go somewhere different, that might explain why it doesn't find the Image type.

Byxs20 commented 1 year ago

This is what happened when I opened my previous project: [Info - 01:08:49] (16480) Pylance language server 2023.3.40 (pyright 04471971) starting [Info - 01:08:49] (16480) Server root directory: c:\Users\97766.vscode\extensions\ms-python.vscode-pylance-2023.3.40\dist [Info - 01:08:49] (16480) Starting service instance "5.贪吃蛇的秘密2.0" [Info - 01:08:49] (16480) Auto-indent enabled [Info - 01:08:49] (16480) No configuration file found. [Info - 01:08:49] (16480) No pyproject.toml file found. [Info - 01:08:49] (16480) Setting pythonPath for service "5.贪吃蛇的秘密2.0": "D:\Python\python.exe" [Warn - 01:08:49] (16480) stubPath f:\CTF题目\ctfshow_大牛杯\5.贪吃蛇的秘密2.0\typings is not a valid directory. [Info - 01:08:49] (16480) Assuming Python version 3.8 [Info - 01:08:49] (16480) Assuming Python platform Windows [Info - 01:08:49] (16480) Searching for source files [Info - 01:08:49] (16480) Found 1 source file [Info - 01:08:49] (16480) Background analysis(1) root directory: c:\Users\97766.vscode\extensions\ms-python.vscode-pylance-2023.3.40\dist [Info - 01:08:49] (16480) Background analysis(1) started [Info - 01:08:49] (16480) Indexer background runner(2) root directory: c:\Users\97766.vscode\extensions\ms-python.vscode-pylance-2023.3.40\dist (index) [Info - 01:08:49] (16480) Indexing(2) started [Info - 01:08:50] (16480) scanned(2) 323 files over 1 exec env [Info - 01:08:52] (16480) [IDX(2)] Long operation: index execution environment f:\CTF题目\ctfshow_大牛杯\5.贪吃蛇的秘密2.0 (2670ms) [Info - 01:08:52] (16480) [IDX(2)] Long operation: index packages f:\CTF题目\ctfshow_大牛杯\5.贪吃蛇的秘密2.0 (2675ms) [Info - 01:08:52] (16480) indexed(2) 229 files over 1 exec env [Info - 01:08:52] (16480) Indexing finished(2).

But I just created a new demo folder on my desktop and then opened the folder with Vscode, and there was no problem

[Info - 01:08:14] (10976) Pylance language server 2023.3.40 (pyright 04471971) starting [Info - 01:08:14] (10976) Server root directory: c:\Users\97766.vscode\extensions\ms-python.vscode-pylance-2023.3.40\dist [Info - 01:08:14] (10976) Starting service instance "demo" [Info - 01:08:14] (10976) Auto-indent enabled [Info - 01:08:14] (10976) No configuration file found. [Info - 01:08:14] (10976) No pyproject.toml file found. [Info - 01:08:14] (10976) Setting pythonPath for service "demo": "D:\Python\python.exe" [Warn - 01:08:14] (10976) stubPath c:\Users\97766\Desktop\demo\typings is not a valid directory. [Info - 01:08:14] (10976) Assuming Python version 3.8 [Info - 01:08:14] (10976) Assuming Python platform Windows [Info - 01:08:14] (10976) Searching for source files [Info - 01:08:14] (10976) Found 1 source file [Info - 01:08:15] (10976) Background analysis(1) root directory: c:\Users\97766.vscode\extensions\ms-python.vscode-pylance-2023.3.40\dist [Info - 01:08:15] (10976) Background analysis(1) started [Info - 01:08:15] (10976) Indexer background runner(2) root directory: c:\Users\97766.vscode\extensions\ms-python.vscode-pylance-2023.3.40\dist (index) [Info - 01:08:15] (10976) Indexing(2) started [Info - 01:08:15] (10976) scanned(2) 323 files over 1 exec env [Info - 01:08:18] (10976) [IDX(2)] Long operation: index execution environment c:\Users\97766\Desktop\demo (2689ms) [Info - 01:08:18] (10976) [IDX(2)] Long operation: index packages c:\Users\97766\Desktop\demo (2694ms) [Info - 01:08:18] (10976) indexed(2) 229 files over 1 exec env [Info - 01:08:18] (10976) Indexing finished(2).

I'm not quite sure why a new project is fine, Can you tell me why this is happening? Thanks!

rchiodo commented 1 year ago

Might be the folder name. Your old folder name has non ascii characters in it. Let me try that myself.

rchiodo commented 1 year ago

No that works for me.

What happens when you goto definition on PIL in the old project?

Byxs20 commented 1 year ago

In the old project: I held down ctrl + left mouse button to go to the following directory: D:\Python\Lib\site-packages\PIL\Image.py

Pressing and holding PIL will go to D:\Python\Lib\site-packages\PIL__init__.py

But in my new project it also jumps to the same py file as in the old project

rchiodo commented 1 year ago

In the image.py file, what does the definition of open look like? You're using python 3.8, so maybe the problem is it doesn't match the bits here:

<user root>\.vscode\extensions\ms-python.vscode-pylance-2023.3.40\dist\typeshed-fallback\stubs\Pillow\PIL\__init__.pyi

rchiodo commented 1 year ago

For me (I'm using 3.10), open looks like this:

def open(fp, mode="r", formats=None):

And the .pyi file (where we get the type from) looks like this:

def open(
    fp: str | bytes | Path | SupportsRead[bytes], mode: Literal["r"] = ..., formats: list[str] | tuple[str, ...] | None = ...
) -> Image: ...

So the definition of the real function matches the definition in the pyi, so we can tell it's a match.

Byxs20 commented 1 year ago

C:\Users\97766.vscode\extensions\ms-python.vscode-pylance-2023.3.40\dist\typeshed-fallback\stubs\Pillow\PIL__init__.pyi:

image

D:\Python\Lib\site-packages\PIL\Image.py:

def open(fp, mode="r", formats=None): ...

rchiodo commented 1 year ago

Your __init__.pyi only has 3 lines in it?

Oh no wait that's normal.

The image.pyi is the one that has the definition of open in it.

Byxs20 commented 1 year ago

Sometimes it works for me, sometimes it doesn't prompt intelligently, I can create a new project, but not my old project before, I'm not sure why

def open( fp: str | bytes | Path | SupportsRead[bytes], mode: Literal["r"] = ..., formats: list[str] | tuple[str, ...] | None = ... ) -> Image: ...

rchiodo commented 1 year ago

Can you set this setting to "trace" and try again? The log may show something else. It might be the old project has too many files and we're bailing out early:

"python.analysis.logLevel": "Trace"
Byxs20 commented 1 year ago

This is the log message I got from the python server after I set it up:

info.txt

rchiodo commented 1 year ago

And it's not working after that runs? Looks like it indexed a lot of stuff but should have finished after 2 seconds or so. It should be working after the 2 seconds are up.

Byxs20 commented 1 year ago

But he just can't predict that the type of IMG is Image. Image, and then there is no key API completion.

rchiodo commented 1 year ago

I think we'd need to repro locally to figure out what the problem is at this point.

Is there anything else that you have different between the two projects besides the name of the folder?

Byxs20 commented 1 year ago

I'll see if I can find some time to study it tomorrow. In fact, I don't know why sometimes I can and sometimes I can't. Some time ago I considered changing the IDE tool.

Byxs20 commented 1 year ago

image image