Skip to content

Commit 918358a

Browse files
authored
Migrate some inference tests to mdtests (#14795)
As part of #13696, this PR ports a smallish number of inference tests over to the mdtest framework.
1 parent b01a651 commit 918358a

File tree

8 files changed

+139
-248
lines changed

8 files changed

+139
-248
lines changed

crates/red_knot_python_semantic/resources/mdtest/assignment/unbound.md

-40
Original file line numberDiff line numberDiff line change
@@ -18,43 +18,3 @@ Note: in this particular example, one could argue that the most likely error wou
1818
of the `x`/`foo` definitions, and so it could be desirable to infer `Literal[1]` for the type of
1919
`x`. On the other hand, there might be a variable `fob` a little higher up in this file, and the
2020
actual error might have been just a typo. Inferring `Unknown` thus seems like the safest option.
21-
22-
## Unbound class variable
23-
24-
Name lookups within a class scope fall back to globals, but lookups of class attributes don't.
25-
26-
```py
27-
def bool_instance() -> bool:
28-
return True
29-
30-
flag = bool_instance()
31-
x = 1
32-
33-
class C:
34-
y = x
35-
if flag:
36-
x = 2
37-
38-
# error: [possibly-unbound-attribute] "Attribute `x` on type `Literal[C]` is possibly unbound"
39-
reveal_type(C.x) # revealed: Literal[2]
40-
reveal_type(C.y) # revealed: Literal[1]
41-
```
42-
43-
## Possibly unbound in class and global scope
44-
45-
```py
46-
def bool_instance() -> bool:
47-
return True
48-
49-
if bool_instance():
50-
x = "abc"
51-
52-
class C:
53-
if bool_instance():
54-
x = 1
55-
56-
# error: [possibly-unresolved-reference]
57-
y = x
58-
59-
reveal_type(C.y) # revealed: Literal[1] | Literal["abc"]
60-
```

crates/red_knot_python_semantic/resources/mdtest/import/errors.md

+21
Original file line numberDiff line numberDiff line change
@@ -55,3 +55,24 @@ from b import x
5555

5656
x = "foo" # error: [invalid-assignment] "Object of type `Literal["foo"]"
5757
```
58+
59+
## Import cycle
60+
61+
```py path=a.py
62+
class A: ...
63+
64+
reveal_type(A.__mro__) # revealed: tuple[Literal[A], Literal[object]]
65+
import b
66+
67+
class C(b.B): ...
68+
69+
reveal_type(C.__mro__) # revealed: tuple[Literal[C], Literal[B], Literal[A], Literal[object]]
70+
```
71+
72+
```py path=b.py
73+
from a import A
74+
75+
class B(A): ...
76+
77+
reveal_type(B.__mro__) # revealed: tuple[Literal[B], Literal[A], Literal[object]]
78+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Invalid syntax
2+
3+
## Missing module name
4+
5+
```py
6+
from import bar # error: [invalid-syntax]
7+
8+
reveal_type(bar) # revealed: Unknown
9+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
# Ellipsis literals
2+
3+
## Simple
4+
5+
```py
6+
reveal_type(...) # revealed: EllipsisType | ellipsis
7+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Nonlocal references
2+
3+
## One level up
4+
5+
```py
6+
def f():
7+
x = 1
8+
def g():
9+
reveal_type(x) # revealed: Literal[1]
10+
```
11+
12+
## Two levels up
13+
14+
```py
15+
def f():
16+
x = 1
17+
def g():
18+
def h():
19+
reveal_type(x) # revealed: Literal[1]
20+
```
21+
22+
## Skips class scope
23+
24+
```py
25+
def f():
26+
x = 1
27+
28+
class C:
29+
x = 2
30+
def g():
31+
reveal_type(x) # revealed: Literal[1]
32+
```
33+
34+
## Skips annotation-only assignment
35+
36+
```py
37+
def f():
38+
x = 1
39+
def g():
40+
# it's pretty weird to have an annotated assignment in a function where the
41+
# name is otherwise not defined; maybe should be an error?
42+
x: int
43+
def h():
44+
reveal_type(x) # revealed: Literal[1]
45+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# Unbound
2+
3+
## Unbound class variable
4+
5+
Name lookups within a class scope fall back to globals, but lookups of class attributes don't.
6+
7+
```py
8+
def bool_instance() -> bool:
9+
return True
10+
11+
flag = bool_instance()
12+
x = 1
13+
14+
class C:
15+
y = x
16+
if flag:
17+
x = 2
18+
19+
# error: [possibly-unbound-attribute] "Attribute `x` on type `Literal[C]` is possibly unbound"
20+
reveal_type(C.x) # revealed: Literal[2]
21+
reveal_type(C.y) # revealed: Literal[1]
22+
```
23+
24+
## Possibly unbound in class and global scope
25+
26+
```py
27+
def bool_instance() -> bool:
28+
return True
29+
30+
if bool_instance():
31+
x = "abc"
32+
33+
class C:
34+
if bool_instance():
35+
x = 1
36+
37+
# error: [possibly-unresolved-reference]
38+
y = x
39+
40+
reveal_type(C.y) # revealed: Literal[1] | Literal["abc"]
41+
```
42+
43+
## Unbound function local
44+
45+
An unbound function local that has definitions in the scope does not fall back to globals.
46+
47+
```py
48+
x = 1
49+
50+
def f():
51+
# error: [unresolved-reference]
52+
# revealed: Unknown
53+
reveal_type(x)
54+
x = 2
55+
# revealed: Literal[2]
56+
reveal_type(x)
57+
```

0 commit comments

Comments
 (0)