---
source: crates/red_knot_test/src/lib.rs
expression: snapshot
---
---
mdtest name: for.md - For loops - Union type as iterable where one union element has no `__iter__` method
mdtest path: crates/red_knot_python_semantic/resources/mdtest/loops/for.md
---

# Python source files

## mdtest_snippet.py

```
 1 | from typing_extensions import reveal_type
 2 | 
 3 | class TestIter:
 4 |     def __next__(self) -> int:
 5 |         return 42
 6 | 
 7 | class Test:
 8 |     def __iter__(self) -> TestIter:
 9 |         return TestIter()
10 | 
11 | def _(flag: bool):
12 |     # error: [not-iterable]
13 |     for x in Test() if flag else 42:
14 |         reveal_type(x)  # revealed: int
```

# Diagnostics

```
error: lint:not-iterable
  --> /src/mdtest_snippet.py:13:14
   |
11 | def _(flag: bool):
12 |     # error: [not-iterable]
13 |     for x in Test() if flag else 42:
   |              ^^^^^^^^^^^^^^^^^^^^^^ Object of type `Test | Literal[42]` may not be iterable because it may not have an `__iter__` method and it doesn't have a `__getitem__` method
14 |         reveal_type(x)  # revealed: int
   |

```

```
info: revealed-type
  --> /src/mdtest_snippet.py:14:9
   |
12 |     # error: [not-iterable]
13 |     for x in Test() if flag else 42:
14 |         reveal_type(x)  # revealed: int
   |         ^^^^^^^^^^^^^^ Revealed type is `int`
   |

```
