Skip to content
Open
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
2 changes: 1 addition & 1 deletion api/environments/sdk/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ def get_transient_identifier(sdk_trait_data: list[SDKTraitData]) -> str:
if sdk_trait_data:
return hashlib.sha256(
"".join(
f"{trait['trait_key']}{trait_value['value']}"
f"{trait['trait_key']}{trait_value['value']}" # type: ignore[index]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate that we're removing more type: ignores than we're adding, but I'm interested why we have to add this one here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the type for trait_value is not considered narrowed to TraitValue at this point by Mypy. Usually that'd due to Mypy's shortcomings, but sometimes this means an actual bug. I'd prefer an expilicit cast here if we're looking at the former.

for trait in sorted(sdk_trait_data, key=itemgetter("trait_key"))
if (trait_value := trait["trait_value"]) is not None
).encode(),
Expand Down
11 changes: 9 additions & 2 deletions api/environments/sdk/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,17 @@

class SDKTraitValueData(typing.TypedDict):
type: str
value: str
value: str | int | bool | float


# Trait value can be either:
# 1. A structured SDKTraitValueData (from serializer validation via TraitValueField)
# 2. A raw primitive value (str, int, bool, float) when called directly
# 3. None (to delete the trait)
TraitValue: typing.TypeAlias = SDKTraitValueData | str | int | bool | float | None


class SDKTraitData(typing.TypedDict):
trait_key: str
trait_value: SDKTraitValueData | None
trait_value: TraitValue
transient: NotRequired[bool]
4 changes: 2 additions & 2 deletions api/integrations/launch_darkly/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,7 +479,7 @@ def _import_targets(
[
{
"trait_key": "key",
"trait_value": identifier, # type: ignore[typeddict-item]
"trait_value": identifier,
}
]
)
Expand Down Expand Up @@ -1029,7 +1029,7 @@ def _create_segments_from_ld(
[
{
"trait_key": "key",
"trait_value": identifier, # type: ignore[typeddict-item]
"trait_value": identifier,
}
]
)
Expand Down
7 changes: 4 additions & 3 deletions api/tests/unit/environments/identities/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

from environments.identities.models import Identity
from environments.identities.traits.models import Trait
from environments.sdk.types import SDKTraitData, TraitValue


def generate_trait_data_item( # type: ignore[no-untyped-def]
def generate_trait_data_item(
trait_key: str = "trait_key",
trait_value: typing.Any = "trait_value",
trait_value: TraitValue = "trait_value",
transient: bool = False,
):
) -> SDKTraitData:
return {
"trait_key": trait_key,
"trait_value": trait_value,
Expand Down
Loading