Skip to content

Commit

Permalink
Merge pull request #5493 from grondo/fix-to_dict
Browse files Browse the repository at this point in the history
python: fix handling of JobInfo properties and `to_dict()` method with missing attributes
  • Loading branch information
mergify[bot] authored Oct 10, 2023
2 parents 4943070 + f2a2482 commit e79a7e2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 4 deletions.
15 changes: 11 additions & 4 deletions src/bindings/python/flux/job/info.py
Original file line number Diff line number Diff line change
Expand Up @@ -406,10 +406,14 @@ def get_runtime(self):
return runtime

def get_remaining_time(self):
status = str(self.status)
if status != "RUN":
try:
status = str(self.status)
if status != "RUN":
return 0.0
tleft = self.expiration - time.time()
except (KeyError, AttributeError):
# expiration and/or status attributes may not exist, return 0.0
return 0.0
tleft = self.expiration - time.time()
if tleft < 0.0:
return 0.0
return tleft
Expand Down Expand Up @@ -541,7 +545,10 @@ def to_dict(self, filtered=True):
"""
result = {}
for attr in chain(self.defaults.keys(), self.properties):
val = getattr(self, attr)
try:
val = getattr(self, attr)
except AttributeError:
val = None
if val is not None:
result[attr] = val

Expand Down
20 changes: 20 additions & 0 deletions t/python/t0010-job.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,6 +629,18 @@ def cancel_on_start(future, jobid):
}
),
)

# t_remaining returns 0 from JobInfo returned from result():
self.assertEqual(result[ids[0]].get_info().t_remaining, 0.0)

# Ensure to_dict() works for a JobInfo returned from result():
job_dict = result[ids[0]].get_info().to_dict()
self.assertIsInstance(job_dict, dict)
self.assertEqual(job_dict["id"], ids[0])
self.assertEqual(job_dict["result"], "COMPLETED")
self.assertEqual(job_dict["returncode"], 0)
self.assertEqual(job_dict["duration"], 0.0)

self.assertEqual(
result[ids[1]].get_info(),
JobInfo(
Expand All @@ -643,6 +655,14 @@ def cancel_on_start(future, jobid):
}
),
)
# Ensure to_dict() works for a JobInfo returned from result():
job_dict = result[ids[1]].get_info().to_dict()
self.assertIsInstance(job_dict, dict)
self.assertEqual(job_dict["id"], ids[1])
self.assertEqual(job_dict["result"], "FAILED")
self.assertEqual(job_dict["returncode"], 1)
self.assertEqual(job_dict["duration"], 0.0)

self.assertEqual(
result[ids[2]].get_info(),
JobInfo(
Expand Down

0 comments on commit e79a7e2

Please sign in to comment.