-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into binary-arithmetic
- Loading branch information
Showing
61 changed files
with
731 additions
and
557 deletions.
There are no files selected for viewing
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
67 changes: 35 additions & 32 deletions
67
crates/red_knot_python_semantic/resources/mdtest/binary/integers.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 |
---|---|---|
@@ -1,47 +1,50 @@ | ||
## Binary operations on integers | ||
# Binary operations on integers | ||
|
||
## Basic Arithmetic | ||
|
||
```py | ||
a = 2 + 1 | ||
b = a - 4 | ||
c = a * b | ||
d = c // 3 | ||
e = c / 3 | ||
f = 5 % 3 | ||
|
||
reveal_type(a) # revealed: Literal[3] | ||
reveal_type(b) # revealed: Literal[-1] | ||
reveal_type(c) # revealed: Literal[-3] | ||
reveal_type(d) # revealed: Literal[-1] | ||
reveal_type(e) # revealed: float | ||
reveal_type(f) # revealed: Literal[2] | ||
reveal_type(2 + 1) # revealed: Literal[3] | ||
reveal_type(3 - 4) # revealed: Literal[-1] | ||
reveal_type(3 * -1) # revealed: Literal[-3] | ||
reveal_type(-3 // 3) # revealed: Literal[-1] | ||
reveal_type(-3 / 3) # revealed: float | ||
reveal_type(5 % 3) # revealed: Literal[2] | ||
``` | ||
|
||
## Division by Zero | ||
|
||
```py | ||
class MyInt(int): | ||
def __truediv__(self, other): | ||
return 100 | ||
This error is really outside the current Python type system, because e.g. `int.__truediv__` and | ||
friends are not annotated to indicate that it's an error, and we don't even have a facility to | ||
permit such an annotation. So arguably divide-by-zero should be a lint error rather than a type | ||
checker error. But we choose to go ahead and error in the cases that are very likely to be an error: | ||
dividing something typed as `int` or `float` by something known to be `Literal[0]`. | ||
|
||
def returns_int() -> int: | ||
return MyInt(3) | ||
This isn't _definitely_ an error, because the object typed as `int` or `float` could be an instance | ||
of a custom subclass which overrides division behavior to handle zero without error. But if this | ||
unusual case occurs, the error can be avoided by explicitly typing the dividend as that safe custom | ||
subclass; we only emit the error if the LHS type is exactly `int` or `float`, not if its a subclass. | ||
|
||
# TODO: `a` should be `int` and `e` should be `float` once we support inference. | ||
```py | ||
a = 1 / 0 # error: "Cannot divide object of type `Literal[1]` by zero" | ||
b = 2 // 0 # error: "Cannot floor divide object of type `Literal[2]` by zero" | ||
c = 3 % 0 # error: "Cannot reduce object of type `Literal[3]` modulo zero" | ||
# even `int` type could be a subclass of `int` with custom behavior; no error | ||
d = returns_int() / 0 | ||
# this could be flagged as an error, if we had an ExactFloat or ExactInstance | ||
# type, but given only a `float` type we can't issue an error for the same | ||
# reason: could be a custom float subclass | ||
e = 1.0 / 0 | ||
|
||
reveal_type(a) # revealed: float | ||
|
||
b = 2 // 0 # error: "Cannot floor divide object of type `Literal[2]` by zero" | ||
reveal_type(b) # revealed: int | ||
|
||
c = 3 % 0 # error: "Cannot reduce object of type `Literal[3]` modulo zero" | ||
reveal_type(c) # revealed: int | ||
reveal_type(d) # revealed: float | ||
reveal_type(e) # revealed: float | ||
|
||
# error: "Cannot divide object of type `int` by zero" | ||
# revealed: float | ||
reveal_type(int() / 0) | ||
|
||
# error: "Cannot divide object of type `float` by zero" | ||
# revealed: float | ||
reveal_type(1.0 / 0) | ||
|
||
class MyInt(int): pass | ||
|
||
# No error for a subclass of int | ||
# revealed: float | ||
reveal_type(MyInt(3) / 0) | ||
``` |
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
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 |
---|---|---|
|
@@ -3,6 +3,5 @@ | |
```py | ||
class Foo: ... | ||
|
||
x = Foo() | ||
reveal_type(x) # revealed: Foo | ||
reveal_type(Foo()) # revealed: Foo | ||
``` |
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
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
38 changes: 19 additions & 19 deletions
38
crates/red_knot_python_semantic/resources/mdtest/comparison/byte_literals.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 |
---|---|---|
@@ -1,43 +1,43 @@ | ||
### Comparison: Byte literals | ||
# Comparison: Byte literals | ||
|
||
These tests assert that we infer precise `Literal` types for comparisons between objects | ||
inferred as having `Literal` bytes types: | ||
|
||
```py | ||
reveal_type(b"abc" == b"abc") # revealed: Literal[True] | ||
reveal_type(b"abc" == b"ab") # revealed: Literal[False] | ||
reveal_type(b"abc" == b"ab") # revealed: Literal[False] | ||
|
||
reveal_type(b"abc" != b"abc") # revealed: Literal[False] | ||
reveal_type(b"abc" != b"ab") # revealed: Literal[True] | ||
reveal_type(b"abc" != b"ab") # revealed: Literal[True] | ||
|
||
reveal_type(b"abc" < b"abd") # revealed: Literal[True] | ||
reveal_type(b"abc" < b"abb") # revealed: Literal[False] | ||
reveal_type(b"abc" < b"abd") # revealed: Literal[True] | ||
reveal_type(b"abc" < b"abb") # revealed: Literal[False] | ||
|
||
reveal_type(b"abc" <= b"abc") # revealed: Literal[True] | ||
reveal_type(b"abc" <= b"abb") # revealed: Literal[False] | ||
|
||
reveal_type(b"abc" > b"abd") # revealed: Literal[False] | ||
reveal_type(b"abc" > b"abb") # revealed: Literal[True] | ||
reveal_type(b"abc" > b"abd") # revealed: Literal[False] | ||
reveal_type(b"abc" > b"abb") # revealed: Literal[True] | ||
|
||
reveal_type(b"abc" >= b"abc") # revealed: Literal[True] | ||
reveal_type(b"abc" >= b"abd") # revealed: Literal[False] | ||
|
||
reveal_type(b"" in b"") # revealed: Literal[True] | ||
reveal_type(b"" in b"abc") # revealed: Literal[True] | ||
reveal_type(b"abc" in b"") # revealed: Literal[False] | ||
reveal_type(b"ab" in b"abc") # revealed: Literal[True] | ||
reveal_type(b"abc" in b"abc") # revealed: Literal[True] | ||
reveal_type(b"d" in b"abc") # revealed: Literal[False] | ||
reveal_type(b"ac" in b"abc") # revealed: Literal[False] | ||
reveal_type(b"" in b"") # revealed: Literal[True] | ||
reveal_type(b"" in b"abc") # revealed: Literal[True] | ||
reveal_type(b"abc" in b"") # revealed: Literal[False] | ||
reveal_type(b"ab" in b"abc") # revealed: Literal[True] | ||
reveal_type(b"abc" in b"abc") # revealed: Literal[True] | ||
reveal_type(b"d" in b"abc") # revealed: Literal[False] | ||
reveal_type(b"ac" in b"abc") # revealed: Literal[False] | ||
reveal_type(b"\x81\x82" in b"\x80\x81\x82") # revealed: Literal[True] | ||
reveal_type(b"\x82\x83" in b"\x80\x81\x82") # revealed: Literal[False] | ||
|
||
reveal_type(b"ab" not in b"abc") # revealed: Literal[False] | ||
reveal_type(b"ac" not in b"abc") # revealed: Literal[True] | ||
reveal_type(b"ab" not in b"abc") # revealed: Literal[False] | ||
reveal_type(b"ac" not in b"abc") # revealed: Literal[True] | ||
|
||
reveal_type(b"abc" is b"abc") # revealed: bool | ||
reveal_type(b"abc" is b"ab") # revealed: Literal[False] | ||
reveal_type(b"abc" is b"abc") # revealed: bool | ||
reveal_type(b"abc" is b"ab") # revealed: Literal[False] | ||
|
||
reveal_type(b"abc" is not b"abc") # revealed: bool | ||
reveal_type(b"abc" is not b"ab") # revealed: Literal[True] | ||
reveal_type(b"abc" is not b"ab") # revealed: Literal[True] | ||
``` |
42 changes: 14 additions & 28 deletions
42
crates/red_knot_python_semantic/resources/mdtest/comparison/integers.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 |
---|---|---|
@@ -1,41 +1,27 @@ | ||
# Comparing integers | ||
# Comparison: Integers | ||
|
||
## Integer literals | ||
|
||
```py | ||
a = 1 == 1 == True | ||
b = 1 == 1 == 2 == 4 | ||
c = False < True <= 2 < 3 != 6 | ||
d = 1 < 1 | ||
e = 1 > 1 | ||
f = 1 is 1 | ||
g = 1 is not 1 | ||
h = 1 is 2 | ||
i = 1 is not 7 | ||
j = 1 <= "" and 0 < 1 | ||
|
||
reveal_type(a) # revealed: Literal[True] | ||
reveal_type(b) # revealed: Literal[False] | ||
reveal_type(c) # revealed: Literal[True] | ||
reveal_type(d) # revealed: Literal[False] | ||
reveal_type(e) # revealed: Literal[False] | ||
reveal_type(f) # revealed: bool | ||
reveal_type(g) # revealed: bool | ||
reveal_type(h) # revealed: Literal[False] | ||
reveal_type(i) # revealed: Literal[True] | ||
reveal_type(j) # revealed: @Todo | Literal[True] | ||
reveal_type(1 == 1 == True) # revealed: Literal[True] | ||
reveal_type(1 == 1 == 2 == 4) # revealed: Literal[False] | ||
reveal_type(False < True <= 2 < 3 != 6) # revealed: Literal[True] | ||
reveal_type(1 < 1) # revealed: Literal[False] | ||
reveal_type(1 > 1) # revealed: Literal[False] | ||
reveal_type(1 is 1) # revealed: bool | ||
reveal_type(1 is not 1) # revealed: bool | ||
reveal_type(1 is 2) # revealed: Literal[False] | ||
reveal_type(1 is not 7) # revealed: Literal[True] | ||
reveal_type(1 <= "" and 0 < 1) # revealed: @Todo | Literal[True] | ||
``` | ||
|
||
## Integer instance | ||
|
||
```py | ||
# TODO: implement lookup of `__eq__` on typeshed `int` stub. | ||
def int_instance() -> int: ... | ||
a = 1 == int_instance() | ||
b = 9 < int_instance() | ||
c = int_instance() < int_instance() | ||
|
||
reveal_type(a) # revealed: @Todo | ||
reveal_type(b) # revealed: bool | ||
reveal_type(c) # revealed: bool | ||
reveal_type(1 == int_instance()) # revealed: @Todo | ||
reveal_type(9 < int_instance()) # revealed: bool | ||
reveal_type(int_instance() < int_instance()) # revealed: bool | ||
``` |
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
35 changes: 13 additions & 22 deletions
35
crates/red_knot_python_semantic/resources/mdtest/comparison/strings.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 |
---|---|---|
@@ -1,29 +1,20 @@ | ||
# Comparing strings | ||
# Comparison: Strings | ||
|
||
## String literals | ||
|
||
```py | ||
def str_instance() -> str: ... | ||
a = "abc" == "abc" | ||
b = "ab_cd" <= "ab_ce" | ||
c = "abc" in "ab cd" | ||
d = "" not in "hello" | ||
e = "--" is "--" | ||
f = "A" is "B" | ||
g = "--" is not "--" | ||
h = "A" is not "B" | ||
i = str_instance() < "..." | ||
# ensure we're not comparing the interned salsa symbols, which compare by order of declaration. | ||
j = "ab" < "ab_cd" | ||
|
||
reveal_type(a) # revealed: Literal[True] | ||
reveal_type(b) # revealed: Literal[True] | ||
reveal_type(c) # revealed: Literal[False] | ||
reveal_type(d) # revealed: Literal[False] | ||
reveal_type(e) # revealed: bool | ||
reveal_type(f) # revealed: Literal[False] | ||
reveal_type(g) # revealed: bool | ||
reveal_type(h) # revealed: Literal[True] | ||
reveal_type(i) # revealed: bool | ||
reveal_type(j) # revealed: Literal[True] | ||
reveal_type("abc" == "abc") # revealed: Literal[True] | ||
reveal_type("ab_cd" <= "ab_ce") # revealed: Literal[True] | ||
reveal_type("abc" in "ab cd") # revealed: Literal[False] | ||
reveal_type("" not in "hello") # revealed: Literal[False] | ||
reveal_type("--" is "--") # revealed: bool | ||
reveal_type("A" is "B") # revealed: Literal[False] | ||
reveal_type("--" is not "--") # revealed: bool | ||
reveal_type("A" is not "B") # revealed: Literal[True] | ||
reveal_type(str_instance() < "...") # revealed: bool | ||
|
||
# ensure we're not comparing the interned salsa symbols, which compare by order of declaration. | ||
reveal_type("ab" < "ab_cd") # revealed: Literal[True] | ||
``` |
Oops, something went wrong.