diff --git a/argilla/src/argilla/_models/__init__.py b/argilla/src/argilla/_models/__init__.py index 4f69b93024..f6009684f2 100644 --- a/argilla/src/argilla/_models/__init__.py +++ b/argilla/src/argilla/_models/__init__.py @@ -50,7 +50,6 @@ ) from argilla._models._settings._metadata import ( MetadataFieldModel, - MetadataPropertyType, BaseMetadataPropertySettings, TermsMetadataPropertySettings, NumericMetadataPropertySettings, diff --git a/argilla/src/argilla/_models/_settings/_metadata.py b/argilla/src/argilla/_models/_settings/_metadata.py index d29b231cd5..19b43fca78 100644 --- a/argilla/src/argilla/_models/_settings/_metadata.py +++ b/argilla/src/argilla/_models/_settings/_metadata.py @@ -12,7 +12,6 @@ # See the License for the specific language governing permissions and # limitations under the License. -from enum import Enum from typing import List, Literal, Optional, Union, Annotated, Any from uuid import UUID @@ -22,19 +21,12 @@ from argilla._models import ResourceModel -class MetadataPropertyType(str, Enum): - terms = "terms" - integer = "integer" - float = "float" - - class BaseMetadataPropertySettings(BaseModel): - type: MetadataPropertyType visible_for_annotators: Optional[bool] = True class TermsMetadataPropertySettings(BaseMetadataPropertySettings): - type: Literal[MetadataPropertyType.terms] + type: Literal["terms"] = "terms" values: Optional[List[Any]] = None @field_validator("values") @@ -64,7 +56,7 @@ def __validate_min_max(cls, values): class IntegerMetadataPropertySettings(NumericMetadataPropertySettings): - type: Literal[MetadataPropertyType.integer] + type: Literal["integer"] = "integer" @model_validator(mode="before") @classmethod @@ -78,7 +70,7 @@ def __validate_min_max(cls, values): class FloatMetadataPropertySettings(NumericMetadataPropertySettings): - type: Literal[MetadataPropertyType.float] + type: Literal["float"] = "float" MetadataPropertySettings = Annotated[ @@ -97,12 +89,15 @@ class MetadataFieldModel(ResourceModel): name: str settings: MetadataPropertySettings - type: Optional[MetadataPropertyType] = Field(None, validate_default=True) title: Optional[str] = None visible_for_annotators: Optional[bool] = True dataset_id: Optional[UUID] = None + @property + def type(self) -> str: + return self.settings.type + @field_validator("title") @classmethod def __title_default(cls, title, values): @@ -112,10 +107,3 @@ def __title_default(cls, title, values): @field_serializer("id", "dataset_id", when_used="unless-none") def serialize_id(self, value: UUID) -> str: return str(value) - - @field_validator("type", mode="plain") - @classmethod - def __validate_type(cls, type, values): - if type is None: - return values.data["settings"].type - return type diff --git a/argilla/src/argilla/settings/_metadata.py b/argilla/src/argilla/settings/_metadata.py index 73e963c63d..ccc5a90b54 100644 --- a/argilla/src/argilla/settings/_metadata.py +++ b/argilla/src/argilla/settings/_metadata.py @@ -17,7 +17,6 @@ from argilla._api._metadata import MetadataAPI from argilla._exceptions import MetadataError from argilla._models import ( - MetadataPropertyType, TermsMetadataPropertySettings, FloatMetadataPropertySettings, IntegerMetadataPropertySettings, @@ -122,13 +121,12 @@ def __init__( super().__init__(client=client) try: - settings = TermsMetadataPropertySettings(values=options, type=MetadataPropertyType.terms) + settings = TermsMetadataPropertySettings(values=options) except ValueError as e: raise MetadataError(f"Error defining metadata settings for {name}") from e self._model = MetadataFieldModel( name=name, - type=MetadataPropertyType.terms, title=title, settings=settings, visible_for_annotators=visible_for_annotators, @@ -176,13 +174,12 @@ def __init__( super().__init__(client=client) try: - settings = FloatMetadataPropertySettings(min=min, max=max, type=MetadataPropertyType.float) + settings = FloatMetadataPropertySettings(min=min, max=max) except ValueError as e: raise MetadataError(f"Error defining metadata settings for {name}") from e self._model = MetadataFieldModel( name=name, - type=MetadataPropertyType.float, title=title, settings=settings, visible_for_annotators=visible_for_annotators, @@ -237,13 +234,12 @@ def __init__( super().__init__(client=client) try: - settings = IntegerMetadataPropertySettings(min=min, max=max, type=MetadataPropertyType.integer) + settings = IntegerMetadataPropertySettings(min=min, max=max) except ValueError as e: raise MetadataError(f"Error defining metadata settings for {name}") from e self._model = MetadataFieldModel( name=name, - type=MetadataPropertyType.integer, title=title, settings=settings, visible_for_annotators=visible_for_annotators, @@ -283,27 +279,14 @@ def from_model(cls, model: MetadataFieldModel) -> "IntegerMetadataProperty": class MetadataField: @classmethod def from_model(cls, model: MetadataFieldModel) -> MetadataType: - switch = { - MetadataPropertyType.terms: TermsMetadataProperty, - MetadataPropertyType.float: FloatMetadataProperty, - MetadataPropertyType.integer: IntegerMetadataProperty, - } - metadata_type = model.type - try: - return switch[metadata_type].from_model(model) - except KeyError as e: - raise MetadataError(f"Unknown metadata property type: {metadata_type}") from e + if model.type == "terms": + return TermsMetadataProperty.from_model(model) + elif model.type == "float": + return FloatMetadataProperty.from_model(model) + elif model.type == "integer": + return IntegerMetadataProperty.from_model(model) + raise MetadataError(f"Unknown metadata property type: {model.type}") @classmethod def from_dict(cls, data: dict) -> MetadataType: - switch = { - MetadataPropertyType.terms: TermsMetadataProperty, - MetadataPropertyType.float: FloatMetadataProperty, - MetadataPropertyType.integer: IntegerMetadataProperty, - } - metadata_type = data["type"] - try: - metadata_model = MetadataFieldModel(**data) - return switch[metadata_type].from_model(metadata_model) - except KeyError as e: - raise MetadataError(f"Unknown metadata property type: {metadata_type}") from e + return cls.from_model(MetadataFieldModel(**data)) diff --git a/argilla/tests/unit/test_settings/test_metadata.py b/argilla/tests/unit/test_settings/test_metadata.py index e7fca39816..ce9cda3d1b 100644 --- a/argilla/tests/unit/test_settings/test_metadata.py +++ b/argilla/tests/unit/test_settings/test_metadata.py @@ -37,7 +37,6 @@ def test_create_metadata_terms(self, options: list): "name": "metadata", "settings": {"type": "terms", "values": options, "visible_for_annotators": True}, "title": "A metadata property", - "type": "terms", "visible_for_annotators": True, "inserted_at": None, "updated_at": None, @@ -59,7 +58,6 @@ def test_create_terms_metadata_without_options(self): "name": "metadata", "title": "metadata", "settings": {"type": "terms", "values": None, "visible_for_annotators": True}, - "type": "terms", "visible_for_annotators": True, "inserted_at": None, "updated_at": None,