Skip to content

Commit d7fbb9e

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 d7fbb9e

File tree

1 file changed

+129
-6
lines changed

1 file changed

+129
-6
lines changed

docs/spec/directives.rst

Lines changed: 129 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,8 +145,27 @@ 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+
Type checkers should support narrowing based on:
149+
* ``sys.version_info``
150+
* ``sys.platform``
151+
* ``sys.implementation.version``
152+
* ``sys.implementation.name``
153+
154+
The `<comparison>` patterns for these variables are described in more detail in the following paragraphs.
155+
156+
sys.version_info checks
157+
^^^^^^^^^^^^^^^^^^^^^^^^
158+
159+
Type checkers should support the following ``sys.version_info`` comparison patterns:
160+
* ``sys.version_info <comparison> <2-tuple>``
161+
162+
`<comparison>` can be one of the following:
163+
* Greater or equal: ``>=``
164+
* Less than: ``<``
165+
166+
Comparisons checks are only supported against the first two elements of a tuple.
167+
168+
Example::
150169

151170
import sys
152171

@@ -155,13 +174,117 @@ checks, e.g.::
155174
else:
156175
# Python 3.11 and lower
157176

177+
178+
179+
sys.platform checks
180+
^^^^^^^^^^^^^^^^^^^
181+
182+
Type checkers should support ``sys.platform`` comparisons:
183+
184+
Supported patterns:
185+
* ``sys.platform <comparison> <string literal>``
186+
* ``sys.platform in <tuple of string literals>``
187+
188+
`<comparison>` can be one of the following:
189+
* Equality: ``==``
190+
* Inequality: ``!=``
191+
* Membership: ``in``
192+
193+
Example::
194+
195+
import sys
196+
158197
if sys.platform == 'win32':
159198
# Windows specific definitions
160-
else:
161-
# Posix specific definitions
162199

163-
Don't expect a checker to understand obfuscations like
164-
``"".join(reversed(sys.platform)) == "xunil"``.
200+
if sys.platform in ("linux", "darwin"):
201+
# Platform-specific stubs for Linux and macOS
202+
...
203+
204+
205+
.. note::
206+
207+
Type checkers are only required to support the above patterns.
208+
209+
For example, the pattern ``sys.platform == "linux"`` is supported but other syntax variants such as ``platform == "linux"`` and ``"win" not in sys.platform`` are not supported.
210+
The membership check ``sys.platform in ("linux", "darwin")`` only supports simple containment testing to a tuple of literal strings.
211+
212+
sys.implementation.name checks
213+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
214+
215+
Type checkers should support ``sys.implementation.name`` comparisons:
216+
217+
Supported patterns:
218+
* ``sys.implementation.name <comparison> <string literal>``
219+
* ``sys.implementation.name in <tuple of string literals>``
220+
221+
`<comparison>` can be one of the following:
222+
* Equality: ``==``
223+
* Inequality: ``!=``
224+
* Membership: ``in``
225+
226+
Common values: ``"cpython"``, ``"pypy"``, ``"micropython"``, ``"graalpy"``, ``"jython"``, ``"ironpython"``
227+
228+
Example::
229+
230+
import sys
231+
if sys.implementation.name == "cpython":
232+
# CPython-specific stub
233+
if sys.implementation.name == "micropython":
234+
# MicroPython-specific stub
235+
236+
237+
sys.implementation.version checks
238+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
239+
240+
Type checkers should support the following ``sys.implementation.version`` comparison patterns:
241+
* ``sys.implementation.version <comparison> <2-tuple>``
242+
243+
`<comparison>` can be one of the following:
244+
* Greater or equal: ``>=``
245+
* Less than: ``<``
246+
247+
Example::
248+
249+
import sys
250+
251+
if sys.implementation.name == "pypy" and sys.implementation.version >= (7, 3):
252+
# PyPy version 7.3 and above
253+
254+
if sys.implementation.name == "micropython" and sys.implementation.version >= (1, 24):
255+
# MicroPython version 1.24 and above
256+
257+
.. note::
258+
259+
Comparisons checks are only supported against the first two elements of a tuple.
260+
261+
``sys.implementation.version`` is a tuple, in the same format as sys.version_info. However it represents the version of the Python implementation rather than the version of the Python language.
262+
This has a distinct meaning from the specific version of the Python language to which the currently running interpreter conforms.
263+
264+
265+
Combining checks
266+
^^^^^^^^^^^^^^^^
267+
268+
Multiple comparisons can be combined with:
269+
* A ``not`` unary operator
270+
* An ``and`` or ``or`` binary operator
271+
272+
No support for complex expressions
273+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
274+
275+
Type checkers are not required to evaluate complex expressions involving these variables.
276+
Therefore do not expect a checker to understand obfuscations such as::
277+
278+
import sys
279+
if "".join(reversed(sys.platform)) == "xunil":
280+
# Linux specific code
281+
282+
283+
Configuration
284+
^^^^^^^^^^^^^
285+
286+
Type checkers should provide configuration to specify target version, platform, and implementation. The exact mechanism is implementation-defined.
287+
165288

166289
.. _`deprecated`:
167290

0 commit comments

Comments
 (0)