-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
[red-knot] AlwaysTruthy | bool
is equivalent to AlwaysTruthy | Literal[False]
#15513
Comments
There are a few of these that we need to recognise, actually:
|
Some failing tests, if anybody is interested in working on this: --- a/crates/red_knot_python_semantic/resources/mdtest/union_types.md
+++ b/crates/red_knot_python_semantic/resources/mdtest/union_types.md
@@ -141,3 +141,17 @@ def _(
reveal_type(i1) # revealed: P & Q
reveal_type(i2) # revealed: P & Q
```
+
+## Equivalent unions
+
+```py
+from typing_extensions import Literal
+from knot_extensions import is_equivalent_to, AlwaysTruthy, AlwaysFalsy, static_assert, Intersection
+
+static_assert(is_equivalent_to(AlwaysTruthy | bool, AlwaysTruthy | Literal[False]))
+static_assert(is_equivalent_to(AlwaysFalsy | bool, AlwaysFalsy | Literal[True]))
+static_assert(is_equivalent_to(AlwaysTruthy | tuple, AlwaysTruthy | tuple[()]))
+static_assert(is_equivalent_to(AlwaysTruthy | LiteralString, AlwaysTruthy | Literal[""]))
+``` |
This (and the complexity of our existing handling of This would be generally in line with "one internal representation for equivalent things." This means that intersections with |
Yes, that sounds reasonable to me! I agree that this one seems like a bit of a pain to work into our existing infrastructure without doing something like that |
This works for the
|
Ah... and even with the |
Would this be solved by a special case in I guess if our aim is to prefer "one representation for one type" then we should prefer to decompose everywhere or nowhere. But I don't think decomposing everywhere is really an option, as we need the typeshed stub for the |
This equivalence follows from the fact that
bool
is equivalent toLiteral[True] | Literal[False]
and from the fact thatLiteral[True]
is a subtype ofAlwaysTruthy
. A proof of the fact that these are equivalent can be found in the fact that there are two ways that the unionLiteral[False] | Literal[True] | AlwaysTruthy
could be validly simplified:(Literal[False] | Literal[True]) | AlwaysTruthy
->bool | AlwaysTruthy
Literal[False] | (Literal[True] | AlwaysTruthy)
->Literal[False] | AlwaysTruthy
We don't currently recognise that
AlwaysTruthy | bool
andAlwaysTruthy | Literal[False]
are equivalent types; this should be fixed.The text was updated successfully, but these errors were encountered: