Skip to content

Commit 50c6294

Browse files
committed
Add uuid.NIL and uuid.MAX
1 parent 58e9f95 commit 50c6294

File tree

6 files changed

+57
-0
lines changed

6 files changed

+57
-0
lines changed

Doc/library/uuid.rst

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,24 @@ of the :attr:`~UUID.variant` attribute:
288288

289289
Reserved for future definition.
290290

291+
The :mod:`uuid` module defines the special Nil and Max UUID values:
292+
293+
294+
.. data:: NIL
295+
296+
A special form of UUID that is specified to have all 128 bits set to zero
297+
according to :rfc:`RFC 9562, §5.9 <9562#section-5.9>`.
298+
299+
.. versionadded:: 3.14
300+
301+
302+
.. data:: MAX
303+
304+
A special form of UUID that is specified to have all 128 bits set to one
305+
according to :rfc:`RFC 9562, §5.10 <9562#section-5.10>`.
306+
307+
.. versionadded:: 3.14
308+
291309

292310
.. seealso::
293311

@@ -380,6 +398,13 @@ Here are some examples of typical usage of the :mod:`uuid` module::
380398
>>> uuid.UUID(bytes=x.bytes)
381399
UUID('00010203-0405-0607-0809-0a0b0c0d0e0f')
382400

401+
>>> # get the Nil UUID
402+
>>> uuid.NIL
403+
UUID('00000000-0000-0000-0000-000000000000')
404+
405+
>>> # get the Max UUID
406+
>>> uuid.MAX
407+
UUID('ffffffff-ffff-ffff-ffff-ffffffffffff')
383408

384409
.. _uuid-cli-example:
385410

Doc/whatsnew/3.14.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,10 @@ uuid
665665
in :rfc:`9562`.
666666
(Contributed by Bénédikt Tran in :gh:`89083`.)
667667

668+
* :data:`uuid.NIL` and :data:`uuid.MAX` are now available to represent the Nil
669+
and Max UUID formats as defined by :rfc:`9562`. (Contributed by Nick Pope in
670+
:gh:`128427`.)
671+
668672
zipinfo
669673
-------
670674

Lib/test/test_uuid.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ def get_command_stdout(command, args):
3434
class BaseTestUUID:
3535
uuid = None
3636

37+
def test_nil_uuid(self):
38+
self.assertEqual(
39+
self.uuid.NIL,
40+
self.uuid.UUID('00000000-0000-0000-0000-000000000000'),
41+
)
42+
43+
def test_max_uuid(self):
44+
self.assertEqual(
45+
self.uuid.MAX,
46+
self.uuid.UUID('ffffffff-ffff-ffff-ffff-ffffffffffff'),
47+
)
48+
3749
def test_safe_uuid_enum(self):
3850
class CheckedSafeUUID(enum.Enum):
3951
safe = 0

Lib/uuid.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@
4242
# make a UUID from a 16-byte string
4343
>>> uuid.UUID(bytes=x.bytes)
4444
UUID('00010203-0405-0607-0809-0a0b0c0d0e0f')
45+
46+
# get the Nil UUID
47+
>>> uuid.NIL
48+
UUID('00000000-0000-0000-0000-000000000000')
49+
50+
# get the Max UUID
51+
>>> uuid.MAX
52+
UUID('ffffffff-ffff-ffff-ffff-ffffffffffff')
4553
"""
4654

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

810+
# RFC 9562 Sections 5.9 and 5.10 define the special Nil and Max UUID formats.
811+
812+
NIL = UUID('00000000-0000-0000-0000-000000000000')
813+
MAX = UUID('ffffffff-ffff-ffff-ffff-ffffffffffff')
814+
802815
if __name__ == "__main__":
803816
main()

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,6 +1473,7 @@ Michael Pomraning
14731473
Martin Pool
14741474
Iustin Pop
14751475
Claudiu Popa
1476+
Nick Pope
14761477
John Popplewell
14771478
Matheus Vieira Portela
14781479
Davin Potts
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
:data:`uuid.NIL` and :data:`uuid.MAX` are now available to represent the Nil
2+
and Max UUID formats as defined by :rfc:`9562`.

0 commit comments

Comments
 (0)