Skip to main content

Backport of Python 3.14 UUID module (RFC 9562) for Python 3.9+

Project description

uuid-backport

PyPI version Python versions PyPI Downloads Tests codecov

Backport of Python 3.14's UUID module (RFC 9562) for Python 3.9+

Features

  • UUID v6: Reordered timestamp UUID for improved database locality
  • UUID v7: Timestamp-based UUID with millisecond precision and monotonicity
  • UUID v8: Custom 3-block UUID for application-specific use cases
  • NIL/MAX UUID: Special UUID constants defined in RFC 9562

Installation

pip install uuid-backport

Or with uv:

uv add uuid-backport

Usage

UUID v6 - Improved Database Locality

from uuid_backport import uuid6

# Similar to UUID v1 but with reordered timestamp fields
id = uuid6()
print(id)  # Example: 1ef4a7c8-9b2c-6000-8000-0242ac120002

# Maintains time-based ordering
ids = [uuid6() for _ in range(5)]
assert ids == sorted(ids)

UUID v7 - Timestamp + Monotonicity

from uuid_backport import uuid7

# Millisecond-precision timestamp with guaranteed monotonicity
id1 = uuid7()
id2 = uuid7()
assert id1 < id2  # Always true, even within the same millisecond

# Ideal for distributed systems
ids = [uuid7() for _ in range(1000)]
assert ids == sorted(ids)

UUID v8 - Custom UUID

from uuid_backport import uuid8

# Create UUID with custom 3-block structure
# a: 48-bit, b: 12-bit, c: 62-bit
custom_id = uuid8(
    a=0x123456789ABC,      # Application prefix
    b=0xDEF,                # Type identifier
    c=0x123456789ABCDEF0    # Sequence number
)

# Generate random UUID v8
random_id = uuid8()

NIL/MAX UUID

from uuid_backport import NIL, MAX

print(NIL)  # 00000000-0000-0000-0000-000000000000
print(MAX)  # ffffffff-ffff-ffff-ffff-ffffffffffff

# Useful for validation
def is_valid_uuid(uuid):
    return NIL < uuid < MAX

Compatibility with Standard Library

import uuid
from uuid_backport import uuid6, uuid7, uuid8, NIL, MAX

# Use standard library for v1-v5
v1 = uuid.uuid1()
v4 = uuid.uuid4()
v5 = uuid.uuid5(uuid.NAMESPACE_DNS, 'example.com')

# Use backport for v6-v8
v6 = uuid6()
v7 = uuid7()
v8 = uuid8()

# All are instances of uuid.UUID
assert isinstance(v6, uuid.UUID)
assert isinstance(v7, uuid.UUID)

UUID Version Selection Guide

  • UUID v1/v6: MAC address + timestamp based

    • v1: Legacy, poor database locality
    • v6: Improved v1, optimized for database indexes
  • UUID v4: Fully random

    • Most widely used
    • No inherent ordering
    • Extremely low collision probability
  • UUID v7: Timestamp + random + monotonic

    • Recommended for most new projects
    • Time-ordered and sortable
    • Ideal for distributed systems
    • Efficient database indexing
  • UUID v8: Custom structure

    • For specialized requirements only
    • Application-specific optimizations

Development

Setup

git clone https://github.com/line1029/uuid-backport.git
cd uuid-backport

# Install test dependencies
uv sync --group test

# Install lint tools
uv sync --group lint

Testing

# Test with current Python version
uv run pytest tests/ -v

# Test with all Python versions (3.9-3.13) + lint + typing
tox

# Test specific environment
tox -e py311          # Python 3.11 only
tox -e lint           # Linting only
tox -e typing         # Type checking only

Code Quality

# Run linting and format checking
tox -e lint

# Run type checking
tox -e typing

# Or run directly
uv run ruff check .
uv run ruff format --check .
uv run mypy .

Formatting

uv run ruff format .

License

This project is licensed under the terms of the MIT License.

However, this package contains code that has been backported from the Python 3.14 standard library's uuid module. The original code from the Python standard library is licensed under the Python Software Foundation License Version 2.

For clarity:

  • The backporting effort, modifications, and any additional code written specifically for this project are covered by the MIT License.
  • The underlying algorithms and code structure originating from the Python standard library are subject to the terms of the PSF License.

A copy of the MIT License can be found in the LICENSE file, and a copy of the PSF License is included as LICENSE.PSF.

References

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

uuid_backport-0.1.2.tar.gz (8.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

uuid_backport-0.1.2-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

Details for the file uuid_backport-0.1.2.tar.gz.

File metadata

  • Download URL: uuid_backport-0.1.2.tar.gz
  • Upload date:
  • Size: 8.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.11 {"installer":{"name":"uv","version":"0.9.11"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for uuid_backport-0.1.2.tar.gz
Algorithm Hash digest
SHA256 e4b7473a6bf2cb83d3fd508af5cd81a40f7277d0e280302a603c2e7b97f182a5
MD5 aa94deb2a24b017a113b848100614f03
BLAKE2b-256 9baad6368101e290e414287e4df38978d83715ea1037bc244c9ff1064794863f

See more details on using hashes here.

File details

Details for the file uuid_backport-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: uuid_backport-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 8.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.11 {"installer":{"name":"uv","version":"0.9.11"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

File hashes

Hashes for uuid_backport-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 61f79d724c9e7f63d0b3be3dc882958799d726f0cb5a4035321b22d432e68a5d
MD5 18b7641ce3e736bc350b0cce954b841e
BLAKE2b-256 fe1998ddec24b639bb47c238e6c729a13d0e63960cf284a22a99edc0b5345516

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page