Skip to content

Commit

Permalink
[red-knot] Support for noteq None narrowing
Browse files Browse the repository at this point in the history
  • Loading branch information
Lexxxzy committed Oct 14, 2024
1 parent 814ab47 commit d837691
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
15 changes: 15 additions & 0 deletions crates/red_knot_python_semantic/resources/mdtest/conditionals.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Conditionals

## Narrowing

### `is != None`

```py
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]
```
2 changes: 1 addition & 1 deletion crates/red_knot_python_semantic/src/types/narrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
let ty = IntersectionBuilder::new(self.db)
.add_negative(comp_ty)
.build();
Expand Down

0 comments on commit d837691

Please sign in to comment.