Skip to content

Commit 3eb4546

Browse files
authored
[red-knot] Format mdtest Python snippets more concisely (#13905)
1 parent 77ae0cc commit 3eb4546

33 files changed

+4
-193
lines changed

.pre-commit-config.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ repos:
4646
)$
4747
4848
- repo: https://github.com/adamchainz/blacken-docs
49-
rev: 1.19.0
49+
rev: 1.19.1
5050
hooks:
5151
- id: blacken-docs
52-
args: ["--line-length", "130"]
52+
args: ["--pyi", "--line-length", "130"]
5353
files: '^crates/.*/resources/mdtest/.*\.md'
5454
additional_dependencies:
5555
- black==24.10.0

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

-2
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@ Name lookups within a class scope fall back to globals, but lookups of class att
1515
```py
1616
x = 1
1717

18-
1918
class C:
2019
y = x
2120
if flag:
2221
x = 2
2322

24-
2523
reveal_type(C.x) # revealed: Literal[2]
2624
reveal_type(C.y) # revealed: Literal[1]
2725
```

crates/red_knot_python_semantic/resources/mdtest/attributes.md

-3
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,12 @@
44

55
```py
66
if flag:
7-
87
class C:
98
x = 1
109

1110
else:
12-
1311
class C:
1412
x = 2
1513

16-
1714
reveal_type(C.x) # revealed: Literal[1, 2]
1815
```

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

-35
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,8 @@ class A:
5353
def __or__(self, other) -> A:
5454
return self
5555

56-
5756
class B: ...
5857

59-
6058
reveal_type(A() + B()) # revealed: A
6159
reveal_type(A() - B()) # revealed: A
6260
reveal_type(A() * B()) # revealed: A
@@ -117,10 +115,8 @@ class A:
117115
def __ror__(self, other) -> A:
118116
return self
119117

120-
121118
class B: ...
122119

123-
124120
reveal_type(B() + A()) # revealed: A
125121
reveal_type(B() - A()) # revealed: A
126122
reveal_type(B() * A()) # revealed: A
@@ -148,10 +144,8 @@ class A:
148144
def __rsub__(self, other) -> int:
149145
return 1
150146

151-
152147
class B: ...
153148

154-
155149
reveal_type(A() + B()) # revealed: int
156150
reveal_type(B() - A()) # revealed: int
157151
```
@@ -167,15 +161,12 @@ class A:
167161
def __add__(self, other: B) -> int:
168162
return 42
169163

170-
171164
class B:
172165
def __radd__(self, other: A) -> str:
173166
return "foo"
174167

175-
176168
reveal_type(A() + B()) # revealed: int
177169

178-
179170
# Edge case: C is a subtype of C, *but* if the two sides are of *equal* types,
180171
# the lhs *still* takes precedence
181172
class C:
@@ -185,7 +176,6 @@ class C:
185176
def __radd__(self, other: C) -> str:
186177
return "foo"
187178

188-
189179
reveal_type(C() + C()) # revealed: int
190180
```
191181

@@ -203,22 +193,17 @@ class A:
203193
def __radd__(self, other) -> str:
204194
return "foo"
205195

206-
207196
class MyString(str): ...
208197

209-
210198
class B(A):
211199
def __radd__(self, other) -> MyString:
212200
return MyString()
213201

214-
215202
reveal_type(A() + B()) # revealed: MyString
216203

217-
218204
# N.B. Still a subtype of `A`, even though `A` does not appear directly in the class's `__bases__`
219205
class C(B): ...
220206

221-
222207
# TODO: we currently only understand direct subclasses as subtypes of the superclass.
223208
# We need to iterate through the full MRO rather than just the class's bases;
224209
# if we do, we'll understand `C` as a subtype of `A`, and correctly understand this as being
@@ -240,10 +225,8 @@ class A:
240225
def __radd__(self, other) -> int:
241226
return 42
242227

243-
244228
class B(A): ...
245229

246-
247230
reveal_type(A() + B()) # revealed: str
248231
```
249232

@@ -266,12 +249,10 @@ class A:
266249
def __sub__(self, other: A) -> A:
267250
return A()
268251

269-
270252
class B:
271253
def __rsub__(self, other: A) -> B:
272254
return B()
273255

274-
275256
# TODO: this should be `B` (the return annotation of `B.__rsub__`),
276257
# because `A.__sub__` is annotated as only accepting `A`,
277258
# but `B.__rsub__` will accept `A`.
@@ -287,11 +268,9 @@ class A:
287268
def __call__(self, other) -> int:
288269
return 42
289270

290-
291271
class B:
292272
__add__ = A()
293273

294-
295274
reveal_type(B() + B()) # revealed: int
296275
```
297276

@@ -311,15 +290,12 @@ reveal_type(42 + 4.2) # revealed: int
311290
# TODO should be complex, need to check arg type and fall back to `rhs.__radd__`
312291
reveal_type(3 + 3j) # revealed: int
313292

314-
315293
def returns_int() -> int:
316294
return 42
317295

318-
319296
def returns_bool() -> bool:
320297
return True
321298

322-
323299
x = returns_bool()
324300
y = returns_int()
325301

@@ -343,7 +319,6 @@ class A:
343319
def __radd__(self, other) -> A:
344320
return self
345321

346-
347322
reveal_type(A() + 1) # revealed: A
348323
# TODO should be `A` since `int.__add__` doesn't support `A` instances
349324
reveal_type(1 + A()) # revealed: int
@@ -388,15 +363,12 @@ from does_not_exist import Foo # error: [unresolved-import]
388363

389364
reveal_type(Foo) # revealed: Unknown
390365

391-
392366
class X:
393367
def __add__(self, other: object) -> int:
394368
return 42
395369

396-
397370
class Y(Foo): ...
398371

399-
400372
# TODO: Should be `int | Unknown`; see above discussion.
401373
reveal_type(X() + Y()) # revealed: int
402374
```
@@ -411,12 +383,10 @@ The magic method must exist on the class, not just on the instance:
411383
def add_impl(self, other) -> int:
412384
return 1
413385

414-
415386
class A:
416387
def __init__(self):
417388
self.__add__ = add_impl
418389

419-
420390
# error: [unsupported-operator] "Operator `+` is unsupported between objects of type `A` and `A`"
421391
# revealed: Unknown
422392
reveal_type(A() + A())
@@ -427,7 +397,6 @@ reveal_type(A() + A())
427397
```py
428398
class A: ...
429399

430-
431400
# error: [unsupported-operator]
432401
# revealed: Unknown
433402
reveal_type(A() + A())
@@ -441,14 +410,11 @@ A left-hand dunder method doesn't apply for the right-hand operand, or vice vers
441410
class A:
442411
def __add__(self, other) -> int: ...
443412

444-
445413
class B:
446414
def __radd__(self, other) -> int: ...
447415

448-
449416
class C: ...
450417

451-
452418
# error: [unsupported-operator]
453419
# revealed: Unknown
454420
reveal_type(C() + A())
@@ -471,7 +437,6 @@ class Foo:
471437
def __radd__(self, other: Foo) -> Foo:
472438
return self
473439

474-
475440
# error: [unsupported-operator]
476441
# revealed: Unknown
477442
reveal_type(Foo() + Foo())

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

-2
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,8 @@ bool(1) / False
6262
# revealed: float
6363
reveal_type(1.0 / 0)
6464

65-
6665
class MyInt(int): ...
6766

68-
6967
# No error for a subclass of int
7068
# revealed: float
7169
reveal_type(MyInt(3) / 0)

crates/red_knot_python_semantic/resources/mdtest/call/callable_instance.md

-3
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,11 @@ class Multiplier:
1010
def __call__(self, number: float) -> float:
1111
return number * self.factor
1212

13-
1413
a = Multiplier(2.0)(3.0)
1514
reveal_type(a) # revealed: float
1615

17-
1816
class Unit: ...
1917

20-
2118
b = Unit()(3.0) # error: "Object of type `Unit` is not callable"
2219
reveal_type(b) # revealed: Unknown
2320
```

crates/red_knot_python_semantic/resources/mdtest/call/constructor.md

-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,5 @@
33
```py
44
class Foo: ...
55

6-
76
reveal_type(Foo()) # revealed: Foo
87
```

crates/red_knot_python_semantic/resources/mdtest/call/function.md

-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
def get_int() -> int:
77
return 42
88

9-
109
reveal_type(get_int()) # revealed: int
1110
```
1211

@@ -16,7 +15,6 @@ reveal_type(get_int()) # revealed: int
1615
async def get_int_async() -> int:
1716
return 42
1817

19-
2018
# TODO: we don't yet support `types.CoroutineType`, should be generic `Coroutine[Any, Any, int]`
2119
reveal_type(get_int_async()) # revealed: @Todo
2220
```
@@ -26,20 +24,16 @@ reveal_type(get_int_async()) # revealed: @Todo
2624
```py
2725
from typing import Callable
2826

29-
3027
def foo() -> int:
3128
return 42
3229

33-
3430
def decorator(func) -> Callable[[], int]:
3531
return foo
3632

37-
3833
@decorator
3934
def bar() -> str:
4035
return "bar"
4136

42-
4337
# TODO: should reveal `int`, as the decorator replaces `bar` with `foo`
4438
reveal_type(bar()) # revealed: @Todo
4539
```

crates/red_knot_python_semantic/resources/mdtest/call/union.md

-4
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ else:
1313
def f() -> str:
1414
return "foo"
1515

16-
1716
reveal_type(f()) # revealed: int | str
1817
```
1918

@@ -27,7 +26,6 @@ if flag:
2726
def f() -> int:
2827
return 1
2928

30-
3129
reveal_type(f()) # revealed: Unknown | int
3230
```
3331

@@ -43,7 +41,6 @@ else:
4341
def f() -> int:
4442
return 1
4543

46-
4744
x = f() # error: "Object of type `Literal[1] | Literal[f]` is not callable (due to union element `Literal[1]`)"
4845
reveal_type(x) # revealed: Unknown | int
4946
```
@@ -62,7 +59,6 @@ else:
6259
def f() -> int:
6360
return 1
6461

65-
6662
# error: "Object of type `Literal[1] | Literal["foo"] | Literal[f]` is not callable (due to union elements Literal[1], Literal["foo"])"
6763
# revealed: Unknown | int
6864
reveal_type(f())

crates/red_knot_python_semantic/resources/mdtest/comparison/integers.md

-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ reveal_type(1 <= "" and 0 < 1) # revealed: @Todo | Literal[True]
2121
# TODO: implement lookup of `__eq__` on typeshed `int` stub.
2222
def int_instance() -> int: ...
2323

24-
2524
reveal_type(1 == int_instance()) # revealed: @Todo
2625
reveal_type(9 < int_instance()) # revealed: bool
2726
reveal_type(int_instance() < int_instance()) # revealed: bool

crates/red_knot_python_semantic/resources/mdtest/comparison/non_boolean_returns.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,15 @@ Walking through examples:
2121
```py
2222
from __future__ import annotations
2323

24-
2524
class A:
2625
def __lt__(self, other) -> A: ...
26+
2727
class B:
2828
def __lt__(self, other) -> B: ...
29+
2930
class C:
3031
def __lt__(self, other) -> C: ...
3132

32-
3333
x = A() < B() < C()
3434
reveal_type(x) # revealed: A | B
3535

crates/red_knot_python_semantic/resources/mdtest/comparison/strings.md

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
```py
66
def str_instance() -> str: ...
77

8-
98
reveal_type("abc" == "abc") # revealed: Literal[True]
109
reveal_type("ab_cd" <= "ab_ce") # revealed: Literal[True]
1110
reveal_type("abc" in "ab cd") # revealed: Literal[False]

0 commit comments

Comments
 (0)