Skip to content

Commit a649f15

Browse files
committed
Make Source explicitly implement __iter__()
Source was previously iterable because it implements `__getitem__()`, which is apparently a thing from before `__iter__()` was introduced. To reduce mypy's and my own confusion, implement `__iter__()` directly.
1 parent 307add0 commit a649f15

File tree

1 file changed

+4
-0
lines changed

1 file changed

+4
-0
lines changed

src/_pytest/_code/source.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
from ast import PyCF_ONLY_AST as _AST_FLAG
99
from bisect import bisect_right
1010
from types import FrameType
11+
from typing import Iterator
1112
from typing import List
1213
from typing import Optional
1314
from typing import Sequence
@@ -73,6 +74,9 @@ def __getitem__(self, key: Union[int, slice]) -> Union[str, "Source"]: # noqa:
7374
newsource.lines = self.lines[key.start : key.stop]
7475
return newsource
7576

77+
def __iter__(self) -> Iterator[str]:
78+
return iter(self.lines)
79+
7680
def __len__(self) -> int:
7781
return len(self.lines)
7882

0 commit comments

Comments
 (0)