Closed fireattack closed 11 months ago
This behavior is intended. Type checkers in Python are required (by PEP 484) to honor a "python version" and not analyze code that is intended for older versions of Python. The community-maintained typeshed stub libraries that describe stdlib functions rely on this behavior.
By default, pylance uses the version of your configured Python interpreter for the "python version" (which, for you, is 3.11 based on your logs). If you would like to override this, you can do so by specifying a different pythonVersion
in a pyrightconfig.json or pyproject.toml file. For details, refer to the pyright documentation.
Thanks for your prompt reply.
Just to make it clear: by "not analyze code that is intended for older versions of Python", do you mean that the part in my example is greyed out exactly because it's not reachable (due to version check if conditional above) since I'm running py3.11?
If so, then that's indeed very nice and I don't even know it can be this advanced!
Hi @fireattack, your understanding is correct. Static type checkers like Pyright are expected to understand sys.version_info
checks as per PEP 484 and ignore code that is unreachable based on these checks.
Environment data
Code Snippet
Repro Steps
Copy in the above code.
Expected behavior
"False" is reachable and should not be greyed out.
Actual behavior
Logs