Skip to content

Update uuid to uuid7 #2303

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

Draft
wants to merge 8 commits into
base: main
Choose a base branch
from
Draft
Changes from 6 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
39 changes: 29 additions & 10 deletions kombu/utils/uuid.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,34 @@
"""UUID utilities."""
from __future__ import annotations

from typing import Callable
from uuid import UUID, uuid4
from typing import Literal
from uuid import uuid4

try:
# Python 3.11 or later
from uuid import uuid7

Choose a reason for hiding this comment

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

Suggested change
# Python 3.11 or later
# Python 3.14 or later

except ImportError:
# Fallback for older Python versions
def uuid7():
"""Fallback to UUID4 if UUID7 is not available."""
return uuid4()

def uuid(_uuid: Callable[[], UUID] = uuid4) -> str:
"""Generate unique id in UUID4 format.

See Also
--------
For now this is provided by :func:`uuid.uuid4`.
def generate_uuid(version: Literal[4, 7] = 7) -> str:
"""Generate a unique ID based on the specified UUID version (4 or 7).

Parameters
----------
version : Literal[4, 7]
The UUID version to use for generating the unique ID. Defaults to 7.
If UUID7 is unavailable, it falls back to UUID4 regardless of the input.

Returns
-------
str
A string representation of the generated UUID.
"""
return str(_uuid())
if version == 7 and 'uuid7' in globals():
return str(uuid7())
elif version == 4:
return str(uuid4())
else:
raise ValueError("Invalid UUID version. Please choose either 4 or 7.")
Loading