Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 6 additions & 30 deletions crates/ty_python_semantic/resources/mdtest/attributes.md
Original file line number Diff line number Diff line change
Expand Up @@ -300,14 +300,6 @@ reveal_type(c_instance.b) # revealed: Unknown | list[Literal[2, 3]]
#### Attributes defined in for-loop (unpacking)

```py
class IntIterator:
def __next__(self) -> int:
return 1

class IntIterable:
def __iter__(self) -> IntIterator:
return IntIterator()

class TupleIterator:
def __next__(self) -> tuple[int, str]:
return (1, "a")
Expand All @@ -320,7 +312,7 @@ class NonIterable: ...

class C:
def __init__(self):
for self.x in IntIterable():
for self.x in range(3):
pass

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

```py
class IntIterator:
def __next__(self) -> int:
return 1

class IntIterable:
def __iter__(self) -> IntIterator:
return IntIterator()

class TupleIterator:
def __next__(self) -> tuple[int, str]:
return (1, "a")
Expand All @@ -398,19 +382,19 @@ class C:
def __init__(self) -> None:
# TODO: Should not emit this diagnostic
# error: [unresolved-attribute]
[... for self.a in IntIterable()]
[... for self.a in range(3)]
# TODO: Should not emit this diagnostic
# error: [unresolved-attribute]
# error: [unresolved-attribute]
[... for (self.b, self.c) in TupleIterable()]
# TODO: Should not emit this diagnostic
# error: [unresolved-attribute]
# error: [unresolved-attribute]
[... for self.d in IntIterable() for self.e in IntIterable()]
[... for self.d in range(3) for self.e in range(3)]
# TODO: Should not emit this diagnostic
# error: [unresolved-attribute]
[[... for self.f in IntIterable()] for _ in IntIterable()]
[[... for self.g in IntIterable()] for self in [D()]]
[[... for self.f in range(3)] for _ in range(3)]
[[... for self.g in range(3)] for self in [D()]]

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

class IntIterator:
def __next__(self) -> int:
return 42

class IntIterable:
def __iter__(self) -> IntIterator:
return IntIterator()

# error: [invalid-assignment] "Object of type `int` is not assignable to attribute `global_symbol` of type `str`"
for mod.global_symbol in IntIterable():
for mod.global_symbol in range(3):
pass
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,71 +3,47 @@
## Basic comprehensions

```py
class IntIterator:
def __next__(self) -> int:
return 42

class IntIterable:
def __iter__(self) -> IntIterator:
return IntIterator()

# revealed: int
[reveal_type(x) for x in IntIterable()]
[reveal_type(x) for x in range(3)]

class IteratorOfIterables:
def __next__(self) -> IntIterable:
return IntIterable()
class Row:
def __next__(self) -> range:
return range(3)

class IterableOfIterables:
def __iter__(self) -> IteratorOfIterables:
return IteratorOfIterables()
class Table:
def __iter__(self) -> Row:
return Row()

# revealed: tuple[int, IntIterable]
[reveal_type((x, y)) for y in IterableOfIterables() for x in y]
# revealed: tuple[int, range]
[reveal_type((cell, row)) for row in Table() for cell in row]

# revealed: int
{reveal_type(x): 0 for x in IntIterable()}
{reveal_type(x): 0 for x in range(3)}

# revealed: int
{0: reveal_type(x) for x in IntIterable()}
{0: reveal_type(x) for x in range(3)}
```

## Nested comprehension

```py
class IntIterator:
def __next__(self) -> int:
return 42

class IntIterable:
def __iter__(self) -> IntIterator:
return IntIterator()

# revealed: tuple[int, int]
[[reveal_type((x, y)) for x in IntIterable()] for y in IntIterable()]
[[reveal_type((x, y)) for x in range(3)] for y in range(3)]
```

## Comprehension referencing outer comprehension

```py
class IntIterator:
def __next__(self) -> int:
return 42

class IntIterable:
def __iter__(self) -> IntIterator:
return IntIterator()

class IteratorOfIterables:
def __next__(self) -> IntIterable:
return IntIterable()
class Row:
def __next__(self) -> range:
return range(3)

class IterableOfIterables:
def __iter__(self) -> IteratorOfIterables:
return IteratorOfIterables()
class Table:
def __iter__(self) -> Row:
return Row()

# revealed: tuple[int, IntIterable]
[[reveal_type((x, y)) for x in y] for y in IterableOfIterables()]
# revealed: tuple[int, range]
[[reveal_type((cell, row)) for cell in row] for row in Table()]
```

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

class IntIterator:
def __next__(self) -> int:
return 42

class IntIterable:
def __iter__(self) -> IntIterator:
return IntIterator()

# error: [not-iterable] "Object of type `int` is not iterable"
# revealed: tuple[int, Unknown]
[reveal_type((x, z)) for x in IntIterable() for z in x]
[reveal_type((x, z)) for x in range(3) for z in x]
```

## Starred expressions
Expand All @@ -99,16 +67,8 @@ Starred expressions must be iterable
```py
class NotIterable: ...

class Iterator:
def __next__(self) -> int:
return 42

class Iterable:
def __iter__(self) -> Iterator:
return Iterator()

# This is fine:
x = [*Iterable()]
x = [*range(3)]

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

```py
class Iterator:
def __next__(self) -> int:
return 42

class Iterable:
def __iter__(self) -> Iterator:
return Iterator()

async def _():
# error: [not-iterable] "Object of type `Iterable` is not async-iterable"
# error: [not-iterable] "Object of type `range` is not async-iterable"
# revealed: Unknown
[reveal_type(x) async for x in Iterable()]
[reveal_type(x) async for x in range(3)]
```
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
# Comprehensions with invalid syntax

```py
class IntIterator:
def __next__(self) -> int:
return 42

class IntIterable:
def __iter__(self) -> IntIterator:
return IntIterator()

# Missing 'in' keyword.

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

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


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


# Missing iterable
Expand Down
Loading