Make uuid1() and uuid7() reproducible under a fixed seed#2427
Open
vidigoat wants to merge 1 commit into
Open
Conversation
uuid1() and uuid7() built their timestamp bits from time.time(), so their output changed between runs even with a fixed seed, unlike uuid4(). This contradicts Faker's documented seeding guarantee and the methods' own docstrings, which state the timestamp is drawn to ensure seedability. Draw the timestamp from the seeded generator instead of the wall clock, keeping the RFC 4122 / RFC 9562 version and variant bit layout intact. Add test_uuid1_seedability and test_uuid7_seedability mirroring test_uuid4_seedability. Closes joke2k#2426
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.
What does this change
uuid1()anduuid7()now derive their timestamp from the seeded Faker generator instead oftime.time(), so they return the same value on every run under a fixed seed, matchinguuid4(). Addstest_uuid1_seedabilityandtest_uuid7_seedability.What was wrong
Both methods built their timestamp bits from the wall clock:
uuid1:nanoseconds = int(time.time() * 1e9)uuid7:unix_ts_ms = int(time.time() * 1000) + ...The wall-clock value dominates the high bits, so the output changes between runs even with a fixed seed. This breaks the seeding guarantee documented in the README ("A Seed produces the same result when the same methods with the same version of faker are called") and contradicts these methods' own docstrings, which state the timestamp is chosen "to ensure seedability".
uuid4()is reproducible;uuid1()/uuid7()are not:How this fixes it
The timestamp is drawn from
self.generator.randomover a fixed range (up to 2035-01-01 UTC) instead of the wall clock. The RFC 4122 (v1) and RFC 9562 (v7) version/variant bit layout is unchanged, so the generated UUIDs are still structurally valid time-based UUIDs — they are now simply reproducible under a fixed seed.import timeis no longer used and is removed.Fixes #2426
AI Assistance Disclosure (REQUIRED)
I used an AI assistant to help draft this description and cross-check the RFC bit layouts. I reviewed and verified the change myself: reproduced the bug on
master, confirmed the two new tests fail before the fix and pass after, and ran the full test suite (2393 passed) plusmake lint(isort/black 24.4.0/flake8 clean on the changed files; the pre-existing mypy findings injson()are unrelated).Checklist
make lint