From 05b54ba4285232076667ae7a528550f4c6630da4 Mon Sep 17 00:00:00 2001 From: "Benjamin A. Beasley" Date: Tue, 3 Feb 2026 11:04:29 +0000 Subject: [PATCH 1/2] Depend on typing-extensions for Python<3.11; avoid it otherwise Conditionalize the import of `typing_extensions`, needed only in Python 3.10 and older; use `typing` instead for Python 3.11 and later. Add a dependency on `typing-extensions`, appropriately conditioned on the Python interpreter version. Based on https://github.com/mdomke/python-ulid/pull/47 and https://github.com/mdomke/python-ulid/pull/47#issuecomment-3431221960. Fixes https://github.com/mdomke/python-ulid/issues/44. --- pyproject.toml | 3 +++ ulid/__init__.py | 7 ++++++- uv.lock | 8 +++++++- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 5c3b252..c28114c 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -26,6 +26,9 @@ classifiers = [ "Programming Language :: Python :: 3.13", "Topic :: Software Development :: Libraries", ] +dependencies = [ + "typing-extensions; python_version < '3.11'", +] [project.urls] Homepage = "https://github.com/mdomke/python-ulid" diff --git a/ulid/__init__.py b/ulid/__init__.py index d327385..b2e0863 100644 --- a/ulid/__init__.py +++ b/ulid/__init__.py @@ -2,6 +2,7 @@ import functools import os +import sys import time import uuid from datetime import datetime @@ -13,7 +14,11 @@ from typing import TYPE_CHECKING from typing import TypeVar -from typing_extensions import Self + +if sys.version_info >= (3, 11): + from typing import Self +else: + from typing_extensions import Self from ulid import base32 from ulid import constants diff --git a/uv.lock b/uv.lock index 9818362..1371271 100644 --- a/uv.lock +++ b/uv.lock @@ -825,6 +825,9 @@ wheels = [ [[package]] name = "python-ulid" source = { editable = "." } +dependencies = [ + { name = "typing-extensions", marker = "python_full_version < '3.11'" }, +] [package.optional-dependencies] pydantic = [ @@ -841,7 +844,10 @@ dev = [ ] [package.metadata] -requires-dist = [{ name = "pydantic", marker = "extra == 'pydantic'", specifier = ">=2.0" }] +requires-dist = [ + { name = "pydantic", marker = "extra == 'pydantic'", specifier = ">=2.0" }, + { name = "typing-extensions", marker = "python_full_version < '3.11'" }, +] provides-extras = ["pydantic"] [package.metadata.requires-dev] From 1f474917361df13209ece3a9f847bf826e50a878 Mon Sep 17 00:00:00 2001 From: "Benjamin A. Beasley" Date: Tue, 3 Feb 2026 11:43:03 +0000 Subject: [PATCH 2/2] Even on Python<3.11, avoid typing-extensions import unless type-checking Suggested by @QuLogic in https://github.com/mdomke/python-ulid/pull/50#issuecomment-3840795227. --- ulid/__init__.py | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/ulid/__init__.py b/ulid/__init__.py index b2e0863..bba8873 100644 --- a/ulid/__init__.py +++ b/ulid/__init__.py @@ -2,7 +2,6 @@ import functools import os -import sys import time import uuid from datetime import datetime @@ -14,23 +13,23 @@ from typing import TYPE_CHECKING from typing import TypeVar - -if sys.version_info >= (3, 11): - from typing import Self -else: - from typing_extensions import Self - from ulid import base32 from ulid import constants if TYPE_CHECKING: # pragma: no cover + import sys from collections.abc import Callable from pydantic import GetCoreSchemaHandler from pydantic import ValidatorFunctionWrapHandler from pydantic_core import CoreSchema + if sys.version_info >= (3, 11): + from typing import Self + else: + from typing_extensions import Self + try: from importlib.metadata import version except ImportError: # pragma: no cover @@ -204,7 +203,7 @@ def parse(cls, value: Any) -> Self: a value when they're unsure what format/primitive type it will be given in. """ if isinstance(value, ULID): - return cast(Self, value) + return cast("Self", value) if isinstance(value, uuid.UUID): return cls.from_uuid(value) if isinstance(value, str):