Skip to content

Commit 48611de

Browse files
committed
Merge branch 'vb/job_getitem' into vb/flow_decorator
2 parents 61d43c9 + 30b06c1 commit 48611de

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

src/jobflow/core/job.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,24 @@ def __repr__(self):
394394
name, uuid = self.name, self.uuid
395395
return f"Job({name=}, {uuid=})"
396396

397+
def __getitem__(self, key: Any) -> OutputReference:
398+
"""
399+
Get the corresponding `OutputReference` for the `Job`.
400+
401+
This is for when it is indexed like a dictionary or list.
402+
403+
Parameters
404+
----------
405+
key
406+
The index/key.
407+
408+
Returns
409+
-------
410+
OutputReference
411+
The equivalent of `Job.output[k]`
412+
"""
413+
return self.output[key]
414+
397415
def __contains__(self, item: Hashable) -> bool:
398416
"""
399417
Check if the job contains a reference to a given UUID.

tests/core/test_flow.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1353,3 +1353,23 @@ def test_flow_repr():
13531353
assert len(lines) == len(flow_repr)
13541354
for expected, line in zip(lines, flow_repr):
13551355
assert line.startswith(expected), f"{line=} doesn't start with {expected=}"
1356+
1357+
1358+
def test_get_item():
1359+
from jobflow import Flow, job, run_locally
1360+
1361+
@job
1362+
def make_str(s):
1363+
return {"hello": s}
1364+
1365+
@job
1366+
def capitalize(s):
1367+
return s.upper()
1368+
1369+
job1 = make_str("world")
1370+
job2 = capitalize(job1["hello"])
1371+
1372+
flow = Flow([job1, job2])
1373+
1374+
responses = run_locally(flow, ensure_success=True)
1375+
assert responses[job2.uuid][1].output == "WORLD"

0 commit comments

Comments
 (0)