Repository navigation
Conversation
ObjectId.from_datetime() fills the 8 bytes after the timestamp with zeros, so it can only make dummy ids for range queries. With unique=True it fills them like ObjectId() does (process random value plus counter), giving a unique id with an external timestamp, like NewObjectIDFromTimestamp in the Go driver. Assisted-by: Claude
sleepyStick
requested changes
Nov 10, 2026
sleepyStick
left a comment
Contributor
There was a problem hiding this comment.
thanks for your work! I've got some changes - kinda minor i think? but shouldn't take too long to actually fix!
| return cls(cls._generate(timestamp)) | ||
| return cls(_PACK_INT(timestamp) + b"\x00\x00\x00\x00\x00\x00\x00\x00") | ||
|
|
||
| @classmethod |
Contributor
There was a problem hiding this comment.
Suggested change
| @classmethod | |
| @staticmethod |
| return cls(_PACK_INT(timestamp) + b"\x00\x00\x00\x00\x00\x00\x00\x00") | ||
|
|
||
| @classmethod | ||
| def _generate(cls, timestamp: int) -> bytes: |
Contributor
There was a problem hiding this comment.
Suggested change
| def _generate(cls, timestamp: int) -> bytes: | |
| def _generate_bytes(cls, timestamp: int) -> bytes: |
Contributor
There was a problem hiding this comment.
maybe? idk _generate itself feels too vague though. Open to other ideas but I do want the name to be more specific
Assisted-by: Claude
This branch has not been deployed
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.
PYTHON-5883
Changes in this PR
ObjectId.from_datetime()fills the 8 bytes after the timestamp with zeros, so it can only make dummy ids for range queries. The ticket asks for a way to make a real, unique ObjectId from an external timestamp, like Go'sNewObjectIDFromTimestamp.This adds a keyword-only
uniqueparameter:ObjectId.__init__(counter under_inc_lock, then 4-byte timestamp + 5-byte random + 3-byte counter) moved into a private_generate(timestamp)classmethod.ObjectId()now calls it, so its behavior is unchanged, andfrom_datetime(..., unique=True)reuses it.uniqueis keyword-only and defaults toFalse, so existing callers are unaffected.unique=False) withversionchanged:: 4.20, added a changelog entry, and added myself todoc/contributors.rst.I went with a parameter on the existing method to keep one entry point, but I'm happy to switch to a separate classmethod or another name if you prefer (I asked on the ticket too).
I used Claude to help with this, and I reviewed and tested the change myself.
Test Plan
Two new tests in
test/test_objectid.py:test_from_datetime_unique: two calls with the same datetime keep that timestamp but are different, are not zero-filled, carry the process random value in bytes 4-8 (same asObjectId()), the counter goes up by one, and aware datetimes are converted to UTC like before.test_from_datetime_unique_is_keyword_only: passinguniquepositionally raisesTypeError.The new test fails on
mainand passes with the change.pytest test/test_objectid.py test/test_json_util.py: 45 passed.ruff format --checkandmypy bson/objectid.pypass.Checklist
Checklist for Author
Checklist for Reviewer