Skip to content

Commit

Permalink
[red-knot] Porting to new structure after remarks
Browse files Browse the repository at this point in the history
  • Loading branch information
Lexxxzy committed Oct 15, 2024
1 parent a600909 commit 44d6372
Show file tree
Hide file tree
Showing 56 changed files with 1,919 additions and 2,027 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Assignment with annotations

## Annotation only transparent to local inference

```py
x = 1
x: int
y = x

reveal_type(y) # revealed: Literal[1]
```

## Violates own annotation

```py
x: int = 'foo' # error: [invalid-assignment] "Object of type `Literal["foo"]` is not assignable to `int`"

```

## Violates previous annotation

```py
x: int
x = 'foo' # error: [invalid-assignment] "Object of type `Literal["foo"]` is not assignable to `int`"
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# Class attributes assignment

## Union of attributes

```py
if flag:
class C:
x = 1
else:
class C:
x = 2

y = C.x
reveal_type(y) # revealed: Literal[1, 2]
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Errors while declaring

## Violates previous assignment

```py
x = 1
x: str # error: [invalid-declaration] "Cannot declare type `str` for inferred type `Literal[1]`"
```

## Incompatible declarations

```py
if flag:
x: str
else:
x: int
x = 1 # error: [conflicting-declarations] "Conflicting declared types for `x`: str, int"
```

## Partial declarations

```py
if flag:
x: int
x = 1 # error: [conflicting-declarations] "Conflicting declared types for `x`: Unknown, int"
```

## Incompatible declarations with bad assignment

```py
if flag:
x: str
else:
x: int

# error: [conflicting-declarations]
# error: [invalid-assignment]
x = b'foo'
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Multi-target assignment

## Basic

```py
x = y = 1
reveal_type(x) # revealed: Literal[1]
reveal_type(y) # revealed: Literal[1]
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Unbound

## Maybe unbound

```py
if flag:
y = 3
x = y
reveal_type(x) # revealed: Unbound | Literal[3]
```

## Unbound

```py
x = foo; foo = 1
reveal_type(x) # revealed: Unbound
```

## Unbound class variable

Class variables can reference global variables unless overridden within the class scope.

```py
x = 1
class C:
y = x
if flag:
x = 2

reveal_type(C.x) # revealed: Unbound | Literal[2]
reveal_type(C.y) # revealed: Literal[1]
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Walrus operator

## Basic

```py
x = (y := 1) + 1
reveal_type(x) # revealed: Literal[2]
reveal_type(y) # revealed: Literal[1]
```

## Walrus self-addition

```py
x = 0
(x := x + 1)
reveal_type(x) # revealed: Literal[1]
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
## 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]
```

## Division by Zero

```py
# TODO: `a` should be `int` and `e` should be `float` once we support inference.
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"
d = int() / 0 # error: "Cannot divide object of type `int` by zero"
e = 1.0 / 0 # error: "Cannot divide object of type `float` by zero"

reveal_type(a) # revealed: float
reveal_type(b) # revealed: int
reveal_type(c) # revealed: int
reveal_type(d) # revealed: @Todo
reveal_type(e) # revealed: @Todo
```
Loading

0 comments on commit 44d6372

Please sign in to comment.