pyright icon indicating copy to clipboard operation
pyright copied to clipboard

`sys.version_info` compares equal to `(major, minor)` of `pythonVersion`

Open crypt0lith opened this issue 2 weeks ago • 1 comments

Describe the bug

Static conditional evaluation treats sys.version_info as equal to a tuple of the configured major and minor version. At runtime sys.version_info compares greater than that tuple, so >, <=, == and != comparisons with it are evaluated with the wrong result. The branch that can run is analyzed as unreachable, and the branch that can't run is analyzed as reachable.

Code or Screenshots

With pythonVersion 3.13:

import sys

if sys.version_info > (3, 13):
    reveal_type(">")

if sys.version_info <= (3, 13):
    reveal_type("<=")

if sys.version_info == (3, 13):
    reveal_type("==")

if sys.version_info != (3, 13):
    reveal_type("!=")
:7:17 - information: Type of ""<="" is "Literal['<=']"
:10:17 - information: Type of ""=="" is "Literal['==']"
0 errors, 0 warnings, 2 informations

VS Code extension or command-line

Command-line, pyright 1.1.414

crypt0lith avatar Nov 18 '26 01:11 crypt0lith

This also affects tuples with a micro version, which pythonVersion 3.13 doesn't specify:

import sys

if sys.version_info > (3, 13, 1):
    reveal_type(">")

if sys.version_info <= (3, 13, 1):
    reveal_type("<=")
:7:17 - information: Type of ""<="" is "Literal['<=']"
0 errors, 0 warnings, 1 information

crypt0lith avatar Nov 18 '26 01:11 crypt0lith