Skip to content

Commit 8b7b5d4

Browse files
committed
Document all the boolean/for stuff
1 parent 5e105f2 commit 8b7b5d4

1 file changed

Lines changed: 58 additions & 14 deletions

File tree

pre-pep.rst

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -371,10 +371,10 @@ Note first that no changes to the **Python** grammar are being
371371
proposed, only to the grammar of what Python expressions are
372372
considered as valid types.
373373

374-
(It's also slightly imprecise to call this a grammar: where operator
375-
names are mentioned directly, like ``IsSub``, they require that name
376-
to be imported, and it could also be used qualified as
377-
``typing.IsSub`` or imported as a different name.)
374+
(It's also slightly imprecise to call this a grammar:
375+
``<bool-operator>`` refers to any of the names defined in the
376+
:ref:`Boolean Operators <boolean-ops>` section, which might be
377+
imported qualified or with some other name)
378378

379379
::
380380

@@ -390,18 +390,13 @@ to be imported, and it could also be used qualified as
390390
| <ident>[<variadic-type-arg> +]
391391

392392
# Type conditional checks are boolean compositions of
393-
# "subtype checking" and boolean Literal type checking.
393+
# boolean type operators
394394
<type-bool> =
395-
IsSub[<type>, <type>]
396-
| Bool[<type>]
395+
<bool-operator>[<type> +]
397396
| not <type-bool>
398397
| <type-bool> and <type-bool>
399398
| <type-bool> or <type-bool>
400399

401-
# Do we want these next two? Maybe not.
402-
| Any[<variadic-type-arg> +]
403-
| All[<variadic-type-arg> +]
404-
405400
<variadic-type-arg> =
406401
<type> ,
407402
| * <type-for-iter> ,
@@ -415,8 +410,46 @@ to be imported, and it could also be used qualified as
415410
if <type-bool>
416411

417412

418-
TODO: explain conditional types and iteration
413+
There are three core syntactic features introduced: type booleans,
414+
conditional types and unpacked comprehension types.
415+
416+
Type booleans
417+
'''''''''''''
418+
419+
Type booleans are a special subset of the type language that can be
420+
used in the body of conditionals. They consist of the
421+
:ref:`Boolean Operators <boolean-ops>`, defined below,
422+
potentially combined with ``and``, ``or``, and ``not``.
423+
424+
When evaluated, they will evaluate to ``Literal[True]`` or
425+
``Literal[False]]``.
426+
427+
(We want to restrict what operators may be used in a conditional
428+
so that at runtime, we can have those operators produce "type" values
429+
with appropriate behavior, without needing to change the behavior of
430+
existing ``Literal[False]`` values and the like.)
431+
432+
433+
Conditional types
434+
'''''''''''''''''
435+
436+
The type ``true_typ if bool_typ else false_typ`` is a conditional
437+
type, which resolves to ``true_typ`` if ``bool_typ`` is equivalent to
438+
``Literal[True]`` and to ``true_typ`` otherwise.
439+
440+
``bool_typ`` is a type, but it needs syntactically be a type boolean,
441+
defined above.
442+
443+
Unpacked comprehension
444+
''''''''''''''''''''''
419445

446+
An unpacked comprehension, ``*[ty for t in Iter[iter_ty]]`` may appear
447+
anywhere in a type that ``Unpack[...]`` is currently allowed, and it
448+
evaluates essentially to an ``Unpack`` of a tuple produced by a list
449+
comprehension iterating over the arguments of tuple type ``iter_ty``.
450+
451+
The comprehension may also have ``if`` clauses, which filter in the
452+
usual way.
420453

421454
Type operators
422455
--------------
@@ -425,13 +458,24 @@ In some sections below we write things like ``Literal[int]]`` to mean
425458
"a literal that is of type ``int``". I don't think I'm really
426459
proposing to add that as a notion, but we could.
427460

428-
Boolean types
429-
'''''''''''''
461+
.. _boolean-ops:
462+
463+
Boolean operators
464+
'''''''''''''''''
430465

431466
* ``IsSub[T, S]``: What we would **want** is that it returns a boolean
432467
literal type indicating whether ``T`` is a subtype of ``S``.
433468
To support runtime checking, we probably need something weaker.
434469

470+
* ``Bool[T]``: Returns ``Literal[True]`` if ``T`` is also
471+
``Literal[True]`` or a union containing it.
472+
Equivalent to ``IsSub[T, Literal[True]] and not IsSub[T, Never]``.
473+
474+
* ``Any[*Ts]``: Returns ``Literal[True]`` if any of ``Ts`` are true
475+
(by the rule given in ``Bool``) and ``Literal[False]`` otherwise.
476+
477+
* ``All[*Ts]``: Returns ``Literal[True]`` if all of ``Ts`` are true
478+
(by the rule given in ``Bool``) and ``Literal[False]`` otherwise.
435479

436480
Basic operators
437481
'''''''''''''''

0 commit comments

Comments
 (0)