-
Notifications
You must be signed in to change notification settings - Fork 262
Typing concepts: add that object | Any and Never | Any can be simplified #2052
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JelleZijlstra
wants to merge
2
commits into
python:main
Choose a base branch
from
JelleZijlstra:union-with-any
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+10
−4
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
Comment on lines
+378
to
+380
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
Equivalent gradual types can, however, be simplified from unions; e.g. | ||
``list[Any] | list[Any]`` is equivalent to ``list[Any]``. Similarly, the union | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is another exception explained in the paragraph below (for equivalent gradual types). To be honest, I'm not sure if it really helps with clarity to add more exceptions here. It feels to me like the original intent of this paragraph was maybe just to explain that
T | Any
is not generally reducible to a simpler form (Any
)?Also, the existing sentence "It represents an unknown static type with lower bound
T
" is only true ifT
is fully-static.Maybe both of these problems could be solved by going with something like: