Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-128427: Add uuid.NIL and uuid.MAX #128429

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
27 changes: 27 additions & 0 deletions Doc/library/uuid.rst
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,25 @@ of the :attr:`~UUID.variant` attribute:
Reserved for future definition.


The :mod:`uuid` module defines the special Nil and Max UUID values:
ngnpope marked this conversation as resolved.
Show resolved Hide resolved


.. data:: NIL

A special form of UUID that is specified to have all 128 bits set to zero
according to :rfc:`RFC 9562, §5.9 <9562#section-5.9>`.
Copy link
Member

Choose a reason for hiding this comment

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

Linking directly to the section is helpful, perhaps we only need to mention the RFC number here? If so, we could update the same thing for uuid8 above.

Suggested change
according to :rfc:`RFC 9562, §5.9 <9562#section-5.9>`.
according to :rfc:`RFC 9562 <9562#section-5.9>`.


.. versionadded:: next


.. data:: MAX

A special form of UUID that is specified to have all 128 bits set to one
according to :rfc:`RFC 9562, §5.10 <9562#section-5.10>`.
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
according to :rfc:`RFC 9562, §5.10 <9562#section-5.10>`.
according to :rfc:`RFC 9562 <9562#section-5.10>`.


.. versionadded:: next


.. seealso::

:rfc:`9562` - A Universally Unique IDentifier (UUID) URN Namespace
Expand Down Expand Up @@ -380,6 +399,14 @@ Here are some examples of typical usage of the :mod:`uuid` module::
>>> uuid.UUID(bytes=x.bytes)
UUID('00010203-0405-0607-0809-0a0b0c0d0e0f')

>>> # get the Nil UUID
>>> uuid.NIL
UUID('00000000-0000-0000-0000-000000000000')

>>> # get the Max UUID
>>> uuid.MAX
UUID('ffffffff-ffff-ffff-ffff-ffffffffffff')
ngnpope marked this conversation as resolved.
Show resolved Hide resolved


.. _uuid-cli-example:

Expand Down
5 changes: 5 additions & 0 deletions Doc/whatsnew/3.14.rst
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,11 @@ uuid
in :rfc:`9562`.
(Contributed by Bénédikt Tran in :gh:`89083`.)

* :const:`uuid.NIL` and :const:`uuid.MAX` are now available to represent the
Nil and Max UUID formats as defined by :rfc:`9562`.
(Contributed by Nick Pope in :gh:`128427`.)


ngnpope marked this conversation as resolved.
Show resolved Hide resolved
zipinfo
-------

Expand Down
20 changes: 20 additions & 0 deletions Lib/test/test_uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,26 @@ def get_command_stdout(command, args):
class BaseTestUUID:
uuid = None

def test_nil_uuid(self):
nil_uuid = self.uuid.NIL

s = '00000000-0000-0000-0000-000000000000'
i = 0x00000000000000000000000000000000
ngnpope marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(nil_uuid, self.uuid.UUID(s))
self.assertEqual(nil_uuid, self.uuid.UUID(int=i))
self.assertEqual(nil_uuid.int, i)
self.assertEqual(str(nil_uuid), s)

def test_max_uuid(self):
max_uuid = self.uuid.MAX

s = 'ffffffff-ffff-ffff-ffff-ffffffffffff'
i = 0xffffffffffffffffffffffffffffffff
ngnpope marked this conversation as resolved.
Show resolved Hide resolved
ngnpope marked this conversation as resolved.
Show resolved Hide resolved
self.assertEqual(max_uuid, self.uuid.UUID(s))
self.assertEqual(max_uuid, self.uuid.UUID(int=i))
self.assertEqual(max_uuid.int, i)
self.assertEqual(str(max_uuid), s)
ngnpope marked this conversation as resolved.
Show resolved Hide resolved

def test_safe_uuid_enum(self):
class CheckedSafeUUID(enum.Enum):
safe = 0
Expand Down
13 changes: 13 additions & 0 deletions Lib/uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,14 @@
# make a UUID from a 16-byte string
>>> uuid.UUID(bytes=x.bytes)
UUID('00010203-0405-0607-0809-0a0b0c0d0e0f')

# get the Nil UUID
>>> uuid.NIL
UUID('00000000-0000-0000-0000-000000000000')

# get the Max UUID
>>> uuid.MAX
UUID('ffffffff-ffff-ffff-ffff-ffffffffffff')
"""

import os
Expand Down Expand Up @@ -799,5 +807,10 @@ def main():
NAMESPACE_OID = UUID('6ba7b812-9dad-11d1-80b4-00c04fd430c8')
NAMESPACE_X500 = UUID('6ba7b814-9dad-11d1-80b4-00c04fd430c8')

# RFC 9562 Sections 5.9 and 5.10 define the special Nil and Max UUID formats.

NIL = UUID('00000000-0000-0000-0000-000000000000')
MAX = UUID('ffffffff-ffff-ffff-ffff-ffffffffffff')
picnixz marked this conversation as resolved.
Show resolved Hide resolved

if __name__ == "__main__":
main()
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -1473,6 +1473,7 @@ Michael Pomraning
Martin Pool
Iustin Pop
Claudiu Popa
Nick Pope
John Popplewell
Matheus Vieira Portela
Davin Potts
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
:const:`uuid.NIL` and :const:`uuid.MAX` are now available to represent the Nil
and Max UUID formats as defined by :rfc:`9562`.
Loading