pyright icon indicating copy to clipboard operation
pyright copied to clipboard

pythonPlatform: "All" uses union semantics, so platform-specific APIs are accepted unconditionally

Open trim21 opened this issue 3 weeks ago • 1 comments

Summary

pythonPlatform: "All" is documented as "the target platform that will be used to execute the source code" with "All" as one of the allowed values, but its actual behavior is a union of all platforms' symbols. That contradicts the meaning of "this code may run on any platform": if the code may run on Linux, os.startfile() does not exist there, and calling it unconditionally should be an error.

I'd like to propose changing pythonPlatform to accept a list of platforms (string | string[]), which makes the intended semantics expressible and makes the analysis correct for each listed platform.

Current behavior

pythonPlatform: "All" makes _getExpectedPlatformNameFromPlatform return undefined (packages/pyright-internal/src/analyzer/staticExpressions.ts), so platform comparisons are never statically evaluated, both branches of typeshed's platform conditionals are analyzed, and the resulting symbol set is the union of all platforms.

Minimal repro:

import os
import sys

os.startfile("windows-only")   # does not exist on Linux
sys.getwindowsversion()        # does not exist on Linux
os.getuid()                    # does not exist on Windows
pythonPlatform Diagnostics
"All" none
"Linux" os.startfile, sys.getwindowsversion
"Windows" os.getuid

So with "All" — the setting that means "may run anywhere" — code that only works on one platform is accepted unconditionally. This is the least safe of the three settings, which is backwards.

This union behavior is intentional in the implementation; see the maintainer comment in #3523:

when you set "pythonPlatform" to "All", both declarations are now visible to the type evaluator. When multiple declarations are present, the type is evaluated as the union of both types.

Proposal

Accept a list of target platforms:

[tool.pyright]
pythonPlatform = ["Windows", "Linux"]

Semantics: the code must be valid on every listed platform. "All" remains valid as a shorthand for the full list.

This splits into two independent steps, and the first one already fixes the reported problem:

Step 1 — symbol availability is the intersection of the listed platforms. A symbol that exists on only some of the listed platforms is not unconditionally available, so os.startfile() and os.getuid() above become errors under ["Windows", "Linux"]. This is the semantics "may run on any of these platforms" and needs no per-branch platform assumption.

Step 2 (optional, larger) — per-branch platform assumption. A condition that is statically known on some listed platforms narrows the platform assumption for the branch, so the guarded form is still accepted:

if sys.platform == "win32":
    os.startfile("x")     # OK: only reachable on Windows

This is what users actually want from platform checks, and it is neither union nor intersection: each branch is analyzed under the platform assumption implied by the condition. It requires the type evaluator to resolve typeshed conditionals per assumed platform rather than per symbol, which is a significant change — hence proposing it separately from step 1.

Notes

  • Backwards compatibility: a single string keeps its current meaning, "All" keeps working as the full list. Only the analysis of "All" changes (to the safer intersection), which is the point of the issue.
  • mypy has a single --platform and no equivalent setting, so there is no cross-checker precedent to follow here.
  • Related: #3523 (union of duplicate declarations under "All"), #621 (assert sys.platform == ... and reachability).

trim21 avatar Nov 10 '26 11:11 trim21

Step 1 rejects the guarded form. Under "All", pyright doesn't evaluate sys.platform == "win32" statically, so the guard doesn't narrow the platform. With intersection semantics, os.startfile would also be unavailable inside the guard:

if sys.platform == "win32":
    os.startfile("x")  # error under step 1

On its own, step 1 reports errors on code that handles platform differences correctly. The only code it newly rejects for a good reason is an unguarded call, which a project targeting several platforms shouldn't contain. So step 2 isn't optional: step 1 can't ship without it. With step 2, pythonPlatform would no longer choose which platform conditions evaluate statically. It would become a per-branch analysis over a set of platforms, which needs typeshed declarations resolved per platform throughout the evaluator.

To check code against several platforms today, run pyright once per platform with --pythonplatform.

crypt0lith avatar Nov 10 '26 20:11 crypt0lith