-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[red-knot] Implement more types in binary and unary expressions (#13803)
Implemented some points from #12701 - Handle Unknown and Any in Unary operation - Handle Boolean in binary operations - Handle instances in unary operation - Consider division by False to be division by zero --------- Co-authored-by: Carl Meyer <[email protected]> Co-authored-by: Alex Waygood <[email protected]>
- Loading branch information
1 parent
2d2baec
commit 0f0fff4
Showing
4 changed files
with
161 additions
and
5 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
crates/red_knot_python_semantic/resources/mdtest/binary/booleans.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
## Binary operations on booleans | ||
|
||
## Basic Arithmetic | ||
|
||
We try to be precise and all operations except for division will result in Literal type. | ||
|
||
```py | ||
a = True | ||
b = False | ||
|
||
reveal_type(a + a) # revealed: Literal[2] | ||
reveal_type(a + b) # revealed: Literal[1] | ||
reveal_type(b + a) # revealed: Literal[1] | ||
reveal_type(b + b) # revealed: Literal[0] | ||
|
||
reveal_type(a - a) # revealed: Literal[0] | ||
reveal_type(a - b) # revealed: Literal[1] | ||
reveal_type(b - a) # revealed: Literal[-1] | ||
reveal_type(b - b) # revealed: Literal[0] | ||
|
||
reveal_type(a * a) # revealed: Literal[1] | ||
reveal_type(a * b) # revealed: Literal[0] | ||
reveal_type(b * a) # revealed: Literal[0] | ||
reveal_type(b * b) # revealed: Literal[0] | ||
|
||
reveal_type(a % a) # revealed: Literal[0] | ||
reveal_type(b % a) # revealed: Literal[0] | ||
|
||
reveal_type(a // a) # revealed: Literal[1] | ||
reveal_type(b // a) # revealed: Literal[0] | ||
|
||
reveal_type(a**a) # revealed: Literal[1] | ||
reveal_type(a**b) # revealed: Literal[1] | ||
reveal_type(b**a) # revealed: Literal[0] | ||
reveal_type(b**b) # revealed: Literal[1] | ||
|
||
# Division | ||
reveal_type(a / a) # revealed: float | ||
reveal_type(b / a) # revealed: float | ||
b / b # error: [division-by-zero] "Cannot divide object of type `Literal[False]` by zero" | ||
a / b # error: [division-by-zero] "Cannot divide object of type `Literal[True]` by zero" | ||
|
||
# bitwise OR | ||
reveal_type(a | a) # revealed: Literal[True] | ||
reveal_type(a | b) # revealed: Literal[True] | ||
reveal_type(b | a) # revealed: Literal[True] | ||
reveal_type(b | b) # revealed: Literal[False] | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
crates/red_knot_python_semantic/resources/mdtest/unary/instance.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
# Unary Operations | ||
|
||
```py | ||
class Number: | ||
def __init__(self, value: int): | ||
self.value = 1 | ||
|
||
def __pos__(self) -> int: | ||
return +self.value | ||
|
||
def __neg__(self) -> int: | ||
return -self.value | ||
|
||
def __invert__(self) -> Literal[True]: | ||
return True | ||
|
||
|
||
a = Number() | ||
|
||
reveal_type(+a) # revealed: int | ||
reveal_type(-a) # revealed: int | ||
reveal_type(~a) # revealed: @Todo | ||
|
||
|
||
class NoDunder: ... | ||
|
||
|
||
b = NoDunder() | ||
+b # error: [unsupported-operator] "Unary operator `+` is unsupported for type `NoDunder`" | ||
-b # error: [unsupported-operator] "Unary operator `-` is unsupported for type `NoDunder`" | ||
~b # error: [unsupported-operator] "Unary operator `~` is unsupported for type `NoDunder`" | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters