Repository navigation
Fix several static condition reachability bugs - #11929
crypt0lith wants to merge 6 commits into
Conversation
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
🔴 Pyright CLI QA —
|
|
I reproduced #11927 with Pyright 1.1.414 and tested this PR's implementation locally. I found two additional cases worth covering. 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:
I have a small three-file patch containing the correction and regression tests that I can share if helpful. |
|
According to mypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅ |
|
I found another false-unreachability case: the mirrored comparison path treats 🤖 AI-assisted review posted on behalf of Bill Schnurr (@bschnurr) |
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 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
|
Local review: The three older QA cases now preserve their |
Rich Chiodo (rchiodo)
left a comment
There was a problem hiding this comment.
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 0The 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>
|
Pushed With Python 3.13, Linux, basic mode, and For a conditional expression with a dynamic f-string, the original head inferred Validation:
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 |
Summary
sys.version_info, this affectssys.version_info[0],sys.platform,os.nameand defined constants.sys.platform,os.nameor 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.sys.version_info,sys.platform,os.nameortyping.TYPE_CHECKINGcheck 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.nameis also evaluated without an import ofos.sys.version_infocompares 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
_evaluateStaticBoolOrBoolLikeExpressionmatches 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.sys.platform,os.nameand 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.sysandtypingcases 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 theos.namecase matches any nameos. Callers outside the binder pass no aliases, so theirsyscases match any namesys.sys.version_infocase converts the tuple to aPythonVersionand compares it with the configured version. ThePythonVersioncomparisons treat two versions as equal once either lacks a micro version.Fix
Fixes Reversed sys.version_info comparison (tuple on the left) is not evaluated as a static condition. #11927
==and!=betweensys.platform,os.nameor a defined constant and a bytes literal as unequal, and don't evaluate a comparison with an f-string statically.sys,osandtypingcases 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
sysorosas the module #12033sys.version_infowith 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_infocompares equal to(major, minor)ofpythonVersion#12034Test
In
staticExpression1.py, add the reversed form of each existing comparison, andsys.platformandos.namecomparisons 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 ofsys.version_infowith(3, 8), and a>comparison with(3, 8, 1), which expects an error in the branch that is now reachable.Cases added to
staticExpression1.pyfunc1sys.version_infocomparison, branch reachablefunc2sys.version_info[0]comparison, branch reachablefunc3sys.platformcomparison, branch reachablefunc4os.namecomparison, branch reachablefunc5sys.platformcomparison assigned to a variable isboolfunc6typing.TYPE_CHECKING, branch reachableCases added to
staticExpressionDefinedConstants1.pybytes_equal_left==constant, else branch reachablebytes_equal_right==bytes, else branch reachablebytes_not_equal_left!=constant, if branch reachablebytes_not_equal_right!=bytes, if branch reachablebytes_member!=constant member access, if branch reachableformatted_equal_left==constant, both branches reachableformatted_equal_right==f-string, both branches reachableformatted_not_equal_left!=constant, both branches reachableformatted_not_equal_right!=f-string, both branches reachableformatted_constant==constant, if branch reachableformatted_concatenated==constant, both branches reachableformatted_member==f-string, both branches reachableordinary_equalordinary_concatenatedordinary_unequalexpression_types