Skip to content
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] Support for not equal narrowing #13749

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Lexxxzy
Copy link
Contributor

@Lexxxzy Lexxxzy commented Oct 14, 2024

Summary

Planning to support type narrowing for != expression as stated in #13694.

  • x != None
  • x != T (where T is any literal type)
  • x is not T (where T is any singleton type, incl a class or function)

Test Plan

Add tests in new md format.

@AlexWaygood AlexWaygood added the red-knot Multi-file analysis & type inference label Oct 14, 2024
@Lexxxzy
Copy link
Contributor Author

Lexxxzy commented Oct 14, 2024

I have a question: Why isn’t there a case for (1 positive, 1 negative) in the match statement of InnerIntersectionBuilder::build? It seems like (1 & ~0) is essentially just 1. Asking because for this example:

x = None if flag else 1
y = 0

if x != 1:
    y = x

    reveal_type(y) # revealed: None & ~Literal[1]

reveal_type(y) # revealed: Literal[0] | None & ~Literal[1]

y in if x != 1 scope should be just None. Also y should be Literal[0] | None outside of condition scope.

Copy link
Contributor

ruff-ecosystem results

Linter (stable)

✅ ecosystem check detected no linter changes.

Linter (preview)

✅ ecosystem check detected no linter changes.

Copy link
Contributor

@sharkdp sharkdp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you very much for working on this. I added a few comments. Let me know if you need any help.

Comment on lines 8 to 14
x = None if flag else 1
y = 0
if x != None:
y = x

reveal_type(x) # revealed: None | Literal[1]
reveal_type(y) # revealed: Literal[0, 1]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With the new test framework, we can now write this in a shorter way, without using additional variables:

Suggested change
x = None if flag else 1
y = 0
if x != None:
y = x
reveal_type(x) # revealed: None | Literal[1]
reveal_type(y) # revealed: Literal[0, 1]
x = None if flag else 1
if x != None:
reveal_type(x) # revealed: Literal[1]
reveal_type(x) # revealed: None | Literal[1]

@@ -155,7 155,7 @@ impl<'db> NarrowingConstraintsBuilder<'db> {
let inference = infer_expression_types(self.db, expression);
for (op, comparator) in std::iter::zip(&**ops, &**comparators) {
let comp_ty = inference.expression_ty(comparator.scoped_ast_id(self.db, scope));
if matches!(op, ast::CmpOp::IsNot) {
if matches!(op, ast::CmpOp::IsNot | ast::CmpOp::NotEq) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This branch needs the same fix that I did for the IsNot branch here: #13758. If you re-apply this | ast::CmpOp::NotEq to the current version on main, that should work automatically.

If we don't filter out non-singleton types, you would get a type of list[int] & ~list[int] == Never in a case like this:

x = [1]
y = [1, 2]

if x != y:
    reveal_type(x)  # should still have type list[int]!

It might make sense to add an explicit test for this as well, even though we have one for is not already. Just in case we ever split the IsNot/NotEq branches for some reason.

@@ -0,0 1,15 @@
# Conditionals
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please move this test to crates/red_knot_python_semantic/resources/mdtest/narrow/conditionals_not_equals.md and adapt the structure to the what the other files in this folder have?


## Narrowing

### `is != None`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
### `is != None`
### `x != None`

@sharkdp
Copy link
Contributor

sharkdp commented Oct 15, 2024

I have a question: Why isn’t there a case for (1 positive, 1 negative) in the match statement of InnerIntersectionBuilder::build?

There is a case for that. The _ catch-all pattern. If there is a positive and a negative contribution, an intersection type needs to be built.

match (self.positive.len(), self.negative.len()) {
    (0, 0) => Type::Never,
    (1, 0) => self.positive[0],
    _ => {
        self.positive.shrink_to_fit();
        self.negative.shrink_to_fit();
        Type::Intersection(IntersectionType::new(db, self.positive, self.negative))
    }
}

There's still the question on how to simplify those intersection types. For example, A & ~A could be simplified to Never. Maybe this is what you had in mind. I will try to look into this soon.

It seems like (1 & ~0) is essentially just 1.

It is. This simplification is exactly what the (1, 0) branch does.

y in if x != 1 scope should be just None

If we ignore the problem that Literal[1] is not a singleton type (#13758), then yes. We would expect None & ~Literal[1] to be simplified to None. This is harder than the A & ~A case above. A & ~B == A is only true if A and B are disjoint.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
red-knot Multi-file analysis & type inference
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants