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

Pylance thinks an attribute is unknown despite "Go to Type Definition" working. Pyright passes. winsdk #5202

Closed
Avasam opened this issue Dec 2, 2023 · 11 comments
Assignees
Labels
needs repro Issue has not been reproduced yet

Comments

@Avasam
Copy link

Avasam commented Dec 2, 2023

Environment data

  • Language Server version: Pylance v2023.11.102
  • OS and version: Windows Version 10.0.19045 Build 19045
  • Python version (& distribution if applicable, e.g. Anaconda): Python 3.11.6

Code Snippet

Here's my code snippet, without my own comments, and I added Pylance reported errors as comments

import asyncio

from winsdk.windows.ai.machinelearning import LearningModelDevice, LearningModelDeviceKind
from winsdk.windows.media.capture import MediaCapture


def get_direct3d_device():  # Return type is unknownPylancereportUnknownParameterType
    media_capture = MediaCapture()

    async def init_mediacapture():
        # "IAsyncAction" is not awaitablePylancereportGeneralTypeIssues
        await (media_capture.initialize_async() or asyncio.sleep(0))

    asyncio.run(init_mediacapture())
    # Type of "direct_3d_device" is "Unknown | None"PylancereportUnknownVariableType
    direct_3d_device = media_capture.media_capture_settings and media_capture.media_capture_settings.direct3_d11_device
    if not direct_3d_device:
        try:
            direct_3d_device = LearningModelDevice(LearningModelDeviceKind.DIRECT_X_HIGH_PERFORMANCE).direct3_d11_device
        except Exception:
            pass
    if not direct_3d_device:
        raise OSError("Unable to initialize a Direct3D Device.")
    return direct_3d_device  # Return type is unknownPylancereportUnknownVariableType

Repro Steps

  1. Create a venv
  2. python -m pip install winsdk==1.0.0b10
  3. Configure strict type-checking
  4. Paste above code
  5. See pylance raising errors about Unkown types. Hovering also shows partial unknowns
  6. Run pyright: pass (currently on 1.1.338, but dozens of previous versions also pass)

Expected behavior

Full type to be known by Pylance, not partially Unknown

Pyright: information: Type of "direct_3d_device" is "IDirect3DDevice | None"

Actual behavior

Partial Unknowns

Pylance:
image

Logs

Python Language Server log.txt

@github-actions github-actions bot added the needs repro Issue has not been reproduced yet label Dec 2, 2023
@Avasam

This comment was marked as resolved.

@erictraut
Copy link
Contributor

I'm not able to repro the problem above. I wonder if your Pylance install or your venv are corrupt. Are you able to repro the problem with a fresh project and fresh venv?

@Avasam
Copy link
Author

Avasam commented Dec 3, 2023

Fresh new project, new venv, as soon as I set typeCheckingMode to strict all the errors appear (actually the "IAsyncAction" is not awaitable error happens even in basic mode)

  1. python3.11 -m venv .venv
  2. reloaded VSCode to make sure everything picks up the newly created venv
  3. python -m pip install winsdk

image

I wonder if your Pylance install [is] corrupt.

Could be, in case it was a cache thing I found and ran "Pylance: Clear All Persisted Indices" + "Python: Clear Cache and Reload Window" commands. No change.
I have completely uninstalled then re-installed Pylance (v2023.11.10) and all of the errors except "IAsyncAction" is not awaitable are gone both in my existing project and the clean demo/test project. Switched back to pre-release (v2023.11.102), all the errors are back.


Worth noting I don't have the from time import time error from my second comment anymore though

@erictraut
Copy link
Contributor

Worth noting I don't have the from time import time error from my second comment though

That's the error I couldn't repro. I didn't try to repro the other issue because the winsdk library requires a Windows machine to install, and I'm using a Mac. I think the pylance team primarily uses Windows, so they should be able to confirm that repro.

@WorldlyCartos
Copy link

Having same issue with updating python extension yesterday. Switched to the pre-release hoping it would resolve the issue but it hasn't.

@debonte
Copy link
Contributor

debonte commented Dec 4, 2023

I'm unable to repro this on Windows with 2023.11.102. I don't get any diagnostics and the type of direct_3d_device seems correct:
image

@Avasam
Copy link
Author

Avasam commented Dec 4, 2023

I'm unable to repro this on Windows with 2023.11.102. I don't get any diagnostics and the type of direct_3d_device seems correct: image

Just wanna make sure, you are in strict mode right? Otherwise the "IAsyncAction" is not awaitable issue has been happening to me since quite a few versions back, in basic mode, and in current non-pre-release version. Just didn't have time to check if it was a pyright desync back then, pyrigth on my CI was (and is) still passing, and figured it might fix itself with an update by the time I have to work on that project again. But it got worse ^^"

Another possible variable:
My global default python version is 3.9, my venv is 3.11. In case that makes a difference. (but once again, the "IAsyncAction" is not awaitable happenned before I updated my project to 3.10+).

I might have to start bisecting versions of pylance and winsdk if you can't replicate easily.

@debonte
Copy link
Contributor

debonte commented Dec 4, 2023

Yes, I've set "python.analysis.typeCheckingMode": "strict".

I don't see any diagnostics when using the latest Pylance release version (2023.11.10) either.

@Avasam
Copy link
Author

Avasam commented Dec 4, 2023

Using the following minimal repro:

from winsdk.windows.media.capture import MediaCapture
async def foo():
    await MediaCapture().initialize_async() # "IAsyncAction" is not awaitable Pylance[reportGeneralTypeIssues]

(because as mentioned previously, the rest of the issues only happen in strict mode on pre-release, so maybe we can focus on this first)

I have bisected this diagnostic to show starting v2023.10.20. v2023.10.10 and a couple previous versions I tested don't show any diagnostic for the above example.
Which sounds a lot like #431 re-introduced.
image

I understand I may have two different issues here (the "not awaitable" from v2023.10.20 and the "partial Unknown" from pre-release). Although the symptoms come from the same library stubs, and both issues are pylance-specific.

@Avasam
Copy link
Author

Avasam commented Dec 4, 2023

Ok I found the difference in configs!
It's useLibraryCodeForTypes = false that I had configured in my VSCode settings but not in pyright configs.
Starting pyright 1.1.330 I get the same diagnostics with that config in my pyproject.toml

I believe I've previously kept this to false in my editor because

  1. Way back then the default differed between pyright and pylance (ie before pyright 1.1.303)
  2. I wanna know when I use an untyped library (so that I can add missing annotations myself), but still want type completion

So idk what changed in pyright 1.1.330, but I guess that for use with py.typed libraries that use a mix of stubs and regular inline annotations I should update my settings to go back to the default of useLibraryCodeForTypes (true) ? Still surprised this only affects one of the libraries I use.
reportMissingTypeStubs = "warning" is probably actually what I want.

@Avasam
Copy link
Author

Avasam commented Dec 4, 2023

I'll close this as "working as intended". My issue (ie: desync with pyright) boils down to updating an old config after a change in pyright I was not aware of. Thank you for your time.

Now that pyright and pylance agree on my side, if the behaviour seen was unexpected with useLibraryCodeForTypes = false, it would be on pyright's side. (reading the config description it seems to work as intended too)

@Avasam Avasam closed this as completed Dec 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs repro Issue has not been reproduced yet
Projects
None yet
Development

No branches or pull requests

4 participants