From a317c6fe7d5ab38d837cdd71457dd4d2cfa314bc Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sun, 20 Jul 2025 20:28:15 -0700 Subject: [PATCH 1/2] Typing concepts: add that object | Any and Never | Any can be simplified --- docs/spec/concepts.rst | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/docs/spec/concepts.rst b/docs/spec/concepts.rst index ee230f23..4cfe86b1 100644 --- a/docs/spec/concepts.rst +++ b/docs/spec/concepts.rst @@ -298,9 +298,9 @@ visualize this analogy in the following table: * - ``B`` is :term:`equivalent` to ``A`` - ``B`` is :term:`consistent` with ``A`` -We can also define an **equivalence** relation on gradual types: the gradual -types ``A`` and ``B`` are equivalent (that is, the same gradual type, not -merely consistent with one another) if and only if all materializations of +We can also define an **equivalence** relation on gradual types: the gradual +types ``A`` and ``B`` are equivalent (that is, the same gradual type, not +merely consistent with one another) if and only if all materializations of ``A`` are also materializations of ``B``, and all materializations of ``B`` are also materializations of ``A``. @@ -368,10 +368,16 @@ can likewise be materialized to ``T1 | T2``. Thus, the gradual types ``S1`` and If ``B`` is a subtype of ``A``, ``B | A`` is equivalent to ``A``. -This rule applies only to subtypes, not assignable-to. The union ``T | Any`` is +This rule applies only to subtypes, not assignable-to. For any type ``T`` +other than the top and bottom types (``object`` and ``Never``), the union ``T | Any`` is not reducible to a simpler form. It represents an unknown static type with lower bound ``T``. That is, it represents an unknown set of objects which may be as large as ``object``, or as small as ``T``, but no smaller. +The exceptions are ``object`` and ``Never``. The union ``object | Any`` is equivalent to +``object``, because ``object`` is a type containing all values and therefore the ``Any`` +cannot add any values. Similarly, ``Never | Any`` is equivalent to ``Any``, because +``Never`` is a type containing no values, so that including it in a union cannot add any +values to the type. Equivalent gradual types can, however, be simplified from unions; e.g. ``list[Any] | list[Any]`` is equivalent to ``list[Any]``. Similarly, the union From 3e3a30e6272e83e2c40144dcc8f7a24bab51fe0d Mon Sep 17 00:00:00 2001 From: Jelle Zijlstra Date: Sun, 20 Jul 2025 21:37:16 -0700 Subject: [PATCH 2/2] Update docs/spec/concepts.rst --- docs/spec/concepts.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/spec/concepts.rst b/docs/spec/concepts.rst index 4cfe86b1..d2d0353c 100644 --- a/docs/spec/concepts.rst +++ b/docs/spec/concepts.rst @@ -369,7 +369,7 @@ can likewise be materialized to ``T1 | T2``. Thus, the gradual types ``S1`` and If ``B`` is a subtype of ``A``, ``B | A`` is equivalent to ``A``. This rule applies only to subtypes, not assignable-to. For any type ``T`` -other than the top and bottom types (``object`` and ``Never``), the union ``T | Any`` is +(other than the top and bottom types ``object`` and ``Never``), the union ``T | Any`` is not reducible to a simpler form. It represents an unknown static type with lower bound ``T``. That is, it represents an unknown set of objects which may be as large as ``object``, or as small as ``T``, but no smaller.