Skip to content
Merged
Changes from 1 commit
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
42d55b4
add UUIDv7 implementation
picnixz Jun 28, 2024
6826fa1
add tests
picnixz Jun 28, 2024
edc2cab
blurb
picnixz Jun 28, 2024
c6d26b6
update CHANGELOG
picnixz Jun 28, 2024
2ddb4b8
update RFC number
picnixz Jun 28, 2024
bcd1417
add TODO in the docs
picnixz Jun 28, 2024
4630c8f
Merge branch 'main' into uuid-v7-method-1
picnixz Jul 22, 2024
cd80afb
Merge branch 'main' into uuid-v7-89083
picnixz Aug 21, 2024
c3d4745
add UUIDv8 implementation
picnixz Aug 22, 2024
392d289
add tests
picnixz Aug 22, 2024
26889ea
blurb
picnixz Aug 22, 2024
44b66e6
add What's New entry
picnixz Aug 22, 2024
7be6dc4
add docs
picnixz Aug 22, 2024
8ba3d8b
Improve hexadecimal masks reading
picnixz Sep 25, 2024
a14ae9b
add uniqueness test
picnixz Sep 25, 2024
7a169c9
Update mentions to RFC 4122 to RFC 4122/9562 when possible.
picnixz Sep 25, 2024
b082c90
Update docs
picnixz Sep 25, 2024
94c70e9
Merge branch 'main' into uuid-v8-89083
picnixz Sep 25, 2024
05b7a2b
Merge branch 'main' into uuid-v7-method-1
hugovk Nov 2, 2024
275deb7
Merge branch 'main' into uuid-v8-89083
hugovk Nov 2, 2024
5e97cc3
Apply suggestions from code review
picnixz Nov 11, 2024
051f34e
Update Lib/test/test_uuid.py
picnixz Nov 11, 2024
bdf9a77
Apply suggestions from code review
picnixz Nov 11, 2024
00661fc
Merge remote-tracking branch 'origin/uuid-v8-89083'
picnixz Nov 13, 2024
0474de4
Merge remote-tracking branch 'origin/uuid-v8-89083' into uuid-v7-89083
picnixz Nov 14, 2024
a446d53
Merge remote-tracking branch 'upstream/main' into uuid-v7-89083
picnixz Nov 14, 2024
2e39072
update CLI
picnixz Nov 14, 2024
ebc1a07
Merge branch 'main' into uuid-v7-89083
picnixz Nov 14, 2024
694e07f
post-merge
picnixz Nov 14, 2024
965dbc8
Merge remote-tracking branch 'origin/uuid-v7-method-1' into uuid-v7-8…
picnixz Nov 14, 2024
7ff4368
improve readability
picnixz Nov 14, 2024
7c3cab6
post-merge
picnixz Nov 14, 2024
e758741
uniqueness test
picnixz Nov 14, 2024
c18d0c4
improve test comments
picnixz Nov 14, 2024
2df6f41
Merge remote-tracking branch 'upstream/main'
picnixz Nov 15, 2024
6fcb6a1
fix lint
picnixz Nov 15, 2024
f6048c9
Merge branch 'main' into uuid-v7-89083
picnixz Nov 15, 2024
be3f024
post-merge
picnixz Nov 15, 2024
99c6761
Merge branch 'main' into uuid-v7-89083
picnixz Nov 15, 2024
06befca
use versionchanged instead of versionadded
picnixz Nov 15, 2024
2aacadf
Merge branch 'main' into uuid-v7-method-1
picnixz Nov 16, 2024
f7f536e
Merge branch 'main' into uuid-v7-method-1
picnixz Dec 5, 2024
aee2898
improve UUIDv7 tests readability
picnixz Dec 19, 2024
1a5ac19
improve UUIDv7 uniqueness tests
picnixz Dec 19, 2024
8764b28
Merge branch 'main' into uuid-v7-method-1
picnixz Dec 21, 2024
af0baef
Merge branch 'main' into uuid-v7-method-1
picnixz Jan 11, 2025
939b5a8
Merge branch 'main' into feat/uuid/v7-89083
picnixz Jan 20, 2025
ef85b20
use `UUID._from_int` for UUIDv7 and remove `divmod` usage
picnixz Jan 20, 2025
2d08821
Merge branch 'main' into uuid-v7-method-1
picnixz Jan 20, 2025
eaa9ad4
Merge branch 'main' into uuid-v7-method-1
picnixz Feb 17, 2025
571d2fe
backport Victor's review on UUIDv6
picnixz Feb 23, 2025
f9ac658
address Victor's review
picnixz Feb 25, 2025
a756b9d
remove mention of UNIX_EPOCH + 10k years as the proof is long
picnixz Feb 25, 2025
4406796
import `time` globally as UUIDv7 is likely to be used now
picnixz Feb 25, 2025
d4eeded
run half-black
picnixz Feb 25, 2025
0e54a72
update docs
picnixz Feb 25, 2025
40ab2fa
Revert "run half-black"
picnixz Feb 25, 2025
5ee85ad
Merge branch 'main' into uuid-v7-method-1
picnixz Feb 25, 2025
3ce8943
add blank line for readability
picnixz Feb 25, 2025
59e6d7e
update comment
picnixz Feb 25, 2025
437d8cf
Update Lib/uuid.py
picnixz Feb 25, 2025
2d917b0
Merge remote-tracking branch 'upstream/main' into feat/uuid/v7-89083
picnixz Mar 2, 2025
73ab656
improve online docs
picnixz Mar 3, 2025
54d07ae
`constructor` -> `factory` in labels
picnixz Mar 3, 2025
6d76389
reword prolog
picnixz Mar 3, 2025
bd4ab55
'is outside the scope' -> 'exceeds the scope'
picnixz Mar 3, 2025
e9ddb74
Apply suggestions from code review
picnixz Mar 3, 2025
8755de0
apply PEP-8 only for UUID6, UUID7 and UUID8
picnixz Mar 3, 2025
12d7ad4
small fix
merwok Mar 3, 2025
560d87c
avoid complex language :)
picnixz Mar 3, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add UUIDv7 implementation
  • Loading branch information
picnixz committed Jun 28, 2024
commit 42d55b4b0df528761adf43f1cc7b761187901152
79 changes: 69 additions & 10 deletions Lib/uuid.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
r"""UUID objects (universally unique identifiers) according to RFC 4122.

This module provides immutable UUID objects (class UUID) and the functions
uuid1(), uuid3(), uuid4(), uuid5() for generating version 1, 3, 4, and 5
UUIDs as specified in RFC 4122.
uuid1(), uuid3(), uuid4(), uuid5(), and uuid7() for generating version 1, 3,
4, 5, and 7 UUIDs as specified in RFC 4122 (superseeded by RFC 9562 but still
referred to as RFC 4122 for compatibility purposes).

If all you want is a unique ID, you should probably call uuid1() or uuid4().
Note that uuid1() may compromise privacy since it creates a UUID containing
Expand Down Expand Up @@ -129,7 +130,7 @@ class UUID:
variant the UUID variant (one of the constants RESERVED_NCS,
RFC_4122, RESERVED_MICROSOFT, or RESERVED_FUTURE)

version the UUID version number (1 through 5, meaningful only
version the UUID version number (1, 3, 4, 5 and 7, meaningful only
when the variant is RFC_4122)

is_safe An enum indicating whether the UUID has been generated in
Expand Down Expand Up @@ -214,7 +215,7 @@ def __init__(self, hex=None, bytes=None, bytes_le=None, fields=None,
if not 0 <= int < 1<<128:
raise ValueError('int is out of range (need a 128-bit value)')
if version is not None:
if not 1 <= version <= 5:
if not 1 <= version <= 7:
Comment thread
picnixz marked this conversation as resolved.
Outdated
raise ValueError('illegal version number')
# Set the variant to RFC 4122.
int &= ~(0xc000 << 48)
Expand Down Expand Up @@ -656,7 +657,7 @@ def getnode():
assert False, '_random_getnode() returned invalid value: {}'.format(_node)


_last_timestamp = None
_last_timestamp_v1 = None

def uuid1(node=None, clock_seq=None):
"""Generate a UUID from a host ID, sequence number, and the current time.
Expand All @@ -674,15 +675,15 @@ def uuid1(node=None, clock_seq=None):
is_safe = SafeUUID.unknown
return UUID(bytes=uuid_time, is_safe=is_safe)

global _last_timestamp
global _last_timestamp_v1
import time
nanoseconds = time.time_ns()
# 0x01b21dd213814000 is the number of 100-ns intervals between the
# UUID epoch 1582-10-15 00:00:00 and the Unix epoch 1970-01-01 00:00:00.
timestamp = nanoseconds // 100 + 0x01b21dd213814000
if _last_timestamp is not None and timestamp <= _last_timestamp:
timestamp = _last_timestamp + 1
_last_timestamp = timestamp
if _last_timestamp_v1 is not None and timestamp <= _last_timestamp_v1:
timestamp = _last_timestamp_v1 + 1
_last_timestamp_v1 = timestamp
if clock_seq is None:
import random
clock_seq = random.getrandbits(14) # instead of stable storage
Expand Down Expand Up @@ -719,14 +720,72 @@ def uuid5(namespace, name):
hash = sha1(namespace.bytes + name).digest()
return UUID(bytes=hash[:16], version=5)

_last_timestamp_v7 = None
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
_last_timestamp_v7 = None
_last_timestamp_v7 = None

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I wanted to apply a PEP-8 change in a separate PR because the module has inconsistencies. It seems a bit weird to only PEP-8ify this part of the code while the rest is not really PEP-8ified. See #121119 (comment).

Copy link
Copy Markdown
Member

@merwok merwok Mar 3, 2025

Choose a reason for hiding this comment

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

python-dev doesn’t have a practice of doing reformatting-only PRs.
Remember that consistency for its own sake is not a goal (see PEP 20)

Instead, follow good conventions in code that is added or already changed.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Well... if a core dev endorses the change, I think it's fine. I don't mind endorsing it. I didn't do it for uuid6() nor for uuid8() when I wrote the function as there were more 1-blank lines separations rather than 2 blank lines separations. But if you insist on adding 2 blank lines, I'll also add them around the other functions because I prefer being consistent in this case (honestly, having 2 blank lines around only UUIDv7 makes it harder to read IMO).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I would say PEP-8 tells me that we can also ignore the PEP if the surrounding code already breaks it. But I will make a commit to just add blank lines around the functions I've added (uuid6 to uuid8).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I don't think that it's worth it to reformat the whole uuid.py file to PEP 8, but respecting PEP 8 for new code (or code near changed code) is a good practice.

Copy link
Copy Markdown
Member

@merwok merwok Mar 3, 2025

Choose a reason for hiding this comment

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

Also, adding a few blank lines is innocuous (it does not change git blame, or risk changing the meaning of code), so it’s fine to do in existing code in this PR.

Generally people saying they want to «apply PEP 8» think of more bigger changes.

[note: marking this convo as unresolved just to help Victor or Hugo see it, not because there’s something left to do for the PR author]

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I would say PEP-8 tells me that we can also ignore the PEP if the surrounding code already breaks it.

This is about for example methods using camelCase in unittest or logging, not spaces!

_last_counter_v7 = 0 # 42-bit counter

def uuid7():
Comment thread
picnixz marked this conversation as resolved.
"""Generate a UUID from a Unix timestamp in milliseconds and random bits.

UUIDv7 objects feature monotonicity within a millisecond.
"""
# --- 48 --- -- 4 -- --- 12 --- -- 2 -- --- 30 --- - 32 -
# unix_ts_ms | version | counter_hi | variant | counter_lo | random
#
# 'counter = counter_hi | counter_lo' is a 42-bit counter constructed
# with Method 1 of RFC 9562, §6.2, and its MSB is set to 0.
#
# 'random' is a 32-bit random value regenerated for every new UUID.
#
# If multiple UUIDs are generated within the same millisecond, the LSB
# of 'counter' is incremented by 1. When overflowing, the timestamp is
# advanced and the counter is reset to a random 42-bit integer with MSB
# set to 0.

def get_counter_and_tail():
Comment thread
picnixz marked this conversation as resolved.
Outdated
rand = int.from_bytes(os.urandom(10))
# 42-bit counter with MSB set to 0
counter = (rand >> 32) & 0x1ffffffffff
# 32-bit random data
tail = rand & 0xffffffff
Comment thread
picnixz marked this conversation as resolved.
Outdated
return counter, tail

global _last_timestamp_v7
global _last_counter_v7

import time
Comment thread
picnixz marked this conversation as resolved.
Outdated
nanoseconds = time.time_ns()
timestamp_ms, _ = divmod(nanoseconds, 1_000_000)

if _last_timestamp_v7 is None or timestamp_ms > _last_timestamp_v7:
counter, tail = get_counter_and_tail()
else:
if timestamp_ms < _last_timestamp_v7:
timestamp_ms = _last_timestamp_v7 + 1
Comment thread
hugovk marked this conversation as resolved.
# advance the counter
counter = _last_counter_v7 + 1
if counter > 0x3ffffffffff:
timestamp_ms += 1 # advance the timestamp
counter, tail = get_counter_and_tail()
else:
tail = int.from_bytes(os.urandom(4))
Comment thread
picnixz marked this conversation as resolved.

_last_timestamp_v7 = timestamp_ms
Comment thread
picnixz marked this conversation as resolved.
_last_counter_v7 = counter
Comment thread
picnixz marked this conversation as resolved.
Outdated

int_uuid_7 = (timestamp_ms & 0xffffffffffff) << 80
int_uuid_7 |= ((counter >> 30) & 0xfff) << 64
int_uuid_7 |= (counter & 0x3fffffff) << 32
int_uuid_7 |= tail & 0xffffffff
return UUID(int=int_uuid_7, version=7)

def main():
"""Run the uuid command line interface."""
uuid_funcs = {
"uuid1": uuid1,
"uuid3": uuid3,
"uuid4": uuid4,
"uuid5": uuid5
"uuid5": uuid5,
"uuid7": uuid7,
}
uuid_namespace_funcs = ("uuid3", "uuid5")
namespaces = {
Expand Down