Skip to content

Commit

Permalink
Add uuid.NIL and uuid.MAX
Browse files Browse the repository at this point in the history
  • Loading branch information
ngnpope committed Jan 2, 2025
1 parent 58e9f95 commit 50c6294
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 0 deletions.
25 changes: 25 additions & 0 deletions Doc/library/uuid.rst
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,24 @@ of the :attr:`~UUID.variant` attribute:

Reserved for future definition.

The :mod:`uuid` module defines the special Nil and Max UUID values:


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

.. versionadded:: 3.14


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

.. versionadded:: 3.14


.. seealso::

Expand Down Expand Up @@ -380,6 +398,13 @@ 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')

.. _uuid-cli-example:

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

* :data:`uuid.NIL` and :data:`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`.)

zipinfo
-------

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

def test_nil_uuid(self):
self.assertEqual(
self.uuid.NIL,
self.uuid.UUID('00000000-0000-0000-0000-000000000000'),
)

def test_max_uuid(self):
self.assertEqual(
self.uuid.MAX,
self.uuid.UUID('ffffffff-ffff-ffff-ffff-ffffffffffff'),
)

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

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 @@
:data:`uuid.NIL` and :data:`uuid.MAX` are now available to represent the Nil
and Max UUID formats as defined by :rfc:`9562`.

0 comments on commit 50c6294

Please sign in to comment.