Skip to content

Commit 98e3697

Browse files
committed
spec/directives: Process review comments.
Signed-off-by: Jos Verlinde <Jos_Verlinde@hotmail.com>
1 parent 2c4e700 commit 98e3697

1 file changed

Lines changed: 40 additions & 24 deletions

File tree

docs/spec/directives.rst

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ left undefined by the typing spec at this time.
154154
Version and platform checking
155155
-----------------------------
156156

157-
Type checkers should support narrowing based on:
157+
Type checkers should understand code paths as definitely reachable or not reachable due to comparison tests against these symbols:
158158
* ``sys.version_info``
159159
* ``sys.platform``
160160
* ``sys.implementation.version``
@@ -176,8 +176,9 @@ Type checkers should support the following comparison patterns:
176176
* ``sys.version_info >= <2-tuple>``
177177
* ``sys.version_info < <2-tuple>``
178178

179-
Comparisons checks are only supported against the first two elements of the version tuple.
180-
Use of named attributes is not mandated.
179+
Comparison checks are only supported against the first two elements of the version tuple.
180+
It should be noted that type checkers may choose to also support the 3-tuple ``sys.version_info >= <3-tuple>``.
181+
Type checkers are not expected to support comparisons with named attributes of `sys.version_info`.
181182

182183
.. code-block:: python
183184
:caption: Example `sys.version_info`
@@ -186,21 +187,24 @@ Use of named attributes is not mandated.
186187
import sys
187188
if sys.version_info >= (3, 12):
188189
# Python 3.12+
190+
elif sys.version_info >= (3, 11):
191+
# Python 3.11
189192
else:
190-
# Python 3.11 and lower
193+
# Python 3.10 and lower
191194
192195
sys.platform checks
193196
^^^^^^^^^^^^^^^^^^^
194197

195198
Type checkers should support the following comparison patterns:
196199
* ``sys.platform == <string literal>``
197200
* ``sys.platform != <string literal>``
198-
* ``sys.platform in <tuple of string literals>``
199-
* ``sys.platform not in <tuple of string literals>``
201+
* ``sys.platform.startswith(<string literal>)``
202+
* ``sys.platform in <set of string literals>``
203+
* ``sys.platform not in <set of string literals>``
200204

201205
Common values: ``"linux"``, ``"darwin"``, ``"win32"``, ``"emscripten"``, ``"wasi"``
202206

203-
The membership checks ``in`` and ``not in`` only support simple containment testing with a tuple of literal strings.
207+
The membership checks ``in`` and ``not in`` only support simple containment testing with a set of literal strings.
204208

205209
.. code-block:: python
206210
:caption: Example `sys.platform`
@@ -220,11 +224,13 @@ sys.implementation.name checks
220224
Type checkers should support comparison patterns:
221225
* ``sys.implementation.name == <string literal>``
222226
* ``sys.implementation.name != <string literal>``
223-
* ``sys.implementation.name in <tuple of string literals>``
224-
* ``sys.implementation.name not in <tuple of string literals>``
227+
* ``sys.implementation.name in <set of string literals>``
228+
* ``sys.implementation.name not in <set of string literals>``
225229

230+
Default value: ``"cpython"``, unless configured otherwise.
226231
Common values: ``"cpython"``, ``"pypy"``, ``"micropython"``, ``"graalpy"``, ``"jython"``, ``"ironpython"``
227232

233+
228234
.. code-block:: python
229235
:caption: Example `sys.implementation.name`
230236
:emphasize-lines: 2,4
@@ -239,12 +245,16 @@ Type checkers should support comparison patterns:
239245
sys.implementation.version checks
240246
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
241247

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+
242252
Type checkers should support the following comparison patterns:
243253
* ``sys.implementation.version >= <2-tuple>``
244254
* ``sys.implementation.version < <2-tuple>``
245255

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`.
248258

249259
.. code-block:: python
250260
:caption: Example `sys.implementation.version`
@@ -256,39 +266,45 @@ Use of named attributes is not mandated.
256266
if sys.implementation.name == "micropython" and sys.implementation.version >= (1, 24):
257267
# MicroPython version 1.24 and above
258268
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-
264269
265270
No support for complex expressions
266271
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
267272

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.
270274

271275
Therefore checkers are **not required** to understand obfuscations such as:
272276

273277
.. code-block:: python
274278
:caption: Examples of unsupported or overly complex version/platform checks
275-
:emphasize-lines: 3,5,7
279+
:emphasize-lines: 4,6,8
276280
277281
import sys
278282
from sys import platform
283+
279284
if "".join(reversed(sys.platform)) == "xunil":
280-
# Linux specific code
285+
# Typecheckers will not be required to understand this obfuscated check
281286
if platform == "linux":
282-
# Linux specific code
287+
# Typecheckers will not be required to understand this import alias for sys.platform
283288
if "win" not in sys.platform:
284-
# Non-Windows specific code
289+
# Typecheckers will not be required to understand this reversed membership check
285290
286291
287292
Configuration
288293
^^^^^^^^^^^^^
289294

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``.
296+
297+
================================ ========================== ============== ===========================================================================
298+
Symbol Suggested Format Example Suggested Default
299+
================================ ========================== ============== ===========================================================================
300+
``sys.version`` string ``"major.minor"`` ``"3.11"`` The version of the Python interpreter used to run the type checker.
301+
``sys.platform`` lowercase string ``"linux"`` The platform of the Python interpreter used to run the type checker.
302+
``sys.implementation.name`` lowercase string ``"cpython"`` ``"cpython"`` unless configured otherwise.
303+
``sys.implementation.version`` string ``"major.minor"`` ``"3.14"`` The value used for ``sys.version`` unless configured otherwise.
304+
================================ ========================== ============== ===========================================================================
305+
306+
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.
292308

293309
.. _`deprecated`:
294310

0 commit comments

Comments
 (0)