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

misc: define add_scalar alias #821

Merged
merged 2 commits into from
Dec 13, 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
29 changes: 29 additions & 0 deletions encord/metadata_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,35 @@ def add_scalar(
)
self._dirty = True

def set_scalar(
self,
k: str,
*,
data_type: Union[
Literal["boolean", "datetime", "number", "uuid", "varchar", "text", "string", "long_string"],
MetadataSchemaScalarType,
],
) -> None:
"""
Sets a simple metadata type for a given key in the schema.

Alias of add_scalar

**Parameters:**

- k : str: The key for which the metadata type is being set.
- data_type : Literal["boolean", "datetime", "number", "uuid", "varchar", "text", "string", "long_string"]
The type of metadata to be associated with the key. Must be a valid identifier.
"string" is an alias of "varchar"
"long_string" is an alias of "text"

**Raises:**

MetadataSchemaError: If the key `k` is already defined in the schema with a conflicting type.
ValueError: If `data_type` is not a valid type of metadata identifier.
"""
self.add_scalar(k, data_type=data_type)

def delete_key(self, k: str, *, hard: bool = False) -> None:
"""
Delete a metadata key from the schema.
Expand Down
2 changes: 2 additions & 0 deletions tests/test_metadata_schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ def test_metadata_schema() -> None:
meta.add_scalar("g", data_type="number")
meta.add_scalar("g", data_type="text")
meta.add_scalar("g", data_type="number")
meta.set_scalar("g", data_type="text")
meta.set_scalar("g", data_type="number")

assert meta.has_key("g")
assert not meta.has_key("g2")
Expand Down
Loading