Skip to content

Add date to preconfigured converters #420

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

Merged

Conversation

Peter554
Copy link
Contributor

@Peter554 Peter554 commented Aug 21, 2023

Adding support for date to the preconfigured converters. I see there was already some effort made in this direction here #365, however that PR is missing tests and seems to be a bit inactive.

@@ -30,6 +30,8 @@ def configure_converter(converter: BaseConverter):
converter.register_structure_hook(
datetime, lambda v, _: datetime.fromtimestamp(v, timezone.utc)
)
converter.register_unstructure_hook(date, lambda v: v.isoformat())
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't know much about msgpack, but it seems like the msgpack converter is doing things a bit differently with datetime - it is using timestamp rather than isoformat. Is there a reason the msgpack converter does this differently? How should we be handling dates in the msgpack converter?

Copy link
Member

Choose a reason for hiding this comment

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

We can, but let's make it use timestamps too, for consistency.

The idea is for the JSON converters to use the ISO format since JSON is meant to be somewhat human readable, and JavaScript can parse them out of the box. For binary formats like msgpack I think timestamps are a better fit since they're much more efficient.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Interesting, thanks. Updated now. Since date has no .timestamp method, I've used the midnight-aligned datetime.

@Peter554 Peter554 marked this pull request as ready for review August 21, 2023 13:43
@Peter554
Copy link
Contributor Author

Not sure what I have to do to get CI to run, but make lint and make test are passing for me locally.

@Tinche
Copy link
Member

Tinche commented Aug 22, 2023

Thanks for picking this up. I've clicked the button to run the tests, it needs my approval I guess.

@@ -148,6 +150,7 @@ def everythings(
Everything.AnIntEnum.A,
Everything.AStringEnum.A,
draw(dts),
draw(dates()),
Copy link
Member

Choose a reason for hiding this comment

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

Please set the max value here similar to what the datetime strategy is doing above. If we don't do this, we might break some 32-bit Linux downstreams (don't ask...).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah okay 😬 done

@Peter554 Peter554 force-pushed the add-date-to-preconfigured-converters branch 2 times, most recently from ae49e79 to 872448f Compare August 22, 2023 14:43
@Peter554 Peter554 force-pushed the add-date-to-preconfigured-converters branch from 872448f to 333a911 Compare August 22, 2023 14:48
@Tinche
Copy link
Member

Tinche commented Aug 23, 2023

Great job, thanks!

@Tinche Tinche merged commit 1f5781b into python-attrs:main Aug 23, 2023
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 this pull request may close these issues.

2 participants