Skip to content

Commit 15394a8

Browse files
authored
[red-knot] MDTests: Do not depend on precise public-symbol type inference (#15691)
## Summary Another small PR to focus #15674 solely on the relevant changes. This makes our Markdown tests less dependent on precise types of public symbols, without actually changing anything semantically in these tests. Best reviewed using ignore-whitespace-mode. ## Test Plan Tested these changes on `main` and on the branch from #15674.
1 parent fc2ebea commit 15394a8

File tree

9 files changed

+155
-157
lines changed

9 files changed

+155
-157
lines changed

crates/red_knot_python_semantic/resources/mdtest/binary/booleans.md

Lines changed: 40 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -50,46 +50,44 @@ reveal_type(b | b) # revealed: Literal[False]
5050
## Arithmetic with a variable
5151

5252
```py
53-
a = True
54-
b = False
55-
56-
def lhs_is_int(x: int):
57-
reveal_type(x + a) # revealed: int
58-
reveal_type(x - a) # revealed: int
59-
reveal_type(x * a) # revealed: int
60-
reveal_type(x // a) # revealed: int
61-
reveal_type(x / a) # revealed: float
62-
reveal_type(x % a) # revealed: int
63-
64-
def rhs_is_int(x: int):
65-
reveal_type(a + x) # revealed: int
66-
reveal_type(a - x) # revealed: int
67-
reveal_type(a * x) # revealed: int
68-
reveal_type(a // x) # revealed: int
69-
reveal_type(a / x) # revealed: float
70-
reveal_type(a % x) # revealed: int
71-
72-
def lhs_is_bool(x: bool):
73-
reveal_type(x + a) # revealed: int
74-
reveal_type(x - a) # revealed: int
75-
reveal_type(x * a) # revealed: int
76-
reveal_type(x // a) # revealed: int
77-
reveal_type(x / a) # revealed: float
78-
reveal_type(x % a) # revealed: int
79-
80-
def rhs_is_bool(x: bool):
81-
reveal_type(a + x) # revealed: int
82-
reveal_type(a - x) # revealed: int
83-
reveal_type(a * x) # revealed: int
84-
reveal_type(a // x) # revealed: int
85-
reveal_type(a / x) # revealed: float
86-
reveal_type(a % x) # revealed: int
87-
88-
def both_are_bool(x: bool, y: bool):
89-
reveal_type(x + y) # revealed: int
90-
reveal_type(x - y) # revealed: int
91-
reveal_type(x * y) # revealed: int
92-
reveal_type(x // y) # revealed: int
93-
reveal_type(x / y) # revealed: float
94-
reveal_type(x % y) # revealed: int
53+
def _(a: bool):
54+
def lhs_is_int(x: int):
55+
reveal_type(x + a) # revealed: int
56+
reveal_type(x - a) # revealed: int
57+
reveal_type(x * a) # revealed: int
58+
reveal_type(x // a) # revealed: int
59+
reveal_type(x / a) # revealed: float
60+
reveal_type(x % a) # revealed: int
61+
62+
def rhs_is_int(x: int):
63+
reveal_type(a + x) # revealed: int
64+
reveal_type(a - x) # revealed: int
65+
reveal_type(a * x) # revealed: int
66+
reveal_type(a // x) # revealed: int
67+
reveal_type(a / x) # revealed: float
68+
reveal_type(a % x) # revealed: int
69+
70+
def lhs_is_bool(x: bool):
71+
reveal_type(x + a) # revealed: int
72+
reveal_type(x - a) # revealed: int
73+
reveal_type(x * a) # revealed: int
74+
reveal_type(x // a) # revealed: int
75+
reveal_type(x / a) # revealed: float
76+
reveal_type(x % a) # revealed: int
77+
78+
def rhs_is_bool(x: bool):
79+
reveal_type(a + x) # revealed: int
80+
reveal_type(a - x) # revealed: int
81+
reveal_type(a * x) # revealed: int
82+
reveal_type(a // x) # revealed: int
83+
reveal_type(a / x) # revealed: float
84+
reveal_type(a % x) # revealed: int
85+
86+
def both_are_bool(x: bool, y: bool):
87+
reveal_type(x + y) # revealed: int
88+
reveal_type(x - y) # revealed: int
89+
reveal_type(x * y) # revealed: int
90+
reveal_type(x // y) # revealed: int
91+
reveal_type(x / y) # revealed: float
92+
reveal_type(x % y) # revealed: int
9593
```

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ reveal_type(a.b) # revealed: <module 'a.b'>
99
```
1010

1111
```py path=a/__init__.py
12-
b = 42
12+
b: int = 42
1313
```
1414

1515
```py path=a/b.py
@@ -20,11 +20,11 @@ b = 42
2020
```py
2121
from a import b
2222

23-
reveal_type(b) # revealed: Literal[42]
23+
reveal_type(b) # revealed: int
2424
```
2525

2626
```py path=a/__init__.py
27-
b = 42
27+
b: int = 42
2828
```
2929

3030
```py path=a/b.py
@@ -41,7 +41,7 @@ reveal_type(a.b) # revealed: <module 'a.b'>
4141
```
4242

4343
```py path=a/__init__.py
44-
b = 42
44+
b: int = 42
4545
```
4646

4747
```py path=a/b.py
@@ -60,13 +60,13 @@ sees the submodule as the value of `b` instead of the integer.
6060
from a import b
6161
import a.b
6262

63-
# Python would say `Literal[42]` for `b`
63+
# Python would say `int` for `b`
6464
reveal_type(b) # revealed: <module 'a.b'>
6565
reveal_type(a.b) # revealed: <module 'a.b'>
6666
```
6767

6868
```py path=a/__init__.py
69-
b = 42
69+
b: int = 42
7070
```
7171

7272
```py path=a/b.py

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ from a import b.c
2020

2121
# TODO: Should these be inferred as Unknown?
2222
reveal_type(b) # revealed: <module 'a.b'>
23-
reveal_type(b.c) # revealed: Literal[1]
23+
reveal_type(b.c) # revealed: int
2424
```
2525

2626
```py path=a/__init__.py
2727
```
2828

2929
```py path=a/b.py
30-
c = 1
30+
c: int = 1
3131
```

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@ reveal_type(X) # revealed: Unknown
1717
```
1818

1919
```py path=package/foo.py
20-
X = 42
20+
X: int = 42
2121
```
2222

2323
```py path=package/bar.py
2424
from .foo import X
2525

26-
reveal_type(X) # revealed: Literal[42]
26+
reveal_type(X) # revealed: int
2727
```
2828

2929
## Dotted
@@ -32,25 +32,25 @@ reveal_type(X) # revealed: Literal[42]
3232
```
3333

3434
```py path=package/foo/bar/baz.py
35-
X = 42
35+
X: int = 42
3636
```
3737

3838
```py path=package/bar.py
3939
from .foo.bar.baz import X
4040

41-
reveal_type(X) # revealed: Literal[42]
41+
reveal_type(X) # revealed: int
4242
```
4343

4444
## Bare to package
4545

4646
```py path=package/__init__.py
47-
X = 42
47+
X: int = 42
4848
```
4949

5050
```py path=package/bar.py
5151
from . import X
5252

53-
reveal_type(X) # revealed: Literal[42]
53+
reveal_type(X) # revealed: int
5454
```
5555

5656
## Non-existent + bare to package
@@ -66,11 +66,11 @@ reveal_type(X) # revealed: Unknown
6666
```py path=package/__init__.py
6767
from .foo import X
6868

69-
reveal_type(X) # revealed: Literal[42]
69+
reveal_type(X) # revealed: int
7070
```
7171

7272
```py path=package/foo.py
73-
X = 42
73+
X: int = 42
7474
```
7575

7676
## Non-existent + dunder init
@@ -87,13 +87,13 @@ reveal_type(X) # revealed: Unknown
8787
```
8888

8989
```py path=package/foo.py
90-
X = 42
90+
X: int = 42
9191
```
9292

9393
```py path=package/subpackage/subsubpackage/bar.py
9494
from ...foo import X
9595

96-
reveal_type(X) # revealed: Literal[42]
96+
reveal_type(X) # revealed: int
9797
```
9898

9999
## Unbound symbol
@@ -117,13 +117,13 @@ reveal_type(x) # revealed: Unknown
117117
```
118118

119119
```py path=package/foo.py
120-
X = 42
120+
X: int = 42
121121
```
122122

123123
```py path=package/bar.py
124124
from . import foo
125125

126-
reveal_type(foo.X) # revealed: Literal[42]
126+
reveal_type(foo.X) # revealed: int
127127
```
128128

129129
## Non-existent + bare to module
@@ -152,7 +152,7 @@ submodule via the attribute on its parent package.
152152
```
153153

154154
```py path=package/foo.py
155-
X = 42
155+
X: int = 42
156156
```
157157

158158
```py path=package/bar.py

crates/red_knot_python_semantic/resources/mdtest/scopes/builtin.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ def returns_bool() -> bool:
1010
return True
1111

1212
if returns_bool():
13-
chr = 1
13+
chr: int = 1
1414

1515
def f():
16-
reveal_type(chr) # revealed: Literal[chr] | Literal[1]
16+
reveal_type(chr) # revealed: Literal[chr] | int
1717
```
1818

1919
## Conditionally global or builtin, with annotation

crates/red_knot_python_semantic/resources/mdtest/statically_known_branches.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ version:
1111
import sys
1212

1313
if sys.version_info >= (3, 9):
14-
SomeFeature = "available"
14+
SomeFeature: str = "available"
1515
```
1616

1717
If we can statically determine that the condition is always true, then we can also understand that
@@ -21,7 +21,7 @@ If we can statically determine that the condition is always true, then we can al
2121
from module1 import SomeFeature
2222

2323
# SomeFeature is unconditionally available here, because we are on Python 3.9 or newer:
24-
reveal_type(SomeFeature) # revealed: Literal["available"]
24+
reveal_type(SomeFeature) # revealed: str
2525
```
2626

2727
Another scenario where this is useful is for `typing.TYPE_CHECKING` branches, which are often used

crates/red_knot_python_semantic/resources/mdtest/subscript/bytes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def _(n: int):
3131
## Slices
3232

3333
```py
34-
b = b"\x00abc\xff"
34+
b: bytes = b"\x00abc\xff"
3535

3636
reveal_type(b[0:2]) # revealed: Literal[b"\x00a"]
3737
reveal_type(b[-3:]) # revealed: Literal[b"bc\xff"]

crates/red_knot_python_semantic/resources/mdtest/subscript/string.md

Lines changed: 45 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -28,52 +28,52 @@ def _(n: int):
2828
## Slices
2929

3030
```py
31-
s = "abcde"
32-
33-
reveal_type(s[0:0]) # revealed: Literal[""]
34-
reveal_type(s[0:1]) # revealed: Literal["a"]
35-
reveal_type(s[0:2]) # revealed: Literal["ab"]
36-
reveal_type(s[0:5]) # revealed: Literal["abcde"]
37-
reveal_type(s[0:6]) # revealed: Literal["abcde"]
38-
reveal_type(s[1:3]) # revealed: Literal["bc"]
39-
40-
reveal_type(s[-3:5]) # revealed: Literal["cde"]
41-
reveal_type(s[-4:-2]) # revealed: Literal["bc"]
42-
reveal_type(s[-10:10]) # revealed: Literal["abcde"]
43-
44-
reveal_type(s[0:]) # revealed: Literal["abcde"]
45-
reveal_type(s[2:]) # revealed: Literal["cde"]
46-
reveal_type(s[5:]) # revealed: Literal[""]
47-
reveal_type(s[:2]) # revealed: Literal["ab"]
48-
reveal_type(s[:0]) # revealed: Literal[""]
49-
reveal_type(s[:2]) # revealed: Literal["ab"]
50-
reveal_type(s[:10]) # revealed: Literal["abcde"]
51-
reveal_type(s[:]) # revealed: Literal["abcde"]
52-
53-
reveal_type(s[::-1]) # revealed: Literal["edcba"]
54-
reveal_type(s[::2]) # revealed: Literal["ace"]
55-
reveal_type(s[-2:-5:-1]) # revealed: Literal["dcb"]
56-
reveal_type(s[::-2]) # revealed: Literal["eca"]
57-
reveal_type(s[-1::-3]) # revealed: Literal["eb"]
58-
59-
reveal_type(s[None:2:None]) # revealed: Literal["ab"]
60-
reveal_type(s[1:None:1]) # revealed: Literal["bcde"]
61-
reveal_type(s[None:None:None]) # revealed: Literal["abcde"]
62-
63-
start = 1
64-
stop = None
65-
step = 2
66-
reveal_type(s[start:stop:step]) # revealed: Literal["bd"]
67-
68-
reveal_type(s[False:True]) # revealed: Literal["a"]
69-
reveal_type(s[True:3]) # revealed: Literal["bc"]
70-
71-
s[0:4:0] # error: [zero-stepsize-in-slice]
72-
s[:4:0] # error: [zero-stepsize-in-slice]
73-
s[0::0] # error: [zero-stepsize-in-slice]
74-
s[::0] # error: [zero-stepsize-in-slice]
75-
7631
def _(m: int, n: int, s2: str):
32+
s = "abcde"
33+
34+
reveal_type(s[0:0]) # revealed: Literal[""]
35+
reveal_type(s[0:1]) # revealed: Literal["a"]
36+
reveal_type(s[0:2]) # revealed: Literal["ab"]
37+
reveal_type(s[0:5]) # revealed: Literal["abcde"]
38+
reveal_type(s[0:6]) # revealed: Literal["abcde"]
39+
reveal_type(s[1:3]) # revealed: Literal["bc"]
40+
41+
reveal_type(s[-3:5]) # revealed: Literal["cde"]
42+
reveal_type(s[-4:-2]) # revealed: Literal["bc"]
43+
reveal_type(s[-10:10]) # revealed: Literal["abcde"]
44+
45+
reveal_type(s[0:]) # revealed: Literal["abcde"]
46+
reveal_type(s[2:]) # revealed: Literal["cde"]
47+
reveal_type(s[5:]) # revealed: Literal[""]
48+
reveal_type(s[:2]) # revealed: Literal["ab"]
49+
reveal_type(s[:0]) # revealed: Literal[""]
50+
reveal_type(s[:2]) # revealed: Literal["ab"]
51+
reveal_type(s[:10]) # revealed: Literal["abcde"]
52+
reveal_type(s[:]) # revealed: Literal["abcde"]
53+
54+
reveal_type(s[::-1]) # revealed: Literal["edcba"]
55+
reveal_type(s[::2]) # revealed: Literal["ace"]
56+
reveal_type(s[-2:-5:-1]) # revealed: Literal["dcb"]
57+
reveal_type(s[::-2]) # revealed: Literal["eca"]
58+
reveal_type(s[-1::-3]) # revealed: Literal["eb"]
59+
60+
reveal_type(s[None:2:None]) # revealed: Literal["ab"]
61+
reveal_type(s[1:None:1]) # revealed: Literal["bcde"]
62+
reveal_type(s[None:None:None]) # revealed: Literal["abcde"]
63+
64+
start = 1
65+
stop = None
66+
step = 2
67+
reveal_type(s[start:stop:step]) # revealed: Literal["bd"]
68+
69+
reveal_type(s[False:True]) # revealed: Literal["a"]
70+
reveal_type(s[True:3]) # revealed: Literal["bc"]
71+
72+
s[0:4:0] # error: [zero-stepsize-in-slice]
73+
s[:4:0] # error: [zero-stepsize-in-slice]
74+
s[0::0] # error: [zero-stepsize-in-slice]
75+
s[::0] # error: [zero-stepsize-in-slice]
76+
7777
substring1 = s[m:n]
7878
# TODO: Support overloads... Should be `LiteralString`
7979
reveal_type(substring1) # revealed: @Todo(return type)

0 commit comments

Comments
 (0)