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: restore trait value coercion to string #179

Merged
merged 1 commit into from
Jul 4, 2023
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
12 changes: 11 additions & 1 deletion flag_engine/identities/traits/models.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
from pydantic import BaseModel
from typing import Any, get_args

from pydantic import BaseModel, validator

from flag_engine.identities.traits.types import TraitValue

TRAIT_VALUE_TYPES = get_args(TraitValue)


class TraitModel(BaseModel):
trait_key: str
trait_value: TraitValue = ...

@validator("trait_value", pre=True)
def convert_trait_value(cls, value: Any) -> TraitValue:
if isinstance(value, TRAIT_VALUE_TYPES):
return value
return str(value)

class Config:
smart_union: bool = True
4 changes: 4 additions & 0 deletions tests/unit/identities/test_identities_models.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Any

import pytest

from flag_engine.features.models import FeatureModel, FeatureStateModel
Expand Down Expand Up @@ -186,6 +187,9 @@ def test_get_hash_key_with_use_identity_composite_key_for_hashing_disabled(ident
(False, False),
(0.0, 0.0),
(0, 0),
(None, None),
([], "[]"),
(["SUPERADMIN"], "['SUPERADMIN']"),
],
)
def test_trait_model__deserialize__expected_trait_value(
Expand Down