Skip to content

Commit a317c6f

Browse files
committed
Typing concepts: add that object | Any and Never | Any can be simplified
1 parent 2d88da2 commit a317c6f

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

docs/spec/concepts.rst

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -298,9 +298,9 @@ visualize this analogy in the following table:
298298
* - ``B`` is :term:`equivalent` to ``A``
299299
- ``B`` is :term:`consistent` with ``A``
300300

301-
We can also define an **equivalence** relation on gradual types: the gradual
302-
types ``A`` and ``B`` are equivalent (that is, the same gradual type, not
303-
merely consistent with one another) if and only if all materializations of
301+
We can also define an **equivalence** relation on gradual types: the gradual
302+
types ``A`` and ``B`` are equivalent (that is, the same gradual type, not
303+
merely consistent with one another) if and only if all materializations of
304304
``A`` are also materializations of ``B``, and all materializations of ``B``
305305
are also materializations of ``A``.
306306

@@ -368,10 +368,16 @@ can likewise be materialized to ``T1 | T2``. Thus, the gradual types ``S1`` and
368368

369369
If ``B`` is a subtype of ``A``, ``B | A`` is equivalent to ``A``.
370370

371-
This rule applies only to subtypes, not assignable-to. The union ``T | Any`` is
371+
This rule applies only to subtypes, not assignable-to. For any type ``T``
372+
other than the top and bottom types (``object`` and ``Never``), the union ``T | Any`` is
372373
not reducible to a simpler form. It represents an unknown static type with
373374
lower bound ``T``. That is, it represents an unknown set of objects which may
374375
be as large as ``object``, or as small as ``T``, but no smaller.
376+
The exceptions are ``object`` and ``Never``. The union ``object | Any`` is equivalent to
377+
``object``, because ``object`` is a type containing all values and therefore the ``Any``
378+
cannot add any values. Similarly, ``Never | Any`` is equivalent to ``Any``, because
379+
``Never`` is a type containing no values, so that including it in a union cannot add any
380+
values to the type.
375381

376382
Equivalent gradual types can, however, be simplified from unions; e.g.
377383
``list[Any] | list[Any]`` is equivalent to ``list[Any]``. Similarly, the union

0 commit comments

Comments
 (0)