Repository navigation
Pack datetime subclasses as timestamps unless strict_types is set - #755
Closed
mkantautas wants to merge 1 commit into
Closed
mkantautas wants to merge 1 commit into
mkantautas wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues remain for the datetime subclass serialization changes.
No open issues 🎉
What changed in this PR
Aligns C-extension datetime subclass serialization with the Python fallback, addressing the backend inconsistency referenced in #683.
Changes:
- Packs aware datetime subclasses as timestamps when
datetime=Trueandstrict_types=False. - Accepts timedelta subclass subtraction results and raises
TypeErrorfor invalid results. - Adds regression tests for timestamp round trips, callback dispatch, naive values, and strict or disabled-datetime behavior.
| File | Description |
|---|---|
test/test_timestamp.py |
Adds datetime subclass regression tests and a C-only invalid-subtraction check. |
msgpack/_packer.pyx |
Makes datetime checks respect strict_types and accepts timedelta subclasses. |
Review effort: Balanced
Member
|
Could you explain what motivated you to create this PR? If you have a real-world use case where this change is needed, please share it. |
Author
|
There is no real-world use case here. Just wanted to get a free 200 claude plan with 100 PRs merged(already have over 100PRs merged). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
With
datetime=Trueand the defaultstrict_types=False, the C Packer rejects a subclass ofdatetime.datetime, while the Python fallback packs it as a Timestamp. When adefaultcallback is supplied, the C Packer calls it instead. This makes serialization depend on which backend is loaded.The datetime checks now follow
strict_types, as the other type checks do. Aware subclasses pack as timestamps, and naive subclasses raise the sameValueErroras the fallback. Validtimedeltasubclasses returned by subtraction are accepted.strict_types=Trueanddatetime=Falseretain the existing subclass handling. Applications that intentionally serialize datetime subclasses throughdefaultcan usestrict_types=Trueto preserve that behavior.For example, both backends now pack this value as
d7ff64361d20661e3a1d:The regressions cover timestamp bytes and round trips, callback dispatch, naive values, strict and disabled-datetime controls, and subtraction returning a timedelta subclass. A C-only test also checks that an invalid subtraction result raises
TypeError; fallback behavior for invalid overrides is unchanged.Validated on CPython 3.12.3 / Linux x86_64 with Cython 3.3.0:
Other Python versions, operating systems, PyPy and free-threaded builds were not run locally.
Refs #683, finding 16.