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

fix upserting enum columns in registry api #510

Merged
merged 4 commits into from
Nov 21, 2024
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
60 changes: 19 additions & 41 deletions src/latch/registry/table.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
import sys
import time
from contextlib import contextmanager
from dataclasses import dataclass, field
from datetime import date, datetime
from enum import Enum
from inspect import isclass
from typing import (
Dict,
Iterator,
Expand All @@ -23,16 +22,6 @@
import gql
import gql.transport.exceptions
import graphql.language as l
import graphql.language.parser as lp
from latch_sdk_gql.execute import execute
from latch_sdk_gql.utils import (
_GqlJsonValue,
_json_value,
_name_node,
_parse_selection,
_var_def_node,
_var_node,
)
from typing_extensions import TypeAlias

from latch.registry.record import NoSuchColumnError, Record
Expand All @@ -56,8 +45,16 @@
)
from latch.types.directory import LatchDir
from latch.types.file import LatchFile
from latch.utils import NotFoundError, current_workspace
from latch_cli.utils import human_readable_time
from latch.utils import NotFoundError
from latch_sdk_gql.execute import execute
from latch_sdk_gql.utils import (
_GqlJsonValue,
_json_value,
_name_node,
_parse_selection,
_var_def_node,
_var_node,
)

from ..types.json import JsonValue

Expand Down Expand Up @@ -400,9 +397,7 @@ class _TableColumnUpsertData:


_TableRecordsMutationData: TypeAlias = Union[
_TableRecordsUpsertData,
_TableRecordsDeleteData,
_TableColumnUpsertData,
_TableRecordsUpsertData, _TableRecordsDeleteData, _TableColumnUpsertData
]


Expand Down Expand Up @@ -435,11 +430,7 @@ class TableUpdate:
"""

_record_mutations: List[_TableRecordsMutationData] = field(
default_factory=list,
init=False,
repr=False,
hash=False,
compare=False,
default_factory=list, init=False, repr=False, hash=False, compare=False
)

table: Table
Expand Down Expand Up @@ -559,10 +550,7 @@ def _add_record_deletes_selection(

args = l.ArgumentNode()
args.name = _name_node("input")
args.value = _json_value({
"argExperimentId": self.table.id,
"argNames": names,
})
args.value = _json_value({"argExperimentId": self.table.id, "argNames": names})

res.alias = _name_node(f"upd{len(mutations)}")
res.arguments = tuple([args])
Expand All @@ -572,11 +560,7 @@ def _add_record_deletes_selection(
# upsert column

def upsert_column(
self,
key: str,
type: RegistryPythonType,
*,
required: bool = False,
self, key: str, type: RegistryPythonType, *, required: bool = False
):
"""Create a column. Support for updating columns is planned.

Expand Down Expand Up @@ -658,14 +642,11 @@ def upsert_column(
key, type, f"Enum value {repr(x)} is not a string"
)

registry_type = {
"primitive": "enum",
"members": members,
}
registry_type = {"primitive": "enum", "members": members}

if isinstance(type, Enum):
if isclass(type) and issubclass(type, Enum):
members: List[str] = []
for f in cast(Type[Enum], type):
for f in type:
if not isinstance(f.value, str):
raise InvalidColumnTypeError(
key,
Expand All @@ -676,10 +657,7 @@ def upsert_column(

members.append(f.value)

registry_type = {
"primitive": "enum",
"members": members,
}
registry_type = {"primitive": "enum", "members": members}

if registry_type is None:
raise InvalidColumnTypeError(key, type, "Unsupported type")
Expand Down
Loading
Loading