You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Common values: ``"cpython"``, ``"pypy"``, ``"micropython"``, ``"graalpy"``, ``"jython"``, ``"ironpython"``
227
232
233
+
228
234
.. code-block:: python
229
235
:caption: Example `sys.implementation.name`
230
236
:emphasize-lines: 2,4
@@ -239,12 +245,16 @@ Type checkers should support comparison patterns:
239
245
sys.implementation.version checks
240
246
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
241
247
248
+
``sys.implementation.version`` is a tuple, in the same format as sys.version_info. However it represents the version of the Python implementation
249
+
rather than the version of the Python language. This has a distinct meaning from the specific version of the Python language to which the currently
250
+
running interpreter conforms. For CPython this is the same as `sys.version_info`.
251
+
242
252
Type checkers should support the following comparison patterns:
243
253
* ``sys.implementation.version >= <2-tuple>``
244
254
* ``sys.implementation.version < <2-tuple>``
245
255
246
-
Comparisons checks are only supported against the first two elements of the implementation version tuple.
247
-
Use of named attributes is not mandated.
256
+
Comparison checks are only supported against the first two elements of the implementation version tuple.
257
+
Type checkers are not required to support comparisons against named attributes of `sys.implementation.version`.
248
258
249
259
.. code-block:: python
250
260
:caption: Example `sys.implementation.version`
@@ -256,39 +266,45 @@ Use of named attributes is not mandated.
256
266
if sys.implementation.name =="micropython"and sys.implementation.version >= (1, 24):
257
267
# MicroPython version 1.24 and above
258
268
259
-
.. note::
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. For CPython this is the same as `sys.version_info`.
263
-
264
269
265
270
No support for complex expressions
266
271
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
267
272
268
-
Type checkers are only required to support the above patterns, and are not required to evaluate complex expressions involving these variables.
269
-
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 mandated.
273
+
Type checkers are required to support the above patterns, and are not required to evaluate other comparisons or other syntax variants.
270
274
271
275
Therefore checkers are **not required** to understand obfuscations such as:
272
276
273
277
.. code-block:: python
274
278
:caption: Examples of unsupported or overly complex version/platform checks
275
-
:emphasize-lines: 3,5,7
279
+
:emphasize-lines: 4,6,8
276
280
277
281
import sys
278
282
from sys import platform
283
+
279
284
if"".join(reversed(sys.platform)) =="xunil":
280
-
#Linux specific code
285
+
#Typecheckers will not be required to understand this obfuscated check
281
286
if platform =="linux":
282
-
#Linux specific code
287
+
#Typecheckers will not be required to understand this import alias for sys.platform
283
288
if"win"notin sys.platform:
284
-
#Non-Windows specific code
289
+
#Typecheckers will not be required to understand this reversed membership check
285
290
286
291
287
292
Configuration
288
293
^^^^^^^^^^^^^
289
294
290
-
Type checkers should provide configuration or CLI options to specify target sys.version, sys.platform, sys.implementation.name and sys.implementation.version.
291
-
The exact mechanism for this is implementation-defined by the type checker.
295
+
Type checkers must provide configuration or CLI options to specify target ``sys.version``, ``sys.platform``, ``sys.implementation.name`` and ``sys.implementation.version``.
The configuration options should allow users to specify the target values for these symbols, so that type checkers can evaluate the version and platform checks correctly.
307
+
The exact mechanism and name for these configuration options is implementation-specific, and defined by each type checker.
0 commit comments