Skip to content

Fix several static condition reachability bugs - #11929

Open
crypt0lith wants to merge 6 commits into
microsoft:mainfrom
crypt0lith:fix/reversed-static-comparison
Open

crypt0lith wants to merge 6 commits into
microsoft:mainfrom
crypt0lith:fix/reversed-static-comparison

Conversation

@crypt0lith

@crypt0lith crypt0lith commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

  1. Both branches of a reversed check are analyzed as reachable, so diagnostics in the branch that can't run are reported. Besides sys.version_info, this affects sys.version_info[0], sys.platform, os.name and defined constants.
  2. A sys.platform, os.name or defined constant comparison with a bytes literal or an f-string can be evaluated with the wrong result, so the branch that runs is analyzed as unreachable and isn't checked.
  3. A sys.version_info, sys.platform, os.name or typing.TYPE_CHECKING check on a parameter or variable that shadows the module is evaluated as if it were the module, so a branch that can run is analyzed as unreachable. os.name is also evaluated without an import of os.
  4. sys.version_info compares equal to a tuple of the configured major and minor version, so >, <=, == and != comparisons with that tuple are evaluated with the wrong result. A comparison with a tuple that includes a micro version is evaluated even though no micro version is configured.

Root cause

  1. _evaluateStaticBoolOrBoolLikeExpression matches each of these special cases only with the expression as the left operand and the tuple, number or string literal as the right operand. When the operands are reversed, no case matches and the comparison isn't evaluated statically.
  2. The sys.platform, os.name and defined constant cases compare the joined values of the string literal's parts as a str, without checking whether the literal is bytes or an f-string. A bytes literal's value is its text, so it compares equal to a matching str, and an f-string's value is empty.
  3. The sys and typing cases match the name before the dot against the import aliases the binder collects for the whole file, without looking the name up in the scope, and the os.name case matches any name os. Callers outside the binder pass no aliases, so their sys cases match any name sys.
  4. The sys.version_info case converts the tuple to a PythonVersion and compares it with the configured version. The PythonVersion comparisons treat two versions as equal once either lacks a micro version.

Fix

  1. When the left operand of a comparison is a tuple, number or string literal and the right operand isn't, swap the operands and mirror the operator before matching the special cases.
    Fixes Reversed sys.version_info comparison (tuple on the left) is not evaluated as a static condition. #11927
  2. Evaluate == and != between sys.platform, os.name or a defined constant and a bytes literal as unequal, and don't evaluate a comparison with an f-string statically.
  3. Evaluate the sys, os and typing cases only when every declaration of the name in the current scope is an import of that module.
    Fixes Static conditional evaluation treats a parameter named sys or os as the module #12033
  4. Compare sys.version_info with the tuple elementwise as a five-element tuple, and don't evaluate the comparison statically when the result depends on a part of the version that isn't configured.
    Fixes sys.version_info compares equal to (major, minor) of pythonVersion #12034

Test

In staticExpression1.py, add the reversed form of each existing comparison, and sys.platform and os.name comparisons with a bytes literal and with an f-string. The f-string cases expect an error in the branch that is now reachable. Add >, <=, == and != comparisons of sys.version_info with (3, 8), and a > comparison with (3, 8, 1), which expects an error in the branch that is now reachable.

Cases added to staticExpression1.py
Case Expected Description
func1 error shadowed sys.version_info comparison, branch reachable
func2 error shadowed sys.version_info[0] comparison, branch reachable
func3 error shadowed sys.platform comparison, branch reachable
func4 error shadowed os.name comparison, branch reachable
func5 shadowed sys.platform comparison assigned to a variable is bool
func6 error shadowed typing.TYPE_CHECKING, branch reachable
Cases added to staticExpressionDefinedConstants1.py
Case Expected Description
bytes_equal_left error bytes == constant, else branch reachable
bytes_equal_right error constant == bytes, else branch reachable
bytes_not_equal_left error bytes != constant, if branch reachable
bytes_not_equal_right error constant != bytes, if branch reachable
bytes_member error bytes != constant member access, if branch reachable
formatted_equal_left error f-string == constant, both branches reachable
formatted_equal_right error constant == f-string, both branches reachable
formatted_not_equal_left error f-string != constant, both branches reachable
formatted_not_equal_right error constant != f-string, both branches reachable
formatted_constant error f-string with only literal text == constant, if branch reachable
formatted_concatenated error str concatenated with f-string == constant, both branches reachable
formatted_member error constant member access == f-string, both branches reachable
ordinary_equal matching str, else branch unreachable
ordinary_concatenated concatenated matching str, else branch unreachable
ordinary_unequal other str, if branch unreachable
expression_types error conditional expression keeps both types for f-string, one for bytes and matching str

Static condition evaluation recognizes sys.version_info,
sys.version_info[0], sys.platform, os.name and defined constants only
as the left operand of a comparison, so '(3, 14) <= sys.version_info'
is analyzed as a runtime check. Swap the operands and mirror the
operator when only the left operand is a literal.

Fixes microsoft#11927
@heejaechang

Heejae Chang (heejaechang) commented Oct 27, 2026 •

Copy link
Copy Markdown
Collaborator

🔴 Pyright CLI QA — a5f89873 — verdict: red

Automated CLI checks compared this change with its base revision.
25 focused check(s) completed.

Needs review: reportReturnType no longer reported

The base reports reportReturnType for this example; the PR does not. Is this change intended?

Reproduction and observed results

Create these files in an empty directory:

main.py

import sys
reveal_type(0)
def invalid() -> int:
    if (3, 13) < sys.version_info:
        return "reachable"
    return 0

pyrightconfig.json

{
  "include": [
    "main.py"
  ],
  "pythonVersion": "3.13",
  "pythonPlatform": "Linux",
  "typeCheckingMode": "basic"
}

From that directory, run each built Pyright revision:

node <pyright-cli-entrypoint> --outputjson -p .

Base 5e11909c674129533b67b11747b6dbd03c748ec3

{
  "exit_code": 1,
  "diagnostics": [
    {
      "file": "main.py",
      "severity": "error",
      "rule": "reportReturnType",
      "message": "Type \"Literal['reachable']\" is not assignable to return type \"int\"\n  \"Literal['reachable']\" is not assignable to \"int\"",
      "range": {
        "start": [
          4,
          15
        ],
        "end": [
          4,
          26
        ]
      }
    },
    {
      "file": "main.py",
      "severity": "information",
      "rule": null,
      "message": "Type of \"0\" is \"Literal[0]\"",
      "range": {
        "start": [
          1,
          12
        ],
        "end": [
          1,
          13
        ]
      }
    }
  ]
}

PR a5f89873634034c5ab4ef05c98cc0d95add12b29

{
  "exit_code": 0,
  "diagnostics": [
    {
      "file": "main.py",
      "severity": "information",
      "rule": null,
      "message": "Type of \"0\" is \"Literal[0]\"",
      "range": {
        "start": [
          1,
          12
        ],
        "end": [
          1,
          13
        ]
      }
    }
  ]
}

Diagnostic ranges use zero-based line and character positions.

Needs review: reportReturnType no longer reported

The base reports reportReturnType for this example; the PR does not. Is this change intended?

Reproduction and observed results

Create these files in an empty directory:

main.py

reveal_type(0)
MODE = "hi!"
def invalid() -> int:
    if b"hi!" != MODE:
        return "reachable"
    return 0

pyrightconfig.json

{
  "pythonVersion": "3.13",
  "pythonPlatform": "Linux",
  "typeCheckingMode": "basic",
  "defineConstant": {
    "MODE": "hi!"
  },
  "include": [
    "main.py"
  ]
}

From that directory, run each built Pyright revision:

node <pyright-cli-entrypoint> --outputjson -p .

Base 5e11909c674129533b67b11747b6dbd03c748ec3

{
  "exit_code": 1,
  "diagnostics": [
    {
      "file": "main.py",
      "severity": "error",
      "rule": "reportReturnType",
      "message": "Type \"Literal['reachable']\" is not assignable to return type \"int\"\n  \"Literal['reachable']\" is not assignable to \"int\"",
      "range": {
        "start": [
          4,
          15
        ],
        "end": [
          4,
          26
        ]
      }
    },
    {
      "file": "main.py",
      "severity": "information",
      "rule": null,
      "message": "Type of \"0\" is \"Literal[0]\"",
      "range": {
        "start": [
          0,
          12
        ],
        "end": [
          0,
          13
        ]
      }
    }
  ]
}

PR a5f89873634034c5ab4ef05c98cc0d95add12b29

{
  "exit_code": 0,
  "diagnostics": [
    {
      "file": "main.py",
      "severity": "information",
      "rule": null,
      "message": "Type of \"0\" is \"Literal[0]\"",
      "range": {
        "start": [
          0,
          12
        ],
        "end": [
          0,
          13
        ]
      }
    }
  ]
}

Diagnostic ranges use zero-based line and character positions.

Needs review: reportReturnType no longer reported

The base reports reportReturnType for this example; the PR does not. Is this change intended?

Reproduction and observed results

Create these files in an empty directory:

main.py

import sys
reveal_type(0)
class Environment:
    platform: str = "other"
def invalid(sys: Environment) -> int:
    if "linux" != sys.platform:
        return "reachable"
    return 0

pyrightconfig.json

{
  "pythonVersion": "3.13",
  "pythonPlatform": "Linux",
  "typeCheckingMode": "basic",
  "include": [
    "main.py"
  ]
}

From that directory, run each built Pyright revision:

node <pyright-cli-entrypoint> --outputjson -p .

Base 5e11909c674129533b67b11747b6dbd03c748ec3

{
  "exit_code": 1,
  "diagnostics": [
    {
      "file": "main.py",
      "severity": "error",
      "rule": "reportReturnType",
      "message": "Type \"Literal['reachable']\" is not assignable to return type \"int\"\n  \"Literal['reachable']\" is not assignable to \"int\"",
      "range": {
        "start": [
          6,
          15
        ],
        "end": [
          6,
          26
        ]
      }
    },
    {
      "file": "main.py",
      "severity": "information",
      "rule": null,
      "message": "Type of \"0\" is \"Literal[0]\"",
      "range": {
        "start": [
          1,
          12
        ],
        "end": [
          1,
          13
        ]
      }
    }
  ]
}

PR a5f89873634034c5ab4ef05c98cc0d95add12b29

{
  "exit_code": 0,
  "diagnostics": [
    {
      "file": "main.py",
      "severity": "information",
      "rule": null,
      "message": "Type of \"0\" is \"Literal[0]\"",
      "range": {
        "start": [
          1,
          12
        ],
        "end": [
          1,
          13
        ]
      }
    }
  ]
}

Diagnostic ranges use zero-based line and character positions.

@OKAN-SOFTWARE

Copy link
Copy Markdown

I reproduced #11927 with Pyright 1.1.414 and tested this PR's implementation locally.

I found two additional cases worth covering. _isStaticComparisonLiteral() currently treats every StringList as a static string literal, including formatted strings and byte strings. This can cause reachable branches to be incorrectly excluded from analysis, suppressing legitimate reportAssignmentType diagnostics.

Examples:

import sys

def check(text: str) -> None:
    if f"{text}" == sys.platform:
        bad: int = "bad"

    if b"linux" != sys.platform:
        bad2: int = "bad"

I reproduced both regressions against the PR implementation and prepared a focused correction that accepts only ordinary string literals, excluding formatted strings and byte strings.

Validation:

  • Regression tests failed before the correction.
  • All 184 tests in typeEvaluator8.test.ts passed afterward.
  • TypeScript build, ESLint, and Prettier checks passed.

I have a small three-file patch containing the correction and regression tests that I can share if helpful.

@crypt0lith crypt0lith changed the title Evaluate static conditions with the literal operand on the left Mirror reversed static comparisons, check bytes and f-string literals Oct 31, 2026
@github-actions

Copy link
Copy Markdown
Contributor

According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@bschnurr

Copy link
Copy Markdown
Member

I found another false-unreachability case: the mirrored comparison path treats sys.platform, sys.version_info, or os.name as interpreter constants even when sys or os is a parameter shadowing the imported module. For example, def check(sys): if "linux" != sys.platform: return "reachable" can suppress the reachable reportReturnType diagnostic. Could these static recognizers verify that the names resolve to the imported modules before folding? (The f-string case is already noted in the existing review feedback.)

🤖 AI-assisted review posted on behalf of Bill Schnurr (@bschnurr)

@crypt0lith

crypt0lith commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor Author

I found another false-unreachability case: the mirrored comparison path treats sys.platform, sys.version_info, or os.name as interpreter constants even when sys or os is a parameter shadowing the imported module.

Bill Schnurr (@bschnurr) This PR does not necessarily change anything about downstream evaluation of constants, it just makes the binary ops behave the same regardless of their handedness. Which is to say, the false-unreachability cases that you and the QA bot have flagged are pre-existing on the main branch for their mirrored counterparts.

For the sake of the bug tracker, I opened #12033 and #12034 so that I'm not smuggling in unrelated fixes with this PR.

Static condition evaluation recognizes sys.version_info, sys.platform,
os.name and typing.TYPE_CHECKING from the spelling of the name before
the dot, so a parameter that shadows the module is evaluated as the
module, and os.name is evaluated without an import of os. Evaluate
them only when every declaration of that name in scope is an import
of the module.

Fixes microsoft#12033
Static condition evaluation compares sys.version_info with a tuple
through PythonVersion, which treats the two as equal when either
lacks a micro version, so sys.version_info compares equal to a tuple
of the configured major and minor version. Compare them elementwise
as tuples, and don't evaluate a comparison that depends on a part of
the version that isn't configured.

Fixes microsoft#12034
@crypt0lith crypt0lith changed the title Mirror reversed static comparisons, check bytes and f-string literals Fix several static condition reachability bugs Nov 18, 2026
@bschnurr

Copy link
Copy Markdown
Member

Local review: 520aad6c integrated with ff00b031.

The three older QA cases now preserve their reportReturnType diagnostics: reversed version_info, a bytes/string comparison, and shadowed sys. The shadowed-os ternary also retains both Literal[1, "other"] alternatives, rather than only the else branch as on main. All three focused static-expression tests passed.

@rchiodo Rich Chiodo (rchiodo) left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The status saying the three older QA diagnostics are preserved differs from what I see for the existing configured-constant bytes example. I can't tell why the results differ.

With Python target 3.13, Linux, basic mode and defineConstant: {'MODE': 'hi!'}:

MODE = 'hi!'

def invalid() -> int:
    if b'hi!' != MODE:
        return 'reachable'
    return 0

The original base reports reportReturnType at the reachable string return. Head 520aad6c and a clean synthetic merge of that head with main ff00b031 both miss it. The comparison is true at runtime.

Replacing the condition with f"{'hi!'}" == MODE also leaves a reachable return undiagnosed on this defineConstant path. The current head does fix the older version-tuple and shadowed-sys witnesses.

Could defined-constant folding handle bytes and f-strings correctly, or leave the branches reachable when it can't, with tests for both?

Handle byte literals and formatted strings consistently when evaluating reachability, preserving reachable diagnostics and inferred alternatives.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@bschnurr

Copy link
Copy Markdown
Member

Pushed a2ea6f944 to address the configured-constant reachability cases. It reuses the existing string-comparison handling: bytes compare unequal to a configured str, and f-strings are not folded as literal text.

With Python 3.13, Linux, basic mode, and MODE="hi!", the two reported reportReturnType diagnostics were present on pre-PR base 0be51def, missing on head 520aad6c, and present again on a2ea6f944 (and its integration with main 2981f9f1). The new tests also cover both operand orders and operators, configured member access, concatenated literals, and ordinary-string controls.

For a conditional expression with a dynamic f-string, the original head inferred Literal[1] and missed an invalid int consumer; the repair retains Literal[1, 'fallback'] and reports reportArgumentType. The prior shadowed-name cases retain all three expected diagnostics.

Validation:

  • Core TypeScript build, focused ESLint, and Prettier checks passed.
  • jest 'typeEvaluator[1-8]\.test|checker\.test' --runInBand --forceExit: 9 suites, 1,536 tests passed. The additional type-consumer assertions were added afterward, and the focused static-expression tests were rerun.
  • jest typeEvaluator8.test --runInBand --forceExit -t StaticExpression: 4 tests passed on the repaired branch and on the current-main integration; both checkouts built successfully.

The local dependency versions satisfy the manifests but were not installed from a frozen lockfile. No representative performance benchmark was run; ordinary configured-string comparisons perform two additional linear scans of their literal segments.

🤖 AI-assisted validation

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

5 participants