Skip to content

Commit 8bdd9e2

Browse files
committed
Support unary and binary ops
1 parent 0c1745b commit 8bdd9e2

File tree

2 files changed

+9
-8
lines changed

2 files changed

+9
-8
lines changed

crates/red_knot_python_semantic/resources/mdtest/generics/functions.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ def good_return[T: int](x: T) -> T:
107107
return x
108108

109109
def bad_return[T: int](x: T) -> T:
110-
# TODO: error: int is not assignable to T
110+
# error: [invalid-return-type] "Object of type `int` is not assignable to return type `T`"
111111
return x + 1
112112
```
113113

@@ -137,6 +137,8 @@ methods that are compatible with the return type, so the `return` expression is
137137

138138
```py
139139
def same_constrained_types[T: (int, str)](t1: T, t2: T) -> T:
140+
# TODO: no error
141+
# error: [unsupported-operator] "Operator `+` is unsupported between objects of type `T` and `T`"
140142
return t1 + t2
141143
```
142144

crates/red_knot_python_semantic/src/types/infer.rs

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4430,8 +4430,6 @@ impl<'db> TypeInferenceBuilder<'db> {
44304430
match (op, operand_type) {
44314431
(_, Type::Dynamic(_)) => operand_type,
44324432
(_, Type::Never) => Type::Never,
4433-
// TODO: Apply the unary expression to the typevar's upper bound/constraints.
4434-
(_, Type::TypeVar(_)) => Type::unknown(),
44354433

44364434
(ast::UnaryOp::UAdd, Type::IntLiteral(value)) => Type::IntLiteral(value),
44374435
(ast::UnaryOp::USub, Type::IntLiteral(value)) => Type::IntLiteral(-value),
@@ -4472,7 +4470,8 @@ impl<'db> TypeInferenceBuilder<'db> {
44724470
| Type::LiteralString
44734471
| Type::BytesLiteral(_)
44744472
| Type::SliceLiteral(_)
4475-
| Type::Tuple(_),
4473+
| Type::Tuple(_)
4474+
| Type::TypeVar(_),
44764475
) => {
44774476
let unary_dunder_method = match op {
44784477
ast::UnaryOp::Invert => "__invert__",
@@ -4594,8 +4593,6 @@ impl<'db> TypeInferenceBuilder<'db> {
45944593
(todo @ Type::Dynamic(DynamicType::TodoProtocol), _, _)
45954594
| (_, todo @ Type::Dynamic(DynamicType::TodoProtocol), _) => Some(todo),
45964595
(Type::Never, _, _) | (_, Type::Never, _) => Some(Type::Never),
4597-
// TODO: Apply the binary expression to the typevar's upper bound/constraints.
4598-
(Type::TypeVar(_), _, _) | (_, Type::TypeVar(_), _) => Some(Type::unknown()),
45994596

46004597
(Type::IntLiteral(n), Type::IntLiteral(m), ast::Operator::Add) => Some(
46014598
n.checked_add(m)
@@ -4749,7 +4746,8 @@ impl<'db> TypeInferenceBuilder<'db> {
47494746
| Type::LiteralString
47504747
| Type::BytesLiteral(_)
47514748
| Type::SliceLiteral(_)
4752-
| Type::Tuple(_),
4749+
| Type::Tuple(_)
4750+
| Type::TypeVar(_),
47534751
Type::FunctionLiteral(_)
47544752
| Type::Callable(..)
47554753
| Type::BoundMethod(_)
@@ -4769,7 +4767,8 @@ impl<'db> TypeInferenceBuilder<'db> {
47694767
| Type::LiteralString
47704768
| Type::BytesLiteral(_)
47714769
| Type::SliceLiteral(_)
4772-
| Type::Tuple(_),
4770+
| Type::Tuple(_)
4771+
| Type::TypeVar(_),
47734772
op,
47744773
) => {
47754774
// We either want to call lhs.__op__ or rhs.__rop__. The full decision tree from

0 commit comments

Comments
 (0)