Repository navigation
Commit fa6eec1
fix: Convert datetime attributes to ISO-8601 strings (#61)
Evaluation context attributes are now converted to values the
LaunchDarkly context model can represent, instead of being passed
through as-is.
- A `datetime` attribute previously reached the LaunchDarkly context
unchanged, so the context could not be serialized: `json.dumps` on it
raises `TypeError: Object of type datetime is not JSON serializable`,
which breaks analytics event delivery for that context.
- `datetime` values now become their ISO-8601 representation in UTC,
which is the format the LaunchDarkly date targeting operators accept. A
naive `datetime` is treated as UTC.
- The conversion recurses into lists and dictionaries, so a nested
`datetime` is converted too.
- A value of a type which cannot be represented becomes `None` and logs
an error, rather than producing an unusable context.
This aligns the Python provider with the Java, .NET, and JavaScript
providers, which already convert their SDK's date values, and with the
`date or instant` row of the OpenFeature provider behavior spec's value
conversion table.
Found during the weekly OpenFeature provider audit.
<details>
<summary>Implementation details</summary>
**Requirements**
- [x] I have added test coverage for new or changed functionality
- [x] I have followed the repository's [pull request submission
guidelines](../blob/main/CONTRIBUTING.md#submitting-pull-requests)
- [x] I have validated my changes against all supported platform
versions
**Related issues**
None.
**Describe the solution you've provided**
`_to_ld_value` in the context converter handles the OpenFeature value
types: scalars pass through, a `datetime` is converted with
`astimezone(timezone.utc).isoformat()` (with the `+00:00` offset written
as `Z`), mappings and sequences are converted item by item, and anything
else becomes `None` with an error log. Custom attributes are set through
it; `name`, `anonymous`, and `privateAttributes` keep their existing
type-specific handling.
`datetime` is one of the types the OpenFeature Python SDK's
`EvaluationContextAttribute` alias allows, so an application following
the OpenFeature specification can supply one.
**Describe alternatives you've considered**
Rejecting a `datetime` attribute and logging an error: that loses
targeting information the application intended to send, and date
targeting operators exist precisely for this case.
Treating a naive `datetime` as local time: the local zone of a server is
incidental, so the same context would convert differently depending on
where it ran.
**Additional context**
Testing: `make lint` (mypy) and `make test` (86 tests). The new tests
cover an aware `datetime`, a non-UTC offset, a naive `datetime`, a
`datetime` nested inside a dictionary and list, JSON serializability of
the resulting context, and an unconvertible value.
The README's OpenFeature specific considerations section documents the
conversion.
</details>
Link to Devin session:
https://app.devin.ai/sessions/fe1eb757fe694ef79f3d09f6307d4b47
Open in Devin Desktop:
https://app.devin.ai/desktop/session/fe1eb757fe694ef79f3d09f6307d4b47?variant=devin
Requested by: @kinyoklion
<!-- CURSOR_SUMMARY -->
---
> [!NOTE]
> **Overview**
> **Evaluation context attributes are normalized before they are set on
LaunchDarkly contexts**, fixing cases where a `datetime` on the context
broke JSON serialization (e.g. analytics events).
>
> Custom attributes now pass through `_to_ld_value`: `datetime` values
become ISO-8601 strings in UTC (naive datetimes treated as UTC, `+00:00`
rendered as `Z`), with the same conversion applied inside nested lists
and dicts. Scalars pass through unchanged; types the LD model cannot
represent are logged and stored as `None`. Special handling for `name`,
`anonymous`, and `privateAttributes` is unchanged.
>
> The README documents this behavior. New tests cover aware/offset/naive
datetimes, nesting, JSON serializability, and unsupported types.
>
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
847f090. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->
Co-authored-by: Devin AI <devin-ai-integration[bot]@users.noreply.github.com>1 parent 1584451 commit fa6eec1
3 files changed
Lines changed: 63 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
95 | 95 | | |
96 | 96 | | |
97 | 97 | | |
| 98 | + | |
| 99 | + | |
98 | 100 | | |
99 | 101 | | |
100 | 102 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
1 | 2 | | |
2 | | - | |
| 3 | + | |
3 | 4 | | |
4 | 5 | | |
5 | 6 | | |
| |||
8 | 9 | | |
9 | 10 | | |
10 | 11 | | |
| 12 | + | |
| 13 | + | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
11 | 39 | | |
12 | 40 | | |
13 | 41 | | |
| |||
108 | 136 | | |
109 | 137 | | |
110 | 138 | | |
111 | | - | |
| 139 | + | |
112 | 140 | | |
113 | 141 | | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
1 | 3 | | |
| 4 | + | |
2 | 5 | | |
3 | 6 | | |
4 | 7 | | |
| |||
219 | 222 | | |
220 | 223 | | |
221 | 224 | | |
| 225 | + | |
| 226 | + | |
| 227 | + | |
| 228 | + | |
| 229 | + | |
| 230 | + | |
| 231 | + | |
| 232 | + | |
| 233 | + | |
| 234 | + | |
| 235 | + | |
| 236 | + | |
| 237 | + | |
| 238 | + | |
| 239 | + | |
| 240 | + | |
| 241 | + | |
| 242 | + | |
| 243 | + | |
| 244 | + | |
| 245 | + | |
| 246 | + | |
| 247 | + | |
| 248 | + | |
| 249 | + | |
| 250 | + | |
| 251 | + | |
| 252 | + | |
0 commit comments