Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions crates/red_knot_python_semantic/resources/mdtest/narrow/type.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,3 +144,13 @@ def _(x: Base):
# express a constraint like `Base & ~ProperSubtypeOf[Base]`.
reveal_type(x) # revealed: Base
```

## Assignment expressions

```py
def _(x: object):
if (y := type(x)) is bool:
reveal_type(y) # revealed: Literal[bool]
if (type(y := x)) is bool:
reveal_type(y) # revealed: bool
```
51 changes: 22 additions & 29 deletions crates/red_knot_python_semantic/src/types/narrow.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,17 @@ fn negate_if<'db>(constraints: &mut NarrowingConstraints<'db>, db: &'db dyn Db,
}
}

fn expr_name(expr: &ast::Expr) -> Option<&ast::name::Name> {
match expr {
ast::Expr::Named(ast::ExprNamed { target, .. }) => match target.as_ref() {
ast::Expr::Name(ast::ExprName { id, .. }) => Some(id),
_ => None,
},
ast::Expr::Name(ast::ExprName { id, .. }) => Some(id),
_ => None,
}
}

Comment on lines +241 to +251
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Im not sure the best place to put a function like this

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this is a fine spot for now!

struct NarrowingConstraintsBuilder<'db> {
db: &'db dyn Db,
predicate: PredicateNode<'db>,
Expand Down Expand Up @@ -497,27 +508,9 @@ impl<'db> NarrowingConstraintsBuilder<'db> {
last_rhs_ty = Some(rhs_ty);

match left {
ast::Expr::Name(ast::ExprName {
range: _,
id,
ctx: _,
}) => {
let symbol = self.expect_expr_name_symbol(id);

let op = if is_positive { *op } else { op.negate() };

if let Some(ty) = self.evaluate_expr_compare_op(lhs_ty, rhs_ty, op) {
constraints.insert(symbol, ty);
}
}
ast::Expr::Named(ast::ExprNamed {
range: _,
target,
value: _,
}) => {
if let ast::Expr::Name(ast::ExprName { id, .. }) = target.as_ref() {
ast::Expr::Name(_) | ast::Expr::Named(_) => {
if let Some(id) = expr_name(left) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice!

let symbol = self.expect_expr_name_symbol(id);

let op = if is_positive { *op } else { op.negate() };

if let Some(ty) = self.evaluate_expr_compare_op(lhs_ty, rhs_ty, op) {
Expand Down Expand Up @@ -545,8 +538,12 @@ impl<'db> NarrowingConstraintsBuilder<'db> {
}
};

let [ast::Expr::Name(ast::ExprName { id, .. })] = &**args else {
continue;
let id = match &**args {
[first] => match expr_name(first) {
Some(id) => id,
None => continue,
},
_ => continue,
};

let is_valid_constraint = if is_positive {
Expand Down Expand Up @@ -598,13 +595,9 @@ impl<'db> NarrowingConstraintsBuilder<'db> {
let function = function_type.known(self.db)?.into_constraint_function()?;

let (id, class_info) = match &*expr_call.arguments.args {
[first, class_info] => match first {
ast::Expr::Named(ast::ExprNamed { target, .. }) => match target.as_ref() {
ast::Expr::Name(ast::ExprName { id, .. }) => (id, class_info),
_ => return None,
},
ast::Expr::Name(ast::ExprName { id, .. }) => (id, class_info),
_ => return None,
[first, class_info] => match expr_name(first) {
Some(id) => (id, class_info),
None => return None,
},
_ => return None,
};
Expand Down
Loading