Skip to content

Commit

Permalink
feat(cli): Fix CLI entrypoint
Browse files Browse the repository at this point in the history
  • Loading branch information
mdomke committed Oct 11, 2024
1 parent c60e7f2 commit fc02f34
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 16 deletions.
11 changes: 3 additions & 8 deletions tests/test_ulid.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from __future__ import annotations

import json
import time
import uuid
from collections.abc import Callable
from datetime import datetime
from datetime import timedelta
from datetime import timezone
from typing import TYPE_CHECKING
from typing import Optional
from typing import Union

import pytest
Expand All @@ -19,10 +18,6 @@
from ulid import ULID


if TYPE_CHECKING: # pragma: no cover
from collections.abc import Callable


def utcnow() -> datetime:
return datetime.now(timezone.utc)

Expand Down Expand Up @@ -220,7 +215,7 @@ def test_pydantic_protocol() -> None:
ulid = ULID()

class Model(BaseModel):
ulid: ULID | None = None
ulid: Optional[ULID, None] = None # noqa: FA100

for value in [ulid, str(ulid), int(ulid), bytes(ulid)]:
model = Model(ulid=value)
Expand Down
4 changes: 3 additions & 1 deletion ulid/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ def parse(cls: type[U], value: Any) -> U:
return cls.from_hex(value)
raise ValueError(f"Cannot parse ULID from string of length {len_value}")
if isinstance(value, int):
return cls.from_int(value)
if len(str(value)) == constants.INT_REPR_LEN:
return cls.from_int(value)
return cls.from_timestamp(value)
if isinstance(value, float):
return cls.from_timestamp(value)
if isinstance(value, datetime):
Expand Down
15 changes: 8 additions & 7 deletions ulid/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -151,17 +151,18 @@ def show(args: argparse.Namespace) -> str:
return ulid.datetime.isoformat()
return textwrap.dedent(
f"""
ULID: {ulid!s}
Hex: {ulid.hex}
Int: {int(ulid)}
Timestamp: {ulid.timestamp}
Datetime: {ulid.datetime.isoformat()}
"""
ULID: {ulid!s}
Hex: {ulid.hex}
Int: {int(ulid)}
Timestamp: {ulid.timestamp}
Datetime: {ulid.datetime.isoformat()}
"""
).strip()


def entrypoint() -> None: # pragma: no cover
pass
if (value := main(sys.argv[1:])) is not None:
print(value) # noqa: T201


if __name__ == "__main__": # pragma: no cover
Expand Down
2 changes: 2 additions & 0 deletions ulid/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@
RANDOMNESS_REPR_LEN = 16
REPR_LEN = TIMESTAMP_REPR_LEN + RANDOMNESS_REPR_LEN

INT_REPR_LEN = 37

UUID_LEN = 32
UUID_REPR_LEN = 36 # UUID with dash-separated segments

0 comments on commit fc02f34

Please sign in to comment.