-
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] more precise types from chained boolean expressions #13632
Comments
If I understand correctly
I have some questions:
|
Those are great questions! Any type "makes sense" independently as long as we can describe it as some set of possible runtime values. So these types can stand alone, in principle: In practice, I expect that we would only create these types in intersections, wherever we are able to determine that a type must be truthy or falsy. If we introduce this type, we accept (at least for now) the fact that it may leak to users, since we won't be able to simplify it in all cases. We will often have this tradeoff, where we can more fully represent our type knowledge, at the cost of displaying more complex types to users. In general, I would prefer to represent the most precise type knowledge we can be confident of, and then find ways to simplify the user type display if that proves to be necessary. For instance, we could later implement logic in the display code for intersections to just hide these types in the output, if we end up feeling that it becomes too noisy otherwise. My hope, though, is that users will find these types reasonably intuitive, and will appreciate more precise typing. One thing to note here is that currently (since we've so far only implemented support for one form of narrowing, |
So, I started working on this. I have the following question: when expressing Let's take the following example: def f(x: int):
if x:
... # here, `x` is `Truthy` or `x` is not `Falsy` |
Related to @Slyces's question here... do we need a |
I think we could probably get away with only the |
This comment has been minimized.
This comment has been minimized.
With the merge of #14687, this task should now be pretty trivial. We now have |
We currently don't eliminate some impossible types in chained boolean or comparison expressions.
This one is an existing test in
comparison/non_bool_returns.md
:Both of these can be fixed by adding negative intersections with
Type::AlwaysTruthy
orType::AlwaysFalsy
in the appropriate cases inTypeInferenceBuilder::infer_chained_boolean_types
.The text was updated successfully, but these errors were encountered: