Skip to main content

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

Project description

uuid-backport

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

MIT License - Based on Python 3.14's uuid module

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.0.post1.tar.gz (7.7 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.0.post1-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

Details for the file uuid_backport-0.1.0.post1.tar.gz.

File metadata

  • Download URL: uuid_backport-0.1.0.post1.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.8

File hashes

Hashes for uuid_backport-0.1.0.post1.tar.gz
Algorithm Hash digest
SHA256 13c1a8b4d6ef882c2aae1eb8015f0e34a4f6b64b982c0b596eb479f349390c0c
MD5 0b910a2e0d9bd847e042bd99f727194d
BLAKE2b-256 dba598c013c544d13e189634981cbf348f4dc1b6783385c62e292a143e6719cc

See more details on using hashes here.

File details

Details for the file uuid_backport-0.1.0.post1-py3-none-any.whl.

File metadata

File hashes

Hashes for uuid_backport-0.1.0.post1-py3-none-any.whl
Algorithm Hash digest
SHA256 0056dad1ce302d1487ecceeebb0b6c9d15f843fcf60f602a3d3786895ced0c83
MD5 c5e42a49a94ef4a4d30731c2bcf32b77
BLAKE2b-256 9f73ae43257dd36ec2311d5bf869a8037d8b0700b55767cd4531a63920d78191

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