Skip to content

Commit c9ba060

Browse files
authored
fix: Don't throw exception when getting representation of unrun GCA objects (#1071)
1 parent be0ccc4 commit c9ba060

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

google/cloud/aiplatform/base.py

+10-1
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,15 @@ def gca_resource(self) -> proto.Message:
655655
self._assert_gca_resource_is_available()
656656
return self._gca_resource
657657

658+
@property
659+
def _resource_is_available(self) -> bool:
660+
"""Returns True if GCA resource has been created and is available, otherwise False"""
661+
try:
662+
self._assert_gca_resource_is_available()
663+
return True
664+
except RuntimeError:
665+
return False
666+
658667
def _assert_gca_resource_is_available(self) -> None:
659668
"""Helper method to raise when property is not accessible.
660669
@@ -1153,7 +1162,7 @@ def delete(self, sync: bool = True) -> None:
11531162
_LOGGER.log_action_completed_against_resource("deleted.", "", self)
11541163

11551164
def __repr__(self) -> str:
1156-
if self._gca_resource:
1165+
if self._gca_resource and self._resource_is_available:
11571166
return VertexAiResourceNoun.__repr__(self)
11581167

11591168
return FutureManager.__repr__(self)

tests/unit/aiplatform/test_custom_job.py

+35
Original file line numberDiff line numberDiff line change
@@ -661,3 +661,38 @@ def test_create_from_local_script_with_all_args(
661661
assert (
662662
job._gca_resource.state == gca_job_state_compat.JobState.JOB_STATE_SUCCEEDED
663663
)
664+
665+
@pytest.mark.usefixtures("get_custom_job_mock", "create_custom_job_mock")
666+
def test_check_custom_job_availability(self):
667+
aiplatform.init(
668+
project=_TEST_PROJECT,
669+
location=_TEST_LOCATION,
670+
staging_bucket=_TEST_STAGING_BUCKET,
671+
encryption_spec_key_name=_TEST_DEFAULT_ENCRYPTION_KEY_NAME,
672+
)
673+
674+
job = aiplatform.CustomJob(
675+
display_name=_TEST_DISPLAY_NAME,
676+
worker_pool_specs=_TEST_WORKER_POOL_SPEC,
677+
base_output_dir=_TEST_BASE_OUTPUT_DIR,
678+
labels=_TEST_LABELS,
679+
)
680+
681+
assert not job._resource_is_available
682+
assert job.__repr__().startswith(
683+
"<google.cloud.aiplatform.jobs.CustomJob object"
684+
)
685+
686+
job.run(
687+
service_account=_TEST_SERVICE_ACCOUNT,
688+
network=_TEST_NETWORK,
689+
timeout=_TEST_TIMEOUT,
690+
restart_job_on_worker_restart=_TEST_RESTART_JOB_ON_WORKER_RESTART,
691+
)
692+
693+
job.wait_for_resource_creation()
694+
695+
assert job._resource_is_available
696+
assert "resource name" in job.__repr__()
697+
698+
job.wait()

0 commit comments

Comments
 (0)