Skip to content

Commit 1b0ee46

Browse files
authored
[ty] Use range instead of custom IntIterable (#21138)
## Summary We previously didn't understand `range` and wrote these custom `IntIterable`/`IntIterator` classes for tests. We can now remove them and make the tests shorter in some places.
1 parent 1ebedf6 commit 1b0ee46

File tree

3 files changed

+32
-112
lines changed

3 files changed

+32
-112
lines changed

crates/ty_python_semantic/resources/mdtest/attributes.md

Lines changed: 6 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -300,14 +300,6 @@ reveal_type(c_instance.b) # revealed: Unknown | list[Literal[2, 3]]
300300
#### Attributes defined in for-loop (unpacking)
301301

302302
```py
303-
class IntIterator:
304-
def __next__(self) -> int:
305-
return 1
306-
307-
class IntIterable:
308-
def __iter__(self) -> IntIterator:
309-
return IntIterator()
310-
311303
class TupleIterator:
312304
def __next__(self) -> tuple[int, str]:
313305
return (1, "a")
@@ -320,7 +312,7 @@ class NonIterable: ...
320312

321313
class C:
322314
def __init__(self):
323-
for self.x in IntIterable():
315+
for self.x in range(3):
324316
pass
325317

326318
for _, self.y in TupleIterable():
@@ -378,14 +370,6 @@ reveal_type(c_instance.y) # revealed: Unknown | int
378370
#### Attributes defined in comprehensions
379371

380372
```py
381-
class IntIterator:
382-
def __next__(self) -> int:
383-
return 1
384-
385-
class IntIterable:
386-
def __iter__(self) -> IntIterator:
387-
return IntIterator()
388-
389373
class TupleIterator:
390374
def __next__(self) -> tuple[int, str]:
391375
return (1, "a")
@@ -398,19 +382,19 @@ class C:
398382
def __init__(self) -> None:
399383
# TODO: Should not emit this diagnostic
400384
# error: [unresolved-attribute]
401-
[... for self.a in IntIterable()]
385+
[... for self.a in range(3)]
402386
# TODO: Should not emit this diagnostic
403387
# error: [unresolved-attribute]
404388
# error: [unresolved-attribute]
405389
[... for (self.b, self.c) in TupleIterable()]
406390
# TODO: Should not emit this diagnostic
407391
# error: [unresolved-attribute]
408392
# error: [unresolved-attribute]
409-
[... for self.d in IntIterable() for self.e in IntIterable()]
393+
[... for self.d in range(3) for self.e in range(3)]
410394
# TODO: Should not emit this diagnostic
411395
# error: [unresolved-attribute]
412-
[[... for self.f in IntIterable()] for _ in IntIterable()]
413-
[[... for self.g in IntIterable()] for self in [D()]]
396+
[[... for self.f in range(3)] for _ in range(3)]
397+
[[... for self.g in range(3)] for self in [D()]]
414398

415399
class D:
416400
g: int
@@ -2058,16 +2042,8 @@ mod.global_symbol = 1
20582042
# TODO: this should be an error, but we do not understand list unpackings yet.
20592043
[_, mod.global_symbol] = [1, 2]
20602044

2061-
class IntIterator:
2062-
def __next__(self) -> int:
2063-
return 42
2064-
2065-
class IntIterable:
2066-
def __iter__(self) -> IntIterator:
2067-
return IntIterator()
2068-
20692045
# error: [invalid-assignment] "Object of type `int` is not assignable to attribute `global_symbol` of type `str`"
2070-
for mod.global_symbol in IntIterable():
2046+
for mod.global_symbol in range(3):
20712047
pass
20722048
```
20732049

crates/ty_python_semantic/resources/mdtest/comprehensions/basic.md

Lines changed: 24 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -3,71 +3,47 @@
33
## Basic comprehensions
44

55
```py
6-
class IntIterator:
7-
def __next__(self) -> int:
8-
return 42
9-
10-
class IntIterable:
11-
def __iter__(self) -> IntIterator:
12-
return IntIterator()
13-
146
# revealed: int
15-
[reveal_type(x) for x in IntIterable()]
7+
[reveal_type(x) for x in range(3)]
168

17-
class IteratorOfIterables:
18-
def __next__(self) -> IntIterable:
19-
return IntIterable()
9+
class Row:
10+
def __next__(self) -> range:
11+
return range(3)
2012

21-
class IterableOfIterables:
22-
def __iter__(self) -> IteratorOfIterables:
23-
return IteratorOfIterables()
13+
class Table:
14+
def __iter__(self) -> Row:
15+
return Row()
2416

25-
# revealed: tuple[int, IntIterable]
26-
[reveal_type((x, y)) for y in IterableOfIterables() for x in y]
17+
# revealed: tuple[int, range]
18+
[reveal_type((cell, row)) for row in Table() for cell in row]
2719

2820
# revealed: int
29-
{reveal_type(x): 0 for x in IntIterable()}
21+
{reveal_type(x): 0 for x in range(3)}
3022

3123
# revealed: int
32-
{0: reveal_type(x) for x in IntIterable()}
24+
{0: reveal_type(x) for x in range(3)}
3325
```
3426

3527
## Nested comprehension
3628

3729
```py
38-
class IntIterator:
39-
def __next__(self) -> int:
40-
return 42
41-
42-
class IntIterable:
43-
def __iter__(self) -> IntIterator:
44-
return IntIterator()
45-
4630
# revealed: tuple[int, int]
47-
[[reveal_type((x, y)) for x in IntIterable()] for y in IntIterable()]
31+
[[reveal_type((x, y)) for x in range(3)] for y in range(3)]
4832
```
4933

5034
## Comprehension referencing outer comprehension
5135

5236
```py
53-
class IntIterator:
54-
def __next__(self) -> int:
55-
return 42
56-
57-
class IntIterable:
58-
def __iter__(self) -> IntIterator:
59-
return IntIterator()
60-
61-
class IteratorOfIterables:
62-
def __next__(self) -> IntIterable:
63-
return IntIterable()
37+
class Row:
38+
def __next__(self) -> range:
39+
return range(3)
6440

65-
class IterableOfIterables:
66-
def __iter__(self) -> IteratorOfIterables:
67-
return IteratorOfIterables()
41+
class Table:
42+
def __iter__(self) -> Row:
43+
return Row()
6844

69-
# revealed: tuple[int, IntIterable]
70-
[[reveal_type((x, y)) for x in y] for y in IterableOfIterables()]
45+
# revealed: tuple[int, range]
46+
[[reveal_type((cell, row)) for cell in row] for row in Table()]
7147
```
7248

7349
## Comprehension with unbound iterable
@@ -79,17 +55,9 @@ Iterating over an unbound iterable yields `Unknown`:
7955
# revealed: Unknown
8056
[reveal_type(z) for z in x]
8157

82-
class IntIterator:
83-
def __next__(self) -> int:
84-
return 42
85-
86-
class IntIterable:
87-
def __iter__(self) -> IntIterator:
88-
return IntIterator()
89-
9058
# error: [not-iterable] "Object of type `int` is not iterable"
9159
# revealed: tuple[int, Unknown]
92-
[reveal_type((x, z)) for x in IntIterable() for z in x]
60+
[reveal_type((x, z)) for x in range(3) for z in x]
9361
```
9462

9563
## Starred expressions
@@ -99,16 +67,8 @@ Starred expressions must be iterable
9967
```py
10068
class NotIterable: ...
10169

102-
class Iterator:
103-
def __next__(self) -> int:
104-
return 42
105-
106-
class Iterable:
107-
def __iter__(self) -> Iterator:
108-
return Iterator()
109-
11070
# This is fine:
111-
x = [*Iterable()]
71+
x = [*range(3)]
11272

11373
# error: [not-iterable] "Object of type `NotIterable` is not iterable"
11474
y = [*NotIterable()]
@@ -138,16 +98,8 @@ This tests that we understand that `async` comprehensions do *not* work accordin
13898
iteration protocol
13999

140100
```py
141-
class Iterator:
142-
def __next__(self) -> int:
143-
return 42
144-
145-
class Iterable:
146-
def __iter__(self) -> Iterator:
147-
return Iterator()
148-
149101
async def _():
150-
# error: [not-iterable] "Object of type `Iterable` is not async-iterable"
102+
# error: [not-iterable] "Object of type `range` is not async-iterable"
151103
# revealed: Unknown
152-
[reveal_type(x) async for x in Iterable()]
104+
[reveal_type(x) async for x in range(3)]
153105
```

crates/ty_python_semantic/resources/mdtest/comprehensions/invalid_syntax.md

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,14 @@
11
# Comprehensions with invalid syntax
22

33
```py
4-
class IntIterator:
5-
def __next__(self) -> int:
6-
return 42
7-
8-
class IntIterable:
9-
def __iter__(self) -> IntIterator:
10-
return IntIterator()
11-
124
# Missing 'in' keyword.
135

146
# It's reasonably clear here what they *meant* to write,
157
# so we'll still infer the correct type:
168

179
# error: [invalid-syntax] "Expected 'in', found name"
1810
# revealed: int
19-
[reveal_type(a) for a IntIterable()]
11+
[reveal_type(a) for a range(3)]
2012

2113

2214
# Missing iteration variable
@@ -25,7 +17,7 @@ class IntIterable:
2517
# error: [invalid-syntax] "Expected 'in', found name"
2618
# error: [unresolved-reference]
2719
# revealed: Unknown
28-
[reveal_type(b) for in IntIterable()]
20+
[reveal_type(b) for in range(3)]
2921

3022

3123
# Missing iterable

0 commit comments

Comments
 (0)