GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-89083: add support for UUID version 7 (RFC 9562) by picnixz · Pull Request #121119 · python/cpython

Conversation

@picnixz

@picnixz picnixz commented Jun 28, 2024 •
edited by github-actions Bot
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

Based on the discussion in #89083 and https://discuss.python.org/t/rfc-4122-9562-uuid-version-7-and-8-implementation/56725/2, this is the implementation that I suggest for the standard library.

The documentation is still missing because I don't have a good formulation for now.

In this PR, I did not include the following:

  • mutex guards
  • timestamp offsets

The reason is that I want to keep the first implementation simple for the sake of review. In addition, we did not give the add mutex for UUIDv1 so I don't want to do it only for v7.

@sergeyprokhorenko I don't know if you have the answer, but is there any safe guards if the timestamp overflows actually? or do we just don't care at all for now? (like, leave the problem for the future generations?)

  • Issue: Support UUIDv6, UUIDv7, and UUIDv8 from RFC 9562 #89083

📚 Documentation preview 📚: https://cpython-previews--121119.org.readthedocs.build/

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

Finkregh, ollandos, d33kayyy, PipeKnight, alechenninger, benedikt-bartscher, ngocnguyen43, edgarrmondragon, glebignatieff, pierrepo, and 11 more reacted with heart emoji
All reactions
  • ❤️ 21 reactions
picnixz added 6 commits June 28, 2024 11:40
@picnixz
add UUIDv7 implementation
42d55b4
@picnixz
add tests
6826fa1
@picnixz
blurb
edc2cab
@picnixz
update CHANGELOG
c6d26b6
@picnixz
update RFC number
2ddb4b8
@picnixz
add TODO in the docs
bcd1417
@bedevere-app bedevere-app Bot mentioned this pull request Jun 28, 2024
Support UUIDv6, UUIDv7, and UUIDv8 from RFC 9562 #89083
Closed
@bedevere-app bedevere-app Bot added the awaiting review label Jun 28, 2024
This was referenced Jun 28, 2024
gh-89083: support UUID version 7 (monotonous version) (RFC 9562) [abandoned proposal] #120830
Closed
gh-89083: add support for UUID version 6 (RFC 9562) #120650
Merged
@sergeyprokhorenko

sergeyprokhorenko commented Jun 28, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

@sergeyprokhorenko I don't know if you have the answer, but is there any safe guards if the timestamp overflows actually? or do we just don't care at all for now? (like, leave the problem for the future generations?)

You already have three counter overflow protections:

  1. Very long counter (42 bits)
  2. Counter segment (MSB) initialized to 0
  3. Incremented timestamp on overflow

The timestamp will not be full for about 6900 years. If the system clock stops and the timestamp is used as a counter, it will also last a long time.

You can be absolutely calm

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@picnixz

picnixz commented Jun 28, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

Yes, but I wanted to know whether the RFC actually considered the case when you use your own offset. Let's say we want to generate a future UUID for some obscure reason, I was wondering "is there anything on that topics?" But I think I'll just leave it to future generations.

What I meant is "what do you do if the operation of incrementing the timestamp itself overflows"?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sergeyprokhorenko

sergeyprokhorenko commented Jun 28, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

Yes, but I wanted to know whether the RFC actually considered the case when you use your own offset. Let's say we want to generate a future UUID for some obscure reason, I was wondering "is there anything on that topics?" But I think I'll just leave it to future generations.

What I meant is "what do you do if the operation of incrementing the timestamp itself overflows"?

Don't set offsets to 6900 years or minus 2k years, and everything will be OK. Foolproofing is an implementation detail.

picnixz reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@sergeyprokhorenko

sergeyprokhorenko commented Jun 28, 2024 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

When the timestamp goes beyond the upper or lower limit of the acceptable range, a zero offset can be applied. This is how I would do it. The RFC does not cover this issue.

I think timestamp offset could be a competitive advantage of this implementation without significant cost.

UUIDv1 can be forgotten and no longer upgraded. This is an outdated version

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@picnixz picnixz changed the title gh-89083: add ref. impl. for UUID version 7 (RFC 9562) gh-89083: add support for UUID version 7 (RFC 9562) Jun 28, 2024
@picnixz picnixz mentioned this pull request Jun 30, 2024
[RFE] fields and time_* properties must not be used on UUIDs that are time-agnostic. #120878
Open
mastizada
mastizada reviewed Jul 7, 2024
View reviewed changes
Comment thread Lib/uuid.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@sixcare sixcare mentioned this pull request Jul 8, 2024
Switch out UUIDv4 with UUIDv7 Turplanlegger/turplanlegger-fastapi#89
Open
@pretoriusdre

pretoriusdre commented Jul 22, 2024

Copy link
Copy Markdown

Great job on this PR. One thing...

In the get_counter_and_tail method:
rand = int.from_bytes(os.urandom(10))

Might I suggest to explicitly specify the required byteorder using the byteorder argument?

Running this code in an older python env gives an error:
TypeError: from_bytes() missing required argument 'byteorder' (pos 2)

It seems like some default is now provided, but in my opinion, this could lead to some ambiguity. See below:
https://discuss.python.org/t/what-should-be-the-default-value-for-int-to-bytes-byteorder/10616

There is another usage of int.from_bytes in the same uuid module, perhaps if the above is being addressed, this could be put within same scope.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@picnixz

picnixz commented Jul 22, 2024

Copy link
Copy Markdown
Member Author

Running this code in an older python env gives an error:

This feature would only be put in 3.14 or later, so we can ignore this.

but in my opinion, this could lead to some ambiguity

It doesn't matter whether it's little or big endian here since we are only interested in randomness and not actual data. In addition, not specifying it might be a bit faster since the C implementation currently does:

    if (byteorder == NULL)
        little_endian = 0;
    else if (_PyUnicode_Equal(byteorder, &_Py_ID(little)))
        little_endian = 1;
    else if (_PyUnicode_Equal(byteorder, &_Py_ID(big)))
        little_endian = 0;

So, not specifying the byteorder, is equivalent to have byteorder being NULL out there, which saves a string comparison.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@picnixz
Merge branch 'main' into uuid-v7-method-1
4630c8f
jnoring-pw
jnoring-pw reviewed Aug 20, 2024
View reviewed changes

@jnoring-pw jnoring-pw left a comment

Copy link
Copy Markdown

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Just a few minor comments as I looked through this code (I'm interested in uuid7 suport in our project). Thanks for this! It's looking good.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

fmnxl reacted with thumbs down emoji
All reactions
  • 👎 1 reaction
Comment thread Lib/uuid.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/uuid.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

picnixz added 6 commits August 21, 2024 13:32
@picnixz
Merge branch 'main' into uuid-v7-89083
cd80afb
@picnixz
add UUIDv8 implementation
c3d4745
@picnixz
add tests
392d289
@picnixz
blurb
26889ea
@picnixz
add What's New entry
44b66e6
@picnixz
add docs
7be6dc4
@picnixz picnixz changed the title gh-89083: add support for UUID version 7 (RFC 9562) gh-89083: add support for UUID version 7 (RFC 9562) Aug 22, 2024
@picnixz
Improve hexadecimal masks reading
8ba3d8b
@picnixz
Merge remote-tracking branch 'upstream/main' into feat/uuid/v7-89083
2d917b0
@picnixz
picnixz requested review from hugovk and merwok March 2, 2025 11:51
@picnixz

picnixz commented Mar 2, 2025

Copy link
Copy Markdown
Member Author

@merwok Do you find the formulation still clear in uuid.py? I made it explicit in the online docs but for the Python file, I think we can be a bit less formal.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

hugovk
hugovk reviewed Mar 2, 2025
View reviewed changes
Comment thread Doc/library/uuid.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/uuid.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@picnixz
improve online docs
73ab656
picnixz
picnixz commented Mar 3, 2025
View reviewed changes
Comment thread Lib/uuid.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

@picnixz

picnixz commented Mar 3, 2025

Copy link
Copy Markdown
Member Author

@merwok @hugovk I've applied some suggestions. WDYT?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

merwok
merwok reviewed Mar 3, 2025
View reviewed changes
Comment thread Doc/library/uuid.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

merwok
merwok reviewed Mar 3, 2025
View reviewed changes
Comment thread Doc/library/uuid.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

merwok
merwok reviewed Mar 3, 2025
View reviewed changes
Comment thread Doc/whatsnew/3.14.rst

* Add support for UUID versions 6 and 8 via :func:`uuid.uuid6` and
:func:`uuid.uuid8` respectively, as specified in :rfc:`9562`.
* Add support for UUID versions 6, 7, and 8 via :func:`uuid.uuid6`,

@merwok merwok Mar 3, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Not to be pedantic, but is via correct? It seems to suggest that the functions are the only thing that support these versions, but the support is added in the UUID class and the functions are there too as a convenience.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@picnixz picnixz Mar 3, 2025

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

Yes, but in general, we don't really want people to directly use the UUID class. Strictly speaking, we're only adding the support for the version value but we don't check how it's been generated.

I prefer users to actually use the factories. Otherwise, I can say that the UUID class now accepts version to be 6, 7, or 8.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@merwok merwok Mar 3, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

I slightly disagree but won’t argue 🙂

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
merwok
merwok reviewed Mar 3, 2025
View reviewed changes
Comment thread Lib/uuid.py
int_uuid_6 |= _RFC_4122_VERSION_6_FLAGS
return UUID._from_int(int_uuid_6)

_last_timestamp_v7 = None

@merwok merwok Mar 3, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@picnixz picnixz Mar 3, 2025

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

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).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@merwok merwok Mar 3, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@picnixz picnixz Mar 3, 2025

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

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).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@picnixz picnixz Mar 3, 2025

Copy link
Copy Markdown
Member Author

There was a problem hiding this comment.

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).

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@vstinner vstinner Mar 3, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@merwok merwok Mar 3, 2025 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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]

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions

@merwok merwok Mar 3, 2025

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

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!

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
merwok
merwok reviewed Mar 3, 2025
View reviewed changes
Comment thread Lib/uuid.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

picnixz added 2 commits March 3, 2025 18:24
@picnixz
constructor -> factory in labels
54d07ae
@picnixz
reword prolog
6d76389
picnixz
picnixz commented Mar 3, 2025
View reviewed changes
Comment thread Doc/library/uuid.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@picnixz
'is outside the scope' -> 'exceeds the scope'
bd4ab55
picnixz
picnixz commented Mar 3, 2025
View reviewed changes
Comment thread Doc/library/uuid.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

Comment thread Lib/uuid.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

picnixz added 2 commits March 3, 2025 19:02
@picnixz
Apply suggestions from code review
e9ddb74
@picnixz
apply PEP-8 only for UUID6, UUID7 and UUID8
8755de0
Opportunity to make new code more PEP-8.
@picnixz

picnixz commented Mar 3, 2025

Copy link
Copy Markdown
Member Author

@hugovk I eventually settled on doing PEP-8 for the new UUID functions and left the rest of the module untouched. Ideally, I really wanted a cosmetic-only PR because the module is slow to evolve, but I don't want to spend too much time trying to convince other core devs. So I'll just update new code.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

merwok
merwok reviewed Mar 3, 2025
View reviewed changes
Comment thread Doc/library/uuid.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@merwok
small fix
12d7ad4
minimize git diff + remove now unwanted article
merwok
merwok approved these changes Mar 3, 2025
View reviewed changes
hugovk
hugovk approved these changes Mar 3, 2025
View reviewed changes
Comment thread Doc/library/uuid.rst Resolved

Uh oh!

There was an error while loading. Please reload this page.

@picnixz @hugovk
avoid complex language :)
560d87c
Co-authored-by: Hugo van Kemenade <[email protected]>
@picnixz
picnixz merged commit 3929af5 into python:main Mar 4, 2025
@bedevere-app bedevere-app Bot removed the awaiting merge label Mar 4, 2025
@picnixz
picnixz deleted the uuid-v7-method-1 branch March 4, 2025 09:47
@picnixz

picnixz commented Mar 4, 2025

Copy link
Copy Markdown
Member Author

We finally landed that PR! Thank you everyone for the feedback and the patience :)

vstinner, dolfinus, dalito, hugovk, ApostolFet, vlkorsakov, VaZark, melya, splch, ferdinandkeller, and 5 more reacted with hooray emoji merwok, ollandos, glentner, and tiangolo reacted with rocket emoji
All reactions
  • 🎉 15 reactions
  • 🚀 4 reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

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

Reviewers

@vstinner vstinner vstinner approved these changes
@merwok merwok merwok approved these changes
@hugovk hugovk hugovk approved these changes
+3 more reviewers
@mastizada mastizada mastizada left review comments
@gbdlin gbdlin gbdlin left review comments
@jnoring-pw jnoring-pw jnoring-pw left review comments
Reviewers whose approvals may not affect merge requirements

Uh oh!

There was an error while loading. Please reload this page.

Assignees

@picnixz picnixz

Labels

type-feature A feature request or enhancement

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

10 participants

@picnixz @sergeyprokhorenko @pretoriusdre @hugovk @Davidamgad2 @vstinner @merwok @mastizada @gbdlin @jnoring-pw
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.
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.