Skip to content

Implement ser_json_temporal config option #1743

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
wants to merge 17 commits into
base: main
Choose a base branch
from

Conversation

ollz272
Copy link
Contributor

@ollz272 ollz272 commented Jun 17, 2025

Implements the first part of pydantic/pydantic#11504, which is the ser_json_temporal config option.

We now have a ser_json_temporal part to the config, which accepts ['iso8601', 'seconds', 'milliseconds']. These have been implemented as designed in the original pydantic pull request ('iso8601' being obvious, seconds and milliseconds each returning the number of seconds and milliseconds in a float).

This has been done by implementing the serializers for each of the specified types (datetime, date, time, timedelta).

Special case for 'timedelta' where if the old config option is set, it will be used instead of ser_json_temporal.

Related issue number

pydantic/pydantic#11504

Checklist

  • [x ] Unit tests for the changes exist
  • [x ] Documentation reflects the changes where applicable
  • Pydantic tests pass with this pydantic-core (except for expected changes)
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

Selected Reviewer: @sydney-runkle

Copy link

codecov bot commented Jun 17, 2025

@ollz272
Copy link
Contributor Author

ollz272 commented Jun 17, 2025

@davidhewitt think i've got most of the way with the serialization part, just a question:
https://github.com/pydantic/pydantic-core/pull/1743/files#diff-c465892d6081b2cc8bd4dbc610f0898a2591a8e53a7f3918dd13a54747f708fbR100-R105

pydantic/pydantic#11504

The pydantic PR mentions the new config option overlaps with ser_json_timedelta, which will be deprecated, but doesn't (at least i can't see where) how we want to handle the case where both are set.

Do we want to do:

  1. Ignore ser_json_timedelta now
  2. if ser_json_timedelta is set, set ser_json_temporal to that value
  3. Have logic here where if ser_json_timedelta and ser_json_temporal is set, prefer using ser_json_timedelta over ser_json_temporal for timedeltas
  4. If both are set, always use ser_json_temporal

Copy link

codspeed-hq bot commented Jun 17, 2025

CodSpeed Performance Report

Merging #1743 will not alter performance

Comparing ollz272:ser_json_datetime (efbd0ae) with main (d9dacb0)

Summary

✅ 157 untouched benchmarks

@davidhewitt
Copy link
Contributor

Thanks!

I would make it such that if ser_json_temporal is set, use that value, otherwise use ser_json_timedelta value (where applicable).

That should make it non-breaking for existing users while adding the new preferred option.

@ollz272 ollz272 marked this pull request as ready for review June 17, 2025 13:41
@ollz272
Copy link
Contributor Author

ollz272 commented Jun 17, 2025

Right @davidhewitt, think i've implemented everything now. Got tests that show everything is working. Not 100% sure on my implementation on deciding between timedelta_mode and temporal_mode, but that should be all the serialisation work done?

As a note the validation part of this seems a lot more complicated so may take a bit (lot) more time to implement.. hopefully supporting this first isn't too bad?

@ollz272
Copy link
Contributor Author

ollz272 commented Jun 17, 2025

please review

Copy link
Contributor

@davidhewitt davidhewitt left a comment

Choose a reason for hiding this comment

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

Sorry for the slow reply. I had to read this one a few times to pick through everything.

@davidhewitt
Copy link
Contributor

As a note the validation part of this seems a lot more complicated so may take a bit (lot) more time to implement.. hopefully supporting this first isn't too bad?

I'm happy with starting here, the validation is a separate option, after all.

@ollz272
Copy link
Contributor Author

ollz272 commented Jun 21, 2025

@davidhewitt I think i've addressed all your comments bar the temporal_mode bits on the timedelta, keep trying things but nothing seems right so i think im doing something wrong!

@ollz272 ollz272 requested a review from davidhewitt June 21, 2025 09:33
@ollz272
Copy link
Contributor Author

ollz272 commented Jun 21, 2025

@davidhewitt actually, think i got the gist of it, didn't use the option in the end just create a method that turns the timedeltamode into a temporal mode for timedelta ser. Think this is everything resolved now

@ollz272
Copy link
Contributor Author

ollz272 commented Jun 26, 2025

@davidhewitt was there anything else you wanted me to have a look at here?

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

Successfully merging this pull request may close these issues.

2 participants