Skip to content

Commit 368e820

Browse files
committed
Update specification for directives for sys.implementation and sys.platform checks.
Signed-off-by: Jos Verlinde <Jos.Verlinde@Microsoft.com>
1 parent f98d75e commit 368e820

File tree

1 file changed

+63
-5
lines changed

1 file changed

+63
-5
lines changed

docs/spec/directives.rst

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,13 @@ left undefined by the typing spec at this time.
145145
Version and platform checking
146146
-----------------------------
147147

148-
Type checkers are expected to understand simple version and platform
149-
checks, e.g.::
148+
149+
Type checkers should support narrowing based on ``sys.version_info``, ``sys.platform``, and ``sys.implementation.name`` checks.
150+
151+
sys.version_info checks
152+
^^^^^^^^^^^^^^^^^^^^^^^^
153+
154+
Type checkers should support ``sys.version_info`` comparisons::
150155

151156
import sys
152157

@@ -155,12 +160,65 @@ checks, e.g.::
155160
else:
156161
# Python 3.11 and lower
157162

163+
**Supported patterns:**
164+
165+
* Equality: ``sys.version_info == (3, 10)``
166+
* Inequality: ``sys.version_info != (3, 9)``
167+
* Comparison: ``sys.version_info >= (3, 10)``, ``sys.version_info < (3, 12)``
168+
* Tuple slicing: ``sys.version_info[:2] >= (3, 10)``
169+
* Element access: ``sys.version_info[0] >= 3``
170+
171+
sys.platform checks
172+
^^^^^^^^^^^^^^^^^^^
173+
174+
Type checkers should support ``sys.platform`` comparisons::
175+
176+
import sys
177+
158178
if sys.platform == 'win32':
159179
# Windows specific definitions
160-
else:
161-
# Posix specific definitions
162180

163-
Don't expect a checker to understand obfuscations like
181+
if sys.platform in ("linux", "darwin"):
182+
# Platform-specific stubs for Linux and macOS
183+
...
184+
185+
**Supported patterns:**
186+
187+
* Equality: ``sys.platform == "linux"``
188+
* Inequality: ``sys.platform != "win32"``
189+
* Membership: ``sys.platform in ("linux", "darwin")``
190+
* Negative membership: ``sys.platform not in ("win32", "cygwin")``
191+
192+
sys.implementation.name checks
193+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
194+
195+
Type checkers should support ``sys.implementation.name`` comparisons::
196+
197+
import sys
198+
if sys.implementation.name == "cpython":
199+
# CPython-specific stub
200+
if sys.implementation.name == "micropython":
201+
# MicroPython-specific stub
202+
203+
**Supported patterns:**
204+
205+
* Equality: ``sys.implementation.name == "cpython"``
206+
* Inequality: ``sys.implementation.name != "cpython"``
207+
* Membership: ``sys.implementation.name in ("pypy", "graalpy")``
208+
* Negative membership: ``sys.implementation.name not in ("cpython", "pypy")``
209+
210+
Common values: ``"cpython"``, ``"pypy"``, ``"micropython"``, ``"graalpy"``, ``"jython"``, ``"ironpython"``
211+
212+
Configuration
213+
^^^^^^^^^^^^^
214+
215+
Type checkers should provide configuration to specify target version, platform, and implementation. The exact mechanism is implementation-defined.
216+
217+
Complex Expressions
218+
^^^^^^^^^^^^^^^^^^^
219+
220+
Type checkers are not required to evaluate complex expressions involving these variables.
221+
Therefore do not expect a checker to understand obfuscations like:
164222
``"".join(reversed(sys.platform)) == "xunil"``.
165223

166224
.. _`deprecated`:

0 commit comments

Comments
 (0)