Skip to main content

Module

UUID utility functions for handling UUID version detection and conversion. This module provides utilities for detecting UUID7s and converting them to UUID4s to ensure compatibility with systems that expect UUID4 format.

convert_uuid_if_uuid7

def convert_uuid_if_uuid7(uuid_obj: Optional[Union[UUID, str]]) -> Optional[UUID]
Convert a UUID to UUID4 if it’s a UUID7, otherwise return the original UUID. This is a convenience function that checks if a UUID is version 7 and converts it to UUID4 if so, otherwise returns the original UUID as a UUID object. Arguments
  • uuid_obj (Optional[Union[UUID, str]]): The UUID to potentially convert, either as a UUID object, string, or None.
Returns
  • Optional[UUID]: The converted UUID4 if input was UUID7, the original UUID object if not UUID7, or None if input was None.

is_uuid7

def is_uuid7(uuid_obj: Union[UUID, str]) -> bool
Check if a UUID is version 7. Arguments
  • uuid_obj (Union[UUID, str]): The UUID to check, either as a UUID object or string.
Returns
  • bool: True if the UUID is version 7, False otherwise.

uuid7_to_uuid4

def uuid7_to_uuid4(uuid_obj: Union[UUID, str]) -> UUID
Convert a UUID7 to a UUID4 by hashing its string representation. This function takes a UUID7 and generates a deterministic UUID4 by hashing the string representation of the UUID7 using SHA-256 and using the first 16 bytes to create a new UUID4. Arguments
  • uuid_obj (Union[UUID, str]): The UUID7 to convert, either as a UUID object or string.
Raises
  • ValueError: If the input is not a valid UUID or not a UUID7.
Returns
  • UUID: A UUID4 generated from the hash of the input UUID7.