Skip to content

Commit

Permalink
use asymmetric example of assignable-to
Browse files Browse the repository at this point in the history
  • Loading branch information
carljm committed May 22, 2024
1 parent 24e6ee5 commit 9e9ebb4
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions docs/spec/concepts.rst
Original file line number Diff line number Diff line change
Expand Up @@ -157,22 +157,15 @@ The consistency relation is symmetric. If ``A`` is consistent with ``B``, ``B``
is also consistent with ``A``. It is also reflexive: ``A`` is always consistent
with ``A``.

The consistent subtype relation
-------------------------------
The assignable-to (or consistent subtyping) relation
----------------------------------------------------

Given the materialization relation and the subtyping relation, we define the
Given the materialization relation and the subtyping relation, we can define the
**consistent subtype** relation over all types. A type ``A`` is a consistent
subtype of a type ``B`` if there exists a materialization ``A'`` of ``A`` and a
materialization ``B'`` of ``B``, where ``A'`` and ``B'`` are both static types,
and ``A'`` is a subtype of ``B'``.

For example, ``Any`` is a consistent subtype of ``int``, because ``int`` is a
materialization of ``Any``, and ``int`` is a subtype of ``int``. The same
materialization also gives that ``int`` is a consistent subtype of ``Any``.

Consistent subtyping defines assignability
------------------------------------------

Consistent subtyping defines "assignability" for Python. An expression can be
assigned to a variable (including passed as a parameter or returned from a
function) if it is a consistent subtype of the variable's type annotation
Expand All @@ -182,10 +175,20 @@ We can say that a type ``A`` is "assignable to" a type ``B`` if ``A`` is a
consistent subtype of ``B``.

In the remainder of this specification, we will usually prefer the term
**assignable to** over "consistent subtype of". The two are synonyms, but
**assignable to** over "consistent subtype of". The two are synonymous, but
"assignable to" is shorter, and may communicate a clearer intuition to many
readers.

For example, ``Any`` is assignable to ``int``, because ``int`` is a
materialization of ``Any``, and ``int`` is a subtype of ``int``. The same
materialization also gives that ``int`` is assignable to ``Any``.

The assignable-to relation is not generally symmetric, though. If ``B`` is a
subtype of ``A``, then ``tuple[Any, B]`` is assignable to ``tuple[int, A]``,
because ``tuple[Any, B]`` can materialize to ``tuple[int, B]``, which is a
subtype of ``tuple[int, A]``. But ``tuple[int, A]`` is not assignable to
``tuple[Any, B]``.

References
----------

Expand Down

0 comments on commit 9e9ebb4

Please sign in to comment.