Skip to content

Commit 13e2165

Browse files
jaycee-licopybara-github
authored andcommitted
fix: Print error for schema classes
PiperOrigin-RevId: 487277666
1 parent fafb0e2 commit 13e2165

File tree

4 files changed

+370
-6
lines changed

4 files changed

+370
-6
lines changed

google/cloud/aiplatform/metadata/schema/base_artifact.py

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,9 @@
1717

1818
import abc
1919

20-
from typing import Optional, Dict
20+
from typing import Any, Optional, Dict
2121

2222
from google.auth import credentials as auth_credentials
23-
2423
from google.cloud.aiplatform.compat.types import artifact as gca_artifact
2524
from google.cloud.aiplatform.metadata import artifact
2625
from google.cloud.aiplatform.constants import base as base_constants
@@ -58,7 +57,7 @@ def __init__(
5857
various structure and field requirements for the metadata field.
5958
6059
Args:
61-
resource_id (str):
60+
artifact_id (str):
6261
Optional. The <resource_id> portion of the Artifact name with
6362
the following format, this is globally unique in a metadataStore:
6463
projects/123/locations/us-central1/metadataStores/<metadata_store_id>/artifacts/<resource_id>.
@@ -82,6 +81,8 @@ def __init__(
8281
Pipelines), and the system does not prescribe or
8382
check the validity of state transitions.
8483
"""
84+
# initialize the exception to resolve the FutureManager exception.
85+
self._exception = None
8586
# resource_id is not stored in the proto. Create method uses the
8687
# resource_id along with project_id and location to construct an
8788
# resource_name which is stored in the proto message.
@@ -178,3 +179,53 @@ def create(
178179
# Reinstantiate this class using the newly created resource.
179180
self._init_with_resource_name(artifact_name=new_artifact_instance.resource_name)
180181
return self
182+
183+
def sync_resource(self):
184+
"""Syncs local resource with the resource in metadata store.
185+
186+
Raises:
187+
RuntimeError: if the artifact resource hasn't been created.
188+
"""
189+
if self._gca_resource.name:
190+
super().sync_resource()
191+
else:
192+
raise RuntimeError(
193+
f"{self.__class__.__name__} resource has not been created."
194+
)
195+
196+
def update(
197+
self,
198+
metadata: Optional[Dict[str, Any]] = None,
199+
description: Optional[str] = None,
200+
credentials: Optional[auth_credentials.Credentials] = None,
201+
):
202+
"""Updates an existing Artifact resource with new metadata.
203+
204+
Args:
205+
metadata (Dict):
206+
Optional. metadata contains the updated metadata information.
207+
description (str):
208+
Optional. Description describes the resource to be updated.
209+
credentials (auth_credentials.Credentials):
210+
Custom credentials to use to update this resource. Overrides
211+
credentials set in aiplatform.init.
212+
213+
Raises:
214+
RuntimeError: if the artifact resource hasn't been created.
215+
"""
216+
if self._gca_resource.name:
217+
super().update(
218+
metadata=metadata,
219+
description=description,
220+
credentials=credentials,
221+
)
222+
else:
223+
raise RuntimeError(
224+
f"{self.__class__.__name__} resource has not been created."
225+
)
226+
227+
def __repr__(self) -> str:
228+
if self._gca_resource.name:
229+
return super().__repr__()
230+
else:
231+
return f"{object.__repr__(self)}\nschema_title: {self.schema_title}"

google/cloud/aiplatform/metadata/schema/base_context.py

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@
1717

1818
import abc
1919

20-
from typing import Optional, Dict
20+
from typing import Dict, List, Optional, Sequence
2121

2222
from google.auth import credentials as auth_credentials
2323

2424
from google.cloud.aiplatform.compat.types import context as gca_context
25+
from google.cloud.aiplatform.compat.types import (
26+
lineage_subgraph as gca_lineage_subgraph,
27+
)
2528
from google.cloud.aiplatform.constants import base as base_constants
2629
from google.cloud.aiplatform.metadata import constants
2730
from google.cloud.aiplatform.metadata import context
@@ -64,6 +67,8 @@ def __init__(
6467
description (str):
6568
Optional. Describes the purpose of the Context to be created.
6669
"""
70+
# initialize the exception to resolve the FutureManager exception.
71+
self._exception = None
6772
# resource_id is not stored in the proto. Create method uses the
6873
# resource_id along with project_id and location to construct an
6974
# resource_name which is stored in the proto message.
@@ -154,3 +159,70 @@ def create(
154159
# Reinstantiate this class using the newly created resource.
155160
self._init_with_resource_name(context_name=new_context.resource_name)
156161
return self
162+
163+
def add_artifacts_and_executions(
164+
self,
165+
artifact_resource_names: Optional[Sequence[str]] = None,
166+
execution_resource_names: Optional[Sequence[str]] = None,
167+
):
168+
"""Associate Executions and attribute Artifacts to a given Context.
169+
170+
Args:
171+
artifact_resource_names (Sequence[str]):
172+
Optional. The full resource name of Artifacts to attribute to
173+
the Context.
174+
execution_resource_names (Sequence[str]):
175+
Optional. The full resource name of Executions to associate with
176+
the Context.
177+
178+
Raises:
179+
RuntimeError: if Context resource hasn't been created.
180+
"""
181+
if self._gca_resource.name:
182+
super().add_artifacts_and_executions(
183+
artifact_resource_names=artifact_resource_names,
184+
execution_resource_names=execution_resource_names,
185+
)
186+
else:
187+
raise RuntimeError(
188+
f"{self.__class__.__name__} resource has not been created."
189+
)
190+
191+
def add_context_children(self, contexts: List[context.Context]):
192+
"""Adds the provided contexts as children of this context.
193+
194+
Args:
195+
contexts (List[_Context]): Contexts to add as children.
196+
197+
Raises:
198+
RuntimeError: if Context resource hasn't been created.
199+
"""
200+
if self._gca_resource.name:
201+
super().add_context_children(contexts)
202+
else:
203+
raise RuntimeError(
204+
f"{self.__class__.__name__} resource has not been created."
205+
)
206+
207+
def query_lineage_subgraph(self) -> gca_lineage_subgraph.LineageSubgraph:
208+
"""Queries lineage subgraph of this context.
209+
210+
Returns:
211+
lineage subgraph(gca_lineage_subgraph.LineageSubgraph):
212+
Lineage subgraph of this Context.
213+
214+
Raises:
215+
RuntimeError: if Context resource hasn't been created.
216+
"""
217+
if self._gca_resource.name:
218+
return super().query_lineage_subgraph()
219+
else:
220+
raise RuntimeError(
221+
f"{self.__class__.__name__} resource has not been created."
222+
)
223+
224+
def __repr__(self) -> str:
225+
if self._gca_resource.name:
226+
return super().__repr__()
227+
else:
228+
return f"{object.__repr__(self)}\nschema_title: {self.schema_title}"

google/cloud/aiplatform/metadata/schema/base_execution.py

Lines changed: 112 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@
1717

1818
import abc
1919

20-
from typing import Optional, Dict
20+
from typing import Any, Dict, List, Optional, Union
2121

2222
from google.auth import credentials as auth_credentials
2323

24+
from google.cloud.aiplatform import models
2425
from google.cloud.aiplatform.compat.types import execution as gca_execution
2526
from google.cloud.aiplatform.constants import base as base_constants
27+
from google.cloud.aiplatform.metadata import artifact
2628
from google.cloud.aiplatform.metadata import constants
2729
from google.cloud.aiplatform.metadata import execution
2830
from google.cloud.aiplatform.metadata import metadata
@@ -70,7 +72,8 @@ def __init__(
7072
description (str):
7173
Optional. Describes the purpose of the Execution to be created.
7274
"""
73-
75+
# initialize the exception to resolve the FutureManager exception.
76+
self._exception = None
7477
# resource_id is not stored in the proto. Create method uses the
7578
# resource_id along with project_id and location to construct an
7679
# resource_name which is stored in the proto message.
@@ -249,3 +252,110 @@ def start_execution(
249252
execution_name=new_execution_instance.resource_name
250253
)
251254
return self
255+
256+
def assign_input_artifacts(
257+
self, artifacts: List[Union[artifact.Artifact, models.Model]]
258+
):
259+
"""Assigns Artifacts as inputs to this Executions.
260+
261+
Args:
262+
artifacts (List[Union[artifact.Artifact, models.Model]]):
263+
Required. Artifacts to assign as input.
264+
265+
Raises:
266+
RuntimeError: if Execution resource hasn't been created.
267+
"""
268+
if self._gca_resource.name:
269+
super().assign_input_artifacts(artifacts)
270+
else:
271+
raise RuntimeError(
272+
f"{self.__class__.__name__} resource has not been created."
273+
)
274+
275+
def assign_output_artifacts(
276+
self, artifacts: List[Union[artifact.Artifact, models.Model]]
277+
):
278+
"""Assigns Artifacts as outputs to this Executions.
279+
280+
Args:
281+
artifacts (List[Union[artifact.Artifact, models.Model]]):
282+
Required. Artifacts to assign as input.
283+
284+
Raises:
285+
RuntimeError: if Execution resource hasn't been created.
286+
"""
287+
if self._gca_resource.name:
288+
super().assign_output_artifacts(artifacts)
289+
else:
290+
raise RuntimeError(
291+
f"{self.__class__.__name__} resource has not been created."
292+
)
293+
294+
def get_input_artifacts(self) -> List[artifact.Artifact]:
295+
"""Get the input Artifacts of this Execution.
296+
297+
Returns:
298+
List of input Artifacts.
299+
300+
Raises:
301+
RuntimeError: if Execution resource hasn't been created.
302+
"""
303+
if self._gca_resource.name:
304+
return super().get_input_artifacts()
305+
else:
306+
raise RuntimeError(
307+
f"{self.__class__.__name__} resource has not been created."
308+
)
309+
310+
def get_output_artifacts(self) -> List[artifact.Artifact]:
311+
"""Get the output Artifacts of this Execution.
312+
313+
Returns:
314+
List of output Artifacts.
315+
316+
Raises:
317+
RuntimeError: if Execution resource hasn't been created.
318+
"""
319+
if self._gca_resource.name:
320+
return super().get_output_artifacts()
321+
else:
322+
raise RuntimeError(
323+
f"{self.__class__.__name__} resource has not been created."
324+
)
325+
326+
def update(
327+
self,
328+
state: Optional[gca_execution.Execution.State] = None,
329+
description: Optional[str] = None,
330+
metadata: Optional[Dict[str, Any]] = None,
331+
):
332+
"""Update this Execution.
333+
334+
Args:
335+
state (gca_execution.Execution.State):
336+
Optional. State of this Execution.
337+
description (str):
338+
Optional. Describes the purpose of the Execution to be created.
339+
metadata (Dict[str, Any):
340+
Optional. Contains the metadata information that will be stored
341+
in the Execution.
342+
343+
Raises:
344+
RuntimeError: if Execution resource hasn't been created.
345+
"""
346+
if self._gca_resource.name:
347+
super().update(
348+
state=state,
349+
description=description,
350+
metadata=metadata,
351+
)
352+
else:
353+
raise RuntimeError(
354+
f"{self.__class__.__name__} resource has not been created."
355+
)
356+
357+
def __repr__(self) -> str:
358+
if self._gca_resource.name:
359+
return super().__repr__()
360+
else:
361+
return f"{object.__repr__(self)}\nschema_title: {self.schema_title}"

0 commit comments

Comments
 (0)