-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Inconsistent behavior between pylance and pyright #9702
Comments
Pylance is built on top of pyright, so if you use the same version and configure them the same, they will output the same results. For a list of possible reasons why you're seeing a difference, refer to this documentation. If you can't figure it out from the documentation, feel free to respond back here, and I'll try to help you out further. I'm going to close in the meantime because this isn't an actionable pyright bug. |
to I am still observing the divergent behavior. I have updated the example repo with the stubs and the config, could you try it out? |
Both pyright and pylance report the same error for me with the code snippet above. I'm using the latest version of pylance (2024.12.1) and the latest version of pyright (1.1.392). I'm using the same virtual environment in both cases. Are you seeing any typing-related errors from pylance? For example, does the following code produce an error? x: int = "" How about basic syntax errors? 1 + |
Thanks for trying it out @erictraut ! A colleague of mine identified the root cause. It is because Pylance uses the version of python specified in given venv, while Pyright requires specifying the version: [tool.pyright]
pythonVersion = "3.11" I would suggest:
Meta point (but I think many have articulated this before): Having the typechecker run consistently between IDE and CI is a table stakes functionality. And this requires consistent CLI and IDE behavior. All efforts towards this goal will greatly benefit teams like ours. Thanks for the great support! |
I'm glad you figured it out.
That's not quite correct. Pyright and pylance both allow you to specify the Python version, but both will also auto-detect the version from the configured virtual environment. In the language server version of pyright (or in pylance, which is also a language server), you can configure the selected virtual environment in your editor. In the command-line version of pyright, you can simply activate the venv prior to running pyright. For more details, refer to this documentation. |
@erictraut The behavior I'm observing does not match the docs:
From the same shell,
But the linked documentation talks about environments, not Python versions, so it's not clear whether this is a bug in Pyright or the intended behavior. |
The You ran Can you provide your import sys, json
json.dump(tuple(sys.version_info), sys.stdout) |
The [tool.poetry]
name = "example-pyright-upath"
version = "0.1.0"
description = ""
authors = ["Your Name <you@example.com>"]
readme = "README.md"
[tool.poetry.dependencies]
python = "^3.11"
universal-pathlib = "0.1.4"
pyright = "^1.1.392.post0"
[build-system]
requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"
[tool.pyright]
autoSearchPaths=false
extraPaths=[] # Include paths from PYTHONPATH env var and .env definition
typeCheckingMode="standard"
reportShadowedImports="warning"
stubPath=".stubs" The observed behavior was reproduced following the instructions I gave by other people in our company. I have also tried: node -e "require('child_process').exec('python3 -c \"import sys; print(sys.version_info)\"', (e, so, se) => { if (e) console.error(e); if (se) console.error(se); console.log(so); })" getting:
I have also tried using the brew version of pyright directly, instead of the pip wrapper:
getting:
How is Pyright "shell executing"? |
Pyright has a function called getPythonVersion that calls the |
@erictraut I added console.logs to the running Pyright JS. In my case the code doesn't invoke pyright/packages/pyright-internal/src/analyzer/service.ts Lines 861 to 863 in 3089c82
because Does this correspond to the docs Isn't this a bug? Or should my case not go through |
Thanks for investigating further. This does indeed look like a bug. Reopening. |
@rchiodo, this looks like a regression that was introduced by a change you made several months ago. Do you happen to recall the context of this change? In particular, why is // Ensure default python version and platform. A default should only be picked if
// there is a python path however.
if (configOptions.pythonPath) {
configOptions.ensureDefaultPythonVersion(host, this._console);
} |
Is it an accidental copy-paste? In the same file the |
Sorry I don't recall. I put that comment there for a reason though. It wasn't there in the code that was copied. |
@rchiodo, is it OK if I assign you this issue? I think you have more context than I do. |
Sure. I can reproduce the issue with 1.1.378, but not with 1.1.377. I'll see if I can figure out why I left that if statement there. |
The change was intended to fix this issue here: If I'm recalling correctly that bug was because we were setting the default version before we actually had gotten the pythonPath so it was always defaulting to the I think moving the setting of the version after everything else should have been sufficient to fix that bug. I'll remove the if statement and make sure that original issue is still fixed. |
Describe the bug
Pyright CLI is reporting a type error, Pylance does not (even reporting "unused type: ignore" if that rule is enabled).
Code
This is from
universal-pathlib = "0.1.4"
Pyright reports
VS Code extension or command-line
Pylance: 2024.12.1
Pyright: 1.1.389
Repro
Assuming you have
poetry
and Python 3.11 installed:The text was updated successfully, but these errors were encountered: