python-ulid 4.0.0__tar.gz → 4.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {python_ulid-4.0.0 → python_ulid-4.1.0}/.github/workflows/lint-and-test.yml +4 -2
- {python_ulid-4.0.0 → python_ulid-4.1.0}/CHANGELOG.rst +29 -0
- {python_ulid-4.0.0 → python_ulid-4.1.0}/CONTRIBUTING.md +1 -1
- {python_ulid-4.0.0 → python_ulid-4.1.0}/PKG-INFO +4 -5
- {python_ulid-4.0.0 → python_ulid-4.1.0}/README.rst +3 -3
- {python_ulid-4.0.0 → python_ulid-4.1.0}/docs/source/conf.py +2 -2
- {python_ulid-4.0.0 → python_ulid-4.1.0}/pyproject.toml +16 -15
- {python_ulid-4.0.0 → python_ulid-4.1.0}/tests/test_cli.py +50 -1
- {python_ulid-4.0.0 → python_ulid-4.1.0}/tests/test_ulid.py +16 -9
- {python_ulid-4.0.0 → python_ulid-4.1.0}/ulid/__init__.py +10 -19
- {python_ulid-4.0.0 → python_ulid-4.1.0}/ulid/__main__.py +8 -4
- {python_ulid-4.0.0 → python_ulid-4.1.0}/ulid/base32.py +3 -1
- python_ulid-4.1.0/uv.lock +1117 -0
- python_ulid-4.0.0/uv.lock +0 -1038
- {python_ulid-4.0.0 → python_ulid-4.1.0}/.envrc +0 -0
- {python_ulid-4.0.0 → python_ulid-4.1.0}/.github/workflows/publish.yml +0 -0
- {python_ulid-4.0.0 → python_ulid-4.1.0}/.gitignore +0 -0
- {python_ulid-4.0.0 → python_ulid-4.1.0}/.pre-commit-config.yaml +0 -0
- {python_ulid-4.0.0 → python_ulid-4.1.0}/.readthedocs.yml +0 -0
- {python_ulid-4.0.0 → python_ulid-4.1.0}/.ruff_defaults.toml +0 -0
- {python_ulid-4.0.0 → python_ulid-4.1.0}/AGENTS.md +0 -0
- {python_ulid-4.0.0 → python_ulid-4.1.0}/CONTEXT.md +0 -0
- {python_ulid-4.0.0 → python_ulid-4.1.0}/LICENSE +0 -0
- {python_ulid-4.0.0 → python_ulid-4.1.0}/docs/Makefile +0 -0
- {python_ulid-4.0.0 → python_ulid-4.1.0}/docs/requirements.txt +0 -0
- {python_ulid-4.0.0 → python_ulid-4.1.0}/docs/source/api.rst +0 -0
- {python_ulid-4.0.0 → python_ulid-4.1.0}/docs/source/changelog.rst +0 -0
- {python_ulid-4.0.0 → python_ulid-4.1.0}/docs/source/index.rst +0 -0
- {python_ulid-4.0.0 → python_ulid-4.1.0}/logo.png +0 -0
- {python_ulid-4.0.0 → python_ulid-4.1.0}/tests/__init__.py +0 -0
- {python_ulid-4.0.0 → python_ulid-4.1.0}/tests/test_base32.py +0 -0
- {python_ulid-4.0.0 → python_ulid-4.1.0}/ulid/constants.py +0 -0
- {python_ulid-4.0.0 → python_ulid-4.1.0}/ulid/py.typed +0 -0
|
@@ -47,7 +47,7 @@ jobs:
|
|
|
47
47
|
name: Run tests
|
|
48
48
|
strategy:
|
|
49
49
|
matrix:
|
|
50
|
-
python-version: ["3.
|
|
50
|
+
python-version: ["3.11", "3.12", "3.13", "3.14"]
|
|
51
51
|
steps:
|
|
52
52
|
- uses: actions/checkout@v7
|
|
53
53
|
- name: Install uv
|
|
@@ -57,6 +57,8 @@ jobs:
|
|
|
57
57
|
|
|
58
58
|
- name: Test
|
|
59
59
|
run: uv run poe test
|
|
60
|
+
env:
|
|
61
|
+
COVERAGE_FILE: .coverage.${{ matrix.python-version }}
|
|
60
62
|
|
|
61
63
|
- name: Upload coverage data
|
|
62
64
|
uses: actions/upload-artifact@v7
|
|
@@ -98,4 +100,4 @@ jobs:
|
|
|
98
100
|
runs-on: ubuntu-latest
|
|
99
101
|
steps:
|
|
100
102
|
- uses: actions/checkout@v7
|
|
101
|
-
- uses: hynek/build-and-inspect-python-package@
|
|
103
|
+
- uses: hynek/build-and-inspect-python-package@67230f7b8410ae0052b39c1efc89d89fff43ff10 # v3.0.2
|
|
@@ -5,6 +5,32 @@ Changelog
|
|
|
5
5
|
|
|
6
6
|
Versions follow `Semantic Versioning <http://www.semver.org>`_
|
|
7
7
|
|
|
8
|
+
`Unreleased`_
|
|
9
|
+
-------------
|
|
10
|
+
|
|
11
|
+
`4.1.0`_ - 2026-10-27
|
|
12
|
+
---------------------
|
|
13
|
+
Changed
|
|
14
|
+
~~~~~~~
|
|
15
|
+
* Dropped support for Python 3.10.
|
|
16
|
+
|
|
17
|
+
Fixed
|
|
18
|
+
~~~~~
|
|
19
|
+
* The ``typing-extensions`` dependency is now also installed on Python 3.11, where it is required
|
|
20
|
+
for ``typing_extensions.override``.
|
|
21
|
+
* Print generated and converted values when using ``python -m ulid``, matching the
|
|
22
|
+
``ulid`` console command `@tongwentao <https://github.com/tongwentao>`_.
|
|
23
|
+
* :meth:`.ULID.from_str` accepts lower case strings again, as the ULID spec defines the string
|
|
24
|
+
representation as case insensitive. Lower case input has been rejected since 2.6.0
|
|
25
|
+
`@reyhaan07 <https://github.com/reyhaan07>`_.
|
|
26
|
+
|
|
27
|
+
`4.0.1`_ - 2026-07-20
|
|
28
|
+
---------------------
|
|
29
|
+
Changed
|
|
30
|
+
~~~~~~~
|
|
31
|
+
* Renamed ``ULID.from_uuidv7`` to :meth:`.ULID.from_uuid7` for naming symmetry with
|
|
32
|
+
:meth:`.ULID.to_uuid7`, correcting an inconsistency introduced in 3.2.0.
|
|
33
|
+
|
|
8
34
|
`4.0.0`_ - 2026-07-20
|
|
9
35
|
---------------------
|
|
10
36
|
|
|
@@ -258,6 +284,9 @@ Changed
|
|
|
258
284
|
* The package now has no external dependencies.
|
|
259
285
|
* The test-coverage has been raised to 100%.
|
|
260
286
|
|
|
287
|
+
.. _Unreleased: https://github.com/mdomke/python-ulid/compare/4.1.0...HEAD
|
|
288
|
+
.. _4.1.0: https://github.com/mdomke/python-ulid/compare/4.0.1...4.1.0
|
|
289
|
+
.. _4.0.1: https://github.com/mdomke/python-ulid/compare/4.0.0...4.0.1
|
|
261
290
|
.. _4.0.0: https://github.com/mdomke/python-ulid/compare/3.2.1...4.0.0
|
|
262
291
|
.. _3.2.1: https://github.com/mdomke/python-ulid/compare/3.2.0...3.2.1
|
|
263
292
|
.. _3.2.0: https://github.com/mdomke/python-ulid/compare/3.1.0...3.2.0
|
|
@@ -33,7 +33,7 @@ If you have [Flox](https://flox.dev/) installed, setting up your environment is
|
|
|
33
33
|
If Flox is not an option, you can set up your environment manually. We use [uv](https://github.com/astral-sh/uv) for fast, robust package and dependency management.
|
|
34
34
|
|
|
35
35
|
1. **Prerequisites**:
|
|
36
|
-
- **Python**: `>= 3.
|
|
36
|
+
- **Python**: `>= 3.11`
|
|
37
37
|
- **uv**: Install via `curl -LsSf https://astral.sh/uv/install.sh | sh` (or your preferred package manager)
|
|
38
38
|
|
|
39
39
|
2. **Fork and clone** the repository:
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
2
|
Name: python-ulid
|
|
3
|
-
Version: 4.
|
|
3
|
+
Version: 4.1.0
|
|
4
4
|
Summary: Universally unique lexicographically sortable identifier
|
|
5
5
|
Project-URL: Homepage, https://github.com/mdomke/python-ulid
|
|
6
6
|
Project-URL: Documentation, https://python-ulid.readthedocs.io
|
|
@@ -18,14 +18,13 @@ Classifier: License :: OSI Approved :: MIT License
|
|
|
18
18
|
Classifier: Operating System :: MacOS :: MacOS X
|
|
19
19
|
Classifier: Operating System :: POSIX :: Linux
|
|
20
20
|
Classifier: Programming Language :: Python
|
|
21
|
-
Classifier: Programming Language :: Python :: 3.10
|
|
22
21
|
Classifier: Programming Language :: Python :: 3.11
|
|
23
22
|
Classifier: Programming Language :: Python :: 3.12
|
|
24
23
|
Classifier: Programming Language :: Python :: 3.13
|
|
25
24
|
Classifier: Programming Language :: Python :: 3.14
|
|
26
25
|
Classifier: Topic :: Software Development :: Libraries
|
|
27
|
-
Requires-Python: >=3.
|
|
28
|
-
Requires-Dist: typing-extensions; python_version < '3.
|
|
26
|
+
Requires-Python: >=3.11
|
|
27
|
+
Requires-Dist: typing-extensions; python_version < '3.12'
|
|
29
28
|
Provides-Extra: pydantic
|
|
30
29
|
Requires-Dist: pydantic>=2.0; extra == 'pydantic'
|
|
31
30
|
Description-Content-Type: text/x-rst
|
|
@@ -22,10 +22,10 @@
|
|
|
22
22
|
<img src="https://img.shields.io/github/actions/workflow/status/mdomke/python-ulid/lint-and-test.yml?style=flat-square&brach=main" />
|
|
23
23
|
</a>
|
|
24
24
|
<a href="https://python-ulid.readthedocs.io">
|
|
25
|
-
<img src="https://readthedocs.org/projects/python-ulid/badge/?version=latest&style=flat-square" />
|
|
25
|
+
<img src="https://app.readthedocs.org/projects/python-ulid/badge/?version=latest&style=flat-square" />
|
|
26
26
|
</a>
|
|
27
|
-
<a href="https://
|
|
28
|
-
<img src="https://img.shields.io/badge/
|
|
27
|
+
<a href="https://github.com/astral-sh/ruff">
|
|
28
|
+
<img src="https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json&style=flat-square" />
|
|
29
29
|
</a>
|
|
30
30
|
</p>
|
|
31
31
|
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import os
|
|
2
2
|
import sys
|
|
3
3
|
from datetime import datetime
|
|
4
|
-
from datetime import
|
|
4
|
+
from datetime import UTC
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
sys.path.insert(0, os.path.abspath("../.."))
|
|
@@ -9,7 +9,7 @@ sys.path.insert(0, os.path.abspath("../.."))
|
|
|
9
9
|
import ulid
|
|
10
10
|
|
|
11
11
|
|
|
12
|
-
copyright = f"{datetime.now(
|
|
12
|
+
copyright = f"{datetime.now(UTC).year}, Martin Domke"
|
|
13
13
|
author = "Martin Domke"
|
|
14
14
|
master_doc = "index"
|
|
15
15
|
source_suffix = {".rst": "restructuredtext", ".md": "restructuredtext"}
|
|
@@ -7,7 +7,7 @@ name = "python-ulid"
|
|
|
7
7
|
description = "Universally unique lexicographically sortable identifier"
|
|
8
8
|
dynamic = ["version", "readme"]
|
|
9
9
|
license = "MIT"
|
|
10
|
-
requires-python = ">=3.
|
|
10
|
+
requires-python = ">=3.11"
|
|
11
11
|
authors = [
|
|
12
12
|
{ name = "Martin Domke", email = "mail@martindomke.net" },
|
|
13
13
|
]
|
|
@@ -19,7 +19,6 @@ classifiers = [
|
|
|
19
19
|
"Operating System :: POSIX :: Linux",
|
|
20
20
|
"Operating System :: MacOS :: MacOS X",
|
|
21
21
|
"Programming Language :: Python",
|
|
22
|
-
"Programming Language :: Python :: 3.10",
|
|
23
22
|
"Programming Language :: Python :: 3.11",
|
|
24
23
|
"Programming Language :: Python :: 3.12",
|
|
25
24
|
"Programming Language :: Python :: 3.13",
|
|
@@ -27,7 +26,7 @@ classifiers = [
|
|
|
27
26
|
"Topic :: Software Development :: Libraries",
|
|
28
27
|
]
|
|
29
28
|
dependencies = [
|
|
30
|
-
"typing-extensions; python_version < '3.
|
|
29
|
+
"typing-extensions; python_version < '3.12'",
|
|
31
30
|
]
|
|
32
31
|
|
|
33
32
|
[project.urls]
|
|
@@ -60,7 +59,7 @@ exclude = [
|
|
|
60
59
|
|
|
61
60
|
[tool.ruff]
|
|
62
61
|
extend = ".ruff_defaults.toml"
|
|
63
|
-
target-version = "
|
|
62
|
+
target-version = "py311"
|
|
64
63
|
line-length = 100
|
|
65
64
|
|
|
66
65
|
[tool.ruff.format]
|
|
@@ -112,12 +111,13 @@ preset = "strict"
|
|
|
112
111
|
project-excludes = ["docs"]
|
|
113
112
|
|
|
114
113
|
[tool.poe.tasks]
|
|
115
|
-
test = "uv run --extra pydantic
|
|
116
|
-
check-code = "ruff check"
|
|
117
|
-
check-fmt = "ruff format --check"
|
|
114
|
+
test = "uv run --extra pydantic pytest --cov --cov-report=term-missing"
|
|
115
|
+
check-code = "uv run ruff check"
|
|
116
|
+
check-fmt = "uv run ruff format --check"
|
|
118
117
|
check-types = "uv run --extra pydantic pyrefly check"
|
|
119
|
-
check-docs = "doc8 docs/source"
|
|
120
|
-
fmt = "ruff format"
|
|
118
|
+
check-docs = "uv run doc8 docs/source"
|
|
119
|
+
fmt = "uv run ruff format"
|
|
120
|
+
fix = "uv run ruff check --fix"
|
|
121
121
|
build = "uv build"
|
|
122
122
|
cov-combine = "coverage combine"
|
|
123
123
|
cov-xml = "coverage xml"
|
|
@@ -146,17 +146,18 @@ max-line-length = 100
|
|
|
146
146
|
|
|
147
147
|
[dependency-groups]
|
|
148
148
|
dev = [
|
|
149
|
-
"coverage==7.
|
|
149
|
+
"coverage==7.17.*",
|
|
150
150
|
"freezegun==1.5.*",
|
|
151
|
-
"poethepoet==0.
|
|
152
|
-
"prek==0.
|
|
153
|
-
"pyrefly==1.
|
|
151
|
+
"poethepoet==0.49.*",
|
|
152
|
+
"prek==0.5.*",
|
|
153
|
+
"pyrefly==1.4.*",
|
|
154
154
|
"pytest==9.1.*",
|
|
155
|
-
"
|
|
155
|
+
"pytest-cov==7.1.*",
|
|
156
|
+
"ruff==0.17.*",
|
|
156
157
|
]
|
|
157
158
|
doc = [
|
|
158
159
|
"doc8>=2.0.0",
|
|
159
|
-
"sphinx
|
|
160
|
+
"sphinx<9.0",
|
|
160
161
|
]
|
|
161
162
|
|
|
162
163
|
[tool.hatch.metadata.hooks.fancy-pypi-readme]
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import subprocess
|
|
2
|
+
import sys
|
|
3
|
+
|
|
1
4
|
import pytest
|
|
2
5
|
|
|
3
6
|
import ulid.__main__ as cli
|
|
@@ -30,7 +33,7 @@ def test_parse_show(option: str):
|
|
|
30
33
|
assert ulid.hex in output
|
|
31
34
|
assert str(int(ulid)) in output
|
|
32
35
|
assert str(ulid.timestamp) in output
|
|
33
|
-
assert
|
|
36
|
+
assert ulid.datetime.isoformat() in output
|
|
34
37
|
|
|
35
38
|
|
|
36
39
|
@pytest.mark.parametrize(
|
|
@@ -79,3 +82,49 @@ def test_build(option: str):
|
|
|
79
82
|
ulid_out = ULID.from_str(output)
|
|
80
83
|
if includes_timestamp:
|
|
81
84
|
assert ulid_out.datetime == ulid.datetime
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
@pytest.mark.parametrize(
|
|
88
|
+
("args", "stdin", "expected"),
|
|
89
|
+
[
|
|
90
|
+
(["build", "--from-int", "0"], None, "00000000000000000000000000\n"),
|
|
91
|
+
(["build", "--from-int", "-"], "0\n", "00000000000000000000000000\n"),
|
|
92
|
+
(["show", "--int", "00000000000000000000000000"], None, "0\n"),
|
|
93
|
+
(["show", "--int", "-"], "00000000000000000000000000\n", "0\n"),
|
|
94
|
+
],
|
|
95
|
+
)
|
|
96
|
+
def test_module_outputs_result(args: list[str], stdin: str | None, expected: str) -> None:
|
|
97
|
+
result = subprocess.run(
|
|
98
|
+
[sys.executable, "-m", "ulid", *args],
|
|
99
|
+
input=stdin,
|
|
100
|
+
capture_output=True,
|
|
101
|
+
text=True,
|
|
102
|
+
check=True,
|
|
103
|
+
timeout=10,
|
|
104
|
+
)
|
|
105
|
+
assert result.stdout == expected
|
|
106
|
+
assert result.stderr == ""
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
@pytest.mark.parametrize("args", [[], ["--help"]])
|
|
110
|
+
def test_module_help(args: list[str]) -> None:
|
|
111
|
+
result = subprocess.run(
|
|
112
|
+
[sys.executable, "-m", "ulid", *args],
|
|
113
|
+
capture_output=True,
|
|
114
|
+
text=True,
|
|
115
|
+
check=True,
|
|
116
|
+
timeout=10,
|
|
117
|
+
)
|
|
118
|
+
assert result.stdout.startswith("usage: python -m ulid")
|
|
119
|
+
assert not result.stdout.endswith("None\n")
|
|
120
|
+
assert result.stderr == ""
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
def test_console_entrypoint(
|
|
124
|
+
monkeypatch: pytest.MonkeyPatch, capsys: pytest.CaptureFixture[str]
|
|
125
|
+
) -> None:
|
|
126
|
+
monkeypatch.setattr(sys, "argv", ["ulid", "build", "--from-int", "0"])
|
|
127
|
+
cli.entrypoint()
|
|
128
|
+
captured = capsys.readouterr()
|
|
129
|
+
assert captured.out == "00000000000000000000000000\n"
|
|
130
|
+
assert captured.err == ""
|
|
@@ -4,7 +4,7 @@ import uuid
|
|
|
4
4
|
from collections.abc import Callable
|
|
5
5
|
from datetime import datetime
|
|
6
6
|
from datetime import timedelta
|
|
7
|
-
from datetime import
|
|
7
|
+
from datetime import UTC
|
|
8
8
|
from typing import Any
|
|
9
9
|
|
|
10
10
|
import pytest
|
|
@@ -23,7 +23,7 @@ from ulid import ULIDGenerator
|
|
|
23
23
|
|
|
24
24
|
|
|
25
25
|
def utcnow() -> datetime:
|
|
26
|
-
return datetime.now(
|
|
26
|
+
return datetime.now(UTC)
|
|
27
27
|
|
|
28
28
|
|
|
29
29
|
def datetimes_almost_equal(a: datetime, b: datetime) -> None:
|
|
@@ -161,7 +161,7 @@ def test_idempotency() -> None:
|
|
|
161
161
|
assert ULID.from_bytes(ulid.bytes) == ulid
|
|
162
162
|
assert ULID.from_str(str(ulid)) == ulid
|
|
163
163
|
assert ULID.from_uuid(ulid.to_uuid()) == ulid
|
|
164
|
-
assert ULID.
|
|
164
|
+
assert ULID.from_uuid7(ulid.to_uuid7(compliant=False)) == ulid
|
|
165
165
|
assert ULID.from_int(int(ulid)) == ulid
|
|
166
166
|
assert ULID.from_hex(ulid.hex) == ulid
|
|
167
167
|
assert ULID.parse(ulid) == ulid
|
|
@@ -177,6 +177,12 @@ def test_idempotency() -> None:
|
|
|
177
177
|
assert ULID.parse(ulid.bytes) == ulid
|
|
178
178
|
|
|
179
179
|
|
|
180
|
+
def test_from_str_is_case_insensitive() -> None:
|
|
181
|
+
ulid = ULID()
|
|
182
|
+
assert ULID.from_str(str(ulid).lower()) == ulid
|
|
183
|
+
assert ULID.parse(str(ulid).lower()) == ulid
|
|
184
|
+
|
|
185
|
+
|
|
180
186
|
def test_to_uuid4() -> None:
|
|
181
187
|
ulid = ULID()
|
|
182
188
|
uuid = ulid.to_uuid4()
|
|
@@ -209,7 +215,7 @@ def test_uuid7_perfect_roundtrip() -> None:
|
|
|
209
215
|
"""Test perfect round-trip conversion with compliant=False."""
|
|
210
216
|
ulid = ULID()
|
|
211
217
|
uuid7 = ulid.to_uuid7(compliant=False)
|
|
212
|
-
ulid_restored = ULID.
|
|
218
|
+
ulid_restored = ULID.from_uuid7(uuid7)
|
|
213
219
|
# Perfect round-trip: all 128 bits should be identical
|
|
214
220
|
assert ulid_restored == ulid
|
|
215
221
|
assert ulid_restored.bytes == ulid.bytes
|
|
@@ -220,7 +226,7 @@ def test_uuid7_compliant_roundtrip() -> None:
|
|
|
220
226
|
"""Test round-trip with compliant=True preserves timestamp but loses some randomness."""
|
|
221
227
|
ulid = ULID()
|
|
222
228
|
uuid7 = ulid.to_uuid7(compliant=True)
|
|
223
|
-
ulid_restored = ULID.
|
|
229
|
+
ulid_restored = ULID.from_uuid7(uuid7)
|
|
224
230
|
# Timestamp should be perfectly preserved
|
|
225
231
|
assert ulid_restored.milliseconds == ulid.milliseconds
|
|
226
232
|
# Full ULID won't match due to lost randomness in version/variant bits (6 bits lost)
|
|
@@ -236,7 +242,7 @@ def test_uuid7_timestamp_preservation() -> None:
|
|
|
236
242
|
# Test both compliant and non-compliant modes
|
|
237
243
|
for compliant in [False, True]:
|
|
238
244
|
uuid7 = ulid.to_uuid7(compliant=compliant)
|
|
239
|
-
ulid_from_uuid7 = ULID.
|
|
245
|
+
ulid_from_uuid7 = ULID.from_uuid7(uuid7)
|
|
240
246
|
|
|
241
247
|
# Check timestamp is perfectly preserved (exact millisecond match)
|
|
242
248
|
assert ulid_from_uuid7.milliseconds == ulid.milliseconds
|
|
@@ -271,7 +277,7 @@ def test_uuid7_same_millisecond() -> None:
|
|
|
271
277
|
|
|
272
278
|
# Perfect round-trip for all
|
|
273
279
|
for ulid, uuid7 in zip(ulids, uuids, strict=True):
|
|
274
|
-
assert ULID.
|
|
280
|
+
assert ULID.from_uuid7(uuid7) == ulid
|
|
275
281
|
|
|
276
282
|
|
|
277
283
|
def test_from_uuid7_with_external_uuid() -> None:
|
|
@@ -289,7 +295,7 @@ def test_from_uuid7_with_external_uuid() -> None:
|
|
|
289
295
|
uuid_int = (timestamp_ms << 80) | (0x7 << 76) | (rand_a << 64) | (0x2 << 62) | rand_b
|
|
290
296
|
uuid7 = uuid.UUID(bytes=uuid_int.to_bytes(16, byteorder="big"))
|
|
291
297
|
|
|
292
|
-
ulid = ULID.
|
|
298
|
+
ulid = ULID.from_uuid7(uuid7)
|
|
293
299
|
|
|
294
300
|
# Check timestamp is correctly extracted (should be exact since it's in milliseconds)
|
|
295
301
|
assert ulid.milliseconds == timestamp_ms
|
|
@@ -350,6 +356,7 @@ Params = bytes | str | int | float
|
|
|
350
356
|
(ULID.from_str, "not-enough"), # invalid length
|
|
351
357
|
(ULID.from_str, 123), # inavlid type
|
|
352
358
|
(ULID.from_str, "notavalidulidnotavalidulid"), # invalid alphabet
|
|
359
|
+
(ULID.from_str, "0" * 25 + "u"), # excluded letter in lower case
|
|
353
360
|
(ULID.from_str, "Z" * 26), # invalid timestamp
|
|
354
361
|
(ULID.from_int, "not-an-int"), # invalid type
|
|
355
362
|
(ULID.from_uuid, "not-a-uuid"), # invalid type
|
|
@@ -382,7 +389,7 @@ def test_ulid_min_input(constructor: Callable[[Params], ULID], value: Params) ->
|
|
|
382
389
|
[
|
|
383
390
|
(ULID, b"\xff" * 16),
|
|
384
391
|
(ULID.from_timestamp, 281474976710655),
|
|
385
|
-
(ULID.from_datetime, datetime.max.replace(tzinfo=
|
|
392
|
+
(ULID.from_datetime, datetime.max.replace(tzinfo=UTC)),
|
|
386
393
|
(ULID.from_bytes, b"\xff" * 16),
|
|
387
394
|
(ULID.from_str, "7" + "Z" * 25),
|
|
388
395
|
(ULID.from_hex, "f" * 32),
|
|
@@ -7,7 +7,8 @@ import time
|
|
|
7
7
|
import uuid
|
|
8
8
|
from collections.abc import Callable
|
|
9
9
|
from datetime import datetime
|
|
10
|
-
from datetime import
|
|
10
|
+
from datetime import UTC
|
|
11
|
+
from importlib.metadata import version
|
|
11
12
|
from threading import Lock
|
|
12
13
|
from typing import Any
|
|
13
14
|
from typing import cast
|
|
@@ -25,22 +26,12 @@ from ulid import constants
|
|
|
25
26
|
|
|
26
27
|
|
|
27
28
|
if TYPE_CHECKING: # pragma: no cover
|
|
28
|
-
import
|
|
29
|
+
from typing import Self
|
|
29
30
|
|
|
30
31
|
from pydantic import GetCoreSchemaHandler
|
|
31
32
|
from pydantic import ValidatorFunctionWrapHandler
|
|
32
33
|
from pydantic_core import CoreSchema
|
|
33
34
|
|
|
34
|
-
if sys.version_info >= (3, 11):
|
|
35
|
-
from typing import Self
|
|
36
|
-
else:
|
|
37
|
-
from typing_extensions import Self
|
|
38
|
-
|
|
39
|
-
try:
|
|
40
|
-
from importlib.metadata import version
|
|
41
|
-
except ImportError: # pragma: no cover
|
|
42
|
-
from importlib_metadata import version # type: ignore
|
|
43
|
-
|
|
44
35
|
|
|
45
36
|
__version__ = version("python-ulid")
|
|
46
37
|
|
|
@@ -413,7 +404,7 @@ class ULID:
|
|
|
413
404
|
>>> ulid.datetime
|
|
414
405
|
datetime.datetime(2020, 4, 30, 14, 33, 27, 560000, tzinfo=datetime.timezone.utc)
|
|
415
406
|
"""
|
|
416
|
-
return datetime.fromtimestamp(self.timestamp,
|
|
407
|
+
return datetime.fromtimestamp(self.timestamp, UTC)
|
|
417
408
|
|
|
418
409
|
@functools.cached_property
|
|
419
410
|
def hex(self) -> str:
|
|
@@ -456,9 +447,9 @@ class ULID:
|
|
|
456
447
|
Examples:
|
|
457
448
|
|
|
458
449
|
>>> ulid = ULID()
|
|
459
|
-
>>> uuid7 = ulid.
|
|
460
|
-
>>> assert ULID.
|
|
461
|
-
>>> uuid7_compliant = ulid.
|
|
450
|
+
>>> uuid7 = ulid.to_uuid7() # Perfect round-trip
|
|
451
|
+
>>> assert ULID.from_uuid7(uuid7) == ulid
|
|
452
|
+
>>> uuid7_compliant = ulid.to_uuid7(compliant=True) # RFC 4122 compliant
|
|
462
453
|
>>> uuid7_compliant.version
|
|
463
454
|
7
|
|
464
455
|
"""
|
|
@@ -487,17 +478,17 @@ class ULID:
|
|
|
487
478
|
return uuid.UUID(bytes=uuid_bytes)
|
|
488
479
|
|
|
489
480
|
@classmethod
|
|
490
|
-
def
|
|
481
|
+
def from_uuid7(cls, value: uuid.UUID) -> Self:
|
|
491
482
|
"""Create a new :class:`ULID` from a UUIDv7 (:class:`uuid.UUID` version 7).
|
|
492
483
|
|
|
493
484
|
Extracts the timestamp from the UUIDv7's first 48 bits (milliseconds since epoch)
|
|
494
485
|
and the remaining 80 bits as randomness. The timestamp is always transparently
|
|
495
|
-
preserved, providing perfect round-trip conversion with :meth:`
|
|
486
|
+
preserved, providing perfect round-trip conversion with :meth:`to_uuid7`.
|
|
496
487
|
|
|
497
488
|
Examples:
|
|
498
489
|
|
|
499
490
|
>>> uuid7 = uuid.UUID("01936c5e-f4c0-7000-8000-000000000000")
|
|
500
|
-
>>> ulid = ULID.
|
|
491
|
+
>>> ulid = ULID.from_uuid7(uuid7)
|
|
501
492
|
>>> ulid.datetime
|
|
502
493
|
datetime.datetime(2025, 11, 10, ...)
|
|
503
494
|
"""
|
|
@@ -45,7 +45,11 @@ def make_parser(prog: str | None = None) -> argparse.ArgumentParser:
|
|
|
45
45
|
width=min(shutil.get_terminal_size().columns - 2, 127),
|
|
46
46
|
),
|
|
47
47
|
)
|
|
48
|
-
|
|
48
|
+
|
|
49
|
+
def help(*_args: Any) -> None:
|
|
50
|
+
parser.print_help()
|
|
51
|
+
|
|
52
|
+
parser.set_defaults(func=help)
|
|
49
53
|
parser.add_argument("--version", "-V", action="version", version=ulid.__version__)
|
|
50
54
|
|
|
51
55
|
subparsers = parser.add_subparsers(title="subcommands")
|
|
@@ -163,10 +167,10 @@ def show(args: argparse.Namespace) -> str:
|
|
|
163
167
|
).strip()
|
|
164
168
|
|
|
165
169
|
|
|
166
|
-
def entrypoint() -> None: # pragma: no cover
|
|
167
|
-
if (value := main(sys.argv[1:])) is not None:
|
|
170
|
+
def entrypoint(prog: str | None = None) -> None: # pragma: no cover
|
|
171
|
+
if (value := main(sys.argv[1:], prog)) is not None:
|
|
168
172
|
print(value) # noqa: T201
|
|
169
173
|
|
|
170
174
|
|
|
171
175
|
if __name__ == "__main__": # pragma: no cover
|
|
172
|
-
|
|
176
|
+
entrypoint("python -m ulid")
|
|
@@ -7,6 +7,8 @@ from ulid import constants
|
|
|
7
7
|
# https://github.com/RobThree/NUlid/blob/89f5a9fc827d191ae5adafe42547575ed3a47723/NUlid/Ulid.cs#L168
|
|
8
8
|
|
|
9
9
|
ENCODE: str = "0123456789ABCDEFGHJKMNPQRSTVWXYZ"
|
|
10
|
+
# The ULID spec defines the string representation as case insensitive.
|
|
11
|
+
DECODE_ALPHABET: str = ENCODE + ENCODE.lower()
|
|
10
12
|
DECODE: Sequence[int] = [
|
|
11
13
|
0xFF,
|
|
12
14
|
0xFF,
|
|
@@ -194,7 +196,7 @@ def encode_randomness(binary: bytes) -> str:
|
|
|
194
196
|
def decode(encoded: str) -> bytes:
|
|
195
197
|
if len(encoded) != constants.REPR_LEN:
|
|
196
198
|
raise ValueError("Encoded ULID has to be exactly 26 characters long.")
|
|
197
|
-
if any((c not in
|
|
199
|
+
if any((c not in DECODE_ALPHABET) for c in encoded):
|
|
198
200
|
raise ValueError(f"Encoded ULID can only consist of letters in {ENCODE}.")
|
|
199
201
|
return decode_timestamp(encoded[: constants.TIMESTAMP_REPR_LEN]) + decode_randomness(
|
|
200
202
|
encoded[constants.TIMESTAMP_REPR_LEN :]
|