Skip to content

Commit 4962b35

Browse files
authored
Fix iterating through ObjectStoragePath (apache#57156)
1 parent 2ed2739 commit 4962b35

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

task-sdk/src/airflow/sdk/io/path.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ def wrapper(*args, **kwargs):
6060

6161
return wrapper
6262

63+
# We need to explicitly implement `__iter__`
64+
# because otherwise python iteration logic could use `__getitem__`
65+
def __iter__(self):
66+
return iter(self._obj)
67+
6368
def __getitem__(self, key):
6469
# Intercept item access
6570
return self._obj[key]

task-sdk/tests/task_sdk/io/test_path.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,15 @@ def test_read_write(self, target):
215215
assert o.open("rb").read() == b"foo"
216216
o.unlink()
217217

218+
def test_read_line_by_line(self, target):
219+
o = ObjectStoragePath(f"file://{target}")
220+
with o.open("wb") as f:
221+
f.write(b"foo\nbar\n")
222+
with o.open("rb") as f:
223+
lines = list(f)
224+
assert lines == [b"foo\n", b"bar\n"]
225+
o.unlink()
226+
218227
def test_stat(self, target):
219228
o = ObjectStoragePath(f"file://{target}")
220229
assert o.stat().st_size == 0

0 commit comments

Comments
 (0)