Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

python: fix handling of JobInfo properties and to_dict() method with missing attributes #5493

Merged
merged 3 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading