ruff
5c0df7a1 - [red-knot] add type narrowing (#11790)

Commit
2 years ago
[red-knot] add type narrowing (#11790) ## Summary Add Constraint nodes to flow graph, and narrow types based on that (only `is None` and `is not None` narrowing supported for now, to prototype the structure.) Also add simplification of zero- and one-element unions and intersections, and flattening of intersections. There's a lot more normalization logic needed for unions and intersections (as is obvious from the inferred type in the added `narrow_none` test), but this will be non-trivial and I'd rather do it in a separate PR. Here's a flowchart diagram for the code in the added `narrow_none` test: ![Screenshot 2024-06-07 at 2 58 00 PM](https://github.com/astral-sh/ruff/assets/61586/5152a400-739c-41ff-8bbf-3c19d16bd083) The top branch is for the `if` expression in the initial assignment to `x`; that `Constraint` node would only affect the type of `flag`, which we don't care about in this test. The second branch is for the `if` statement, with `Constraint` node affecting the type of `x`. ## Test Plan Added tests.
Author
Parents
Loading