Proposal to Improve Support for Other Python Platforms in the Typing Specification

To get a sense of what features are supported, I (well, Codex) wrote a set of scripts to compare across mypy, pyright, pyrefly, and ty.

This is for sys.platform.

Feature mypy pyright ty pyrefly
p = sys.platform :cross_mark: :cross_mark: :white_check_mark: :cross_mark:
TARGETS tuple :cross_mark: :cross_mark: :cross_mark: :cross_mark:
or :white_check_mark: :white_check_mark: :white_check_mark: :white_check_mark:
== :white_check_mark: :white_check_mark: :white_check_mark: :white_check_mark:
from sys import platform :cross_mark: :cross_mark: :white_check_mark: :cross_mark:
import sys as other_name :cross_mark: :white_check_mark: :white_check_mark: :cross_mark:
in (list) :cross_mark: :cross_mark: :cross_mark: :cross_mark:
in (set) :cross_mark: :cross_mark: :cross_mark: :cross_mark:
in (tuple) :cross_mark: :cross_mark: :cross_mark: :cross_mark:
!= :white_check_mark: :white_check_mark: :white_check_mark: :white_check_mark:
startswith :white_check_mark: :cross_mark: :white_check_mark: :white_check_mark:
not (==) :white_check_mark: :white_check_mark: :white_check_mark: :white_check_mark:
not in (list) :cross_mark: :cross_mark: :cross_mark: :cross_mark:
not in (set) :cross_mark: :cross_mark: :cross_mark: :cross_mark:
not in (tuple) :cross_mark: :cross_mark: :cross_mark: :cross_mark:
reverse == :cross_mark: :cross_mark: :white_check_mark: :white_check_mark:
reverse != :cross_mark: :cross_mark: :white_check_mark: :white_check_mark:

I published a repo for this at GitHub - JelleZijlstra/typechecker-matrix; check typechecker-matrix/samples/sys-platform at main · JelleZijlstra/typechecker-matrix · GitHub for more details on the features tested here.

In summary, the only universally supported checks are of the style sys.platform == "foo", !=, not (sys.platform == "foo"), and or of those. Surprisingly (to me), all type checkers but pyright supports startswith checks like if sys.platform.startswith("darwin"). No type checker currently supports sys.platform in (...).


In the spec, we should at a minimum require support for all the universally supported patterns. I’m open to adding support for more patterns. startswith makes some sense because on some OSes a version is appended to the name (see the docs: sys — System-specific parameters and functions — Python 3.14.3 documentation), and is already supported by most type checkers.

@Jos_Verlinde is asking for support for in. I’m open to that, but I’m surprised no type checker has added support for this pattern yet; that suggests it’s not commonly needed.

I’ll do a similar exercise for sys.version_info next.

6 Likes