Skip to content

feat: Conform primitive properties from strings #3014

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

Open
edgarrmondragon opened this issue May 5, 2025 · 0 comments · May be fixed by #2997
Open

feat: Conform primitive properties from strings #3014

edgarrmondragon opened this issue May 5, 2025 · 0 comments · May be fixed by #2997

Comments

@edgarrmondragon
Copy link
Collaborator

Discussed in #2994

Originally posted by ReubenFrankel April 22, 2025
Why does the SDK conform primitive properties of a record dict towards a JSON compatible format (e.g. datetime.datetime to str), but not from strings to their expected schema type? Is/should it be the responsibility of the developer to define this mapping?

def _conform_primitive_property( # noqa: PLR0911
elem: t.Any, # noqa: ANN401
property_schema: dict,
) -> t.Any: # noqa: ANN401
"""Converts a primitive (i.e. not object or array) to a json compatible type."""
if isinstance(elem, (datetime.datetime,)):
return to_json_compatible(elem)
if isinstance(elem, datetime.date):
return elem.isoformat()
if isinstance(elem, datetime.timedelta):
epoch = datetime.datetime.fromtimestamp(0, UTC)
timedelta_from_epoch = epoch + elem
if timedelta_from_epoch.tzinfo is None:
timedelta_from_epoch = timedelta_from_epoch.replace(tzinfo=UTC)
return timedelta_from_epoch.isoformat()
if isinstance(elem, datetime.time):
return str(elem)
if isinstance(elem, bytes):
# for BIT value, treat 0 as False and anything else as True
return elem != b"\x00" if is_boolean_type(property_schema) else elem.hex()
if isinstance(elem, (float, decimal.Decimal)):
if math.isnan(elem) or math.isinf(elem):
return None
return elem
if _is_exclusive_boolean_type(property_schema):
return None if elem is None else elem != 0
return elem

i.e.

    if isinstance(elem, str) and th.IntegerType.__type_name__ in property_schema["type"]:
        return int(elem)
```</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant