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: python datetime [de]serialization bug #734

Merged
merged 1 commit into from
Jul 2, 2021

Conversation

joeldodge79
Copy link
Contributor

we'd forgotten to register a cattr.register_unstructure_hook for datetime.datetime objects.
Clearly no one had been trying to send in any data that included a datetime.datetime till now

closes #720



converter31.register_structure_hook(datetime.datetime, datetime_structure_hook)
converter40.register_structure_hook(datetime.datetime, datetime_structure_hook)
cattr.register_unstructure_hook(model.Model, unstructure_hook) # type: ignore
cattr.register_unstructure_hook(datetime.datetime, lambda dt: dt.strftime(DATETIME_FMT))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

this is the whole fix

@@ -99,6 +99,7 @@ class Model(ml.Model):
# standard types
id: Optional[int] = None
name: Optional[str] = None
datetime_field: Optional[datetime.datetime] = None
Copy link
Contributor Author

Choose a reason for hiding this comment

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

adding this datetime_field everywhere in the tests identically reproduced the issue in #720

Copy link
Contributor

@jkaster jkaster left a comment

Choose a reason for hiding this comment

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

Good fix

@@ -144,6 +144,7 @@ def unstructure_hook(api_model):
return data


DATETIME_FMT = "%Y-%m-%dT%H:%M:%S.%f%z"
Copy link
Contributor

Choose a reason for hiding this comment

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

Love the infinite syntax variation for datetime string formatting

@github-actions
Copy link
Contributor

github-actions bot commented Jul 2, 2021

Python Tests

  10 files    10 suites   1m 55s ⏱️
140 tests 140 ✔️ 0 💤 0 ❌
788 runs  788 ✔️ 0 💤 0 ❌

Results for commit 5032ce4.

@joeldodge79 joeldodge79 merged commit c821f2b into main Jul 2, 2021
@joeldodge79 joeldodge79 deleted the python-datetime-serializeation-fix branch July 2, 2021 22:17
@github-actions

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Python: Synchronize Looker objects and json
2 participants