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
Copy file name to clipboardExpand all lines: docs/spec/directives.rst
+129-6Lines changed: 129 additions & 6 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -145,8 +145,27 @@ left undefined by the typing spec at this time.
145
145
Version and platform checking
146
146
-----------------------------
147
147
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::
150
169
151
170
import sys
152
171
@@ -155,13 +174,117 @@ checks, e.g.::
155
174
else:
156
175
# Python 3.11 and lower
157
176
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
+
158
197
if sys.platform == 'win32':
159
198
# Windows specific definitions
160
-
else:
161
-
# Posix specific definitions
162
199
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:
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.
0 commit comments