Skip to content
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

Improve typing for function uuid7 #3

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all 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
66 changes: 57 additions & 9 deletions uuid_extensions/uuid7.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,62 @@
import os
import struct
import time
from typing import Callable, Optional, Union
import uuid
from typing import Callable, Literal, Optional, Union, overload

# Expose function used by uuid7() to get current time in nanoseconds
# since the Unix epoch.
time_ns = time.time_ns


@overload
def uuid7(
ns: Optional[int] = None,
*,
as_type: Literal[None] = None,
time_func: Callable[[], int] = time_ns,
_last=[],
_last_as_of=[],
) -> uuid.UUID: ...


@overload
def uuid7(
ns: Optional[int] = None,
*,
as_type: Literal["str"],
time_func: Callable[[], int] = time_ns,
_last=[],
_last_as_of=[],
) -> str: ...


@overload
def uuid7(
ns: Optional[int] = None,
*,
as_type: Literal["int"],
time_func: Callable[[], int] = time_ns,
_last=[],
_last_as_of=[],
) -> int: ...


@overload
def uuid7(
ns: Optional[int] = None,
*,
as_type: Literal["bytes"],
time_func: Callable[[], int] = time_ns,
_last=[],
_last_as_of=[],
) -> bytes: ...


def uuid7(
ns: Optional[int] = None,
as_type: Optional[str] = None,
*,
as_type: Literal[None, "str", "int", "hex", "bytes"] = None,
time_func: Callable[[], int] = time_ns,
_last=[0, 0, 0, 0],
_last_as_of=[0, 0, 0, 0],
Expand Down Expand Up @@ -205,8 +251,10 @@ def check_timing_precision(
timing_funcs = [
("time.time_ns()", time.time_ns),
("time.perf_counter_ns()", time.perf_counter_ns),
("datetime.datetime.utcnow", lambda: int(
datetime.datetime.utcnow().timestamp() * 1_000_000_000)),
(
"datetime.datetime.utcnow",
lambda: int(datetime.datetime.utcnow().timestamp() * 1_000_000_000),
),
]
if timing_func is not None:
timing_funcs.append(("user-supplied", timing_func))
Expand All @@ -225,9 +273,9 @@ def check_timing_precision(
precision_ns = elapsed_ns / len(values)
ideal_precision_ns = elapsed_ns / ctr
lines.append(
f"{desc} has a timing precision of {precision_ns:0,.0f}ns \
rather than {ideal_precision_ns:0,.0f}ns ({ctr:,} samples of which \
{len(values):,} are distinct, in {elapsed_ns / 1_000_000_000:0.2f}s)"
f"{desc} has a timing precision of {precision_ns:0,.0f}ns rather than"
f" {ideal_precision_ns:0,.0f}ns ({ctr:,} samples of which"
f" {len(values):,} are distinct, in {elapsed_ns / 1_000_000_000:0.2f}s)"
)

return "\n".join(lines)
Expand Down Expand Up @@ -276,8 +324,8 @@ def timestamp_ns(
return None
else:
raise ValueError(
f"{str(s)} is a version {uuid_version} UUID, \
not v7 so we cannot extract the timestamp."
f"{str(s)} is a version {uuid_version} UUID, not v7 so we cannot extract"
" the timestamp."
)


Expand Down