Skip to content

Bulk asynchronous downloader for multiple file hosts

refactor: move type adapter to models

NTFSvolume9b1e3249651c
diff --git a/cyberdrop_dl/crawlers/pixeldrain.py b/cyberdrop_dl/crawlers/pixeldrain.py
index b651770d5..31d0497f2 100644
--- a/cyberdrop_dl/crawlers/pixeldrain.py
+++ b/cyberdrop_dl/crawlers/pixeldrain.py
@@ -8,8 +8,9 @@ from typing import TYPE_CHECKING, Any, ClassVar, Literal, final, override
from cyberdrop_dl.crawlers.crawler import API, Crawler, RateLimit, SupportedDomains, SupportedPaths, auto_task_id
from cyberdrop_dl.exceptions import ScrapeError
+from cyberdrop_dl.models import type_adapter
from cyberdrop_dl.url_objects import AbsoluteHttpURL
-from cyberdrop_dl.utils import basic_auth, error_handling_wrapper, type_adapter
+from cyberdrop_dl.utils import basic_auth, error_handling_wrapper
if TYPE_CHECKING:
from cyberdrop_dl.clients.response import AbstractResponse
diff --git a/cyberdrop_dl/models/__init__.py b/cyberdrop_dl/models/__init__.py
index f23e54a6c..e144e75eb 100644
--- a/cyberdrop_dl/models/__init__.py
+++ b/cyberdrop_dl/models/__init__.py
@@ -1,9 +1,10 @@
"""Pydantic models"""
+import functools
from typing import Any, ClassVar, TypedDict
from cyclopts import Parameter
-from pydantic import AnyUrl, BaseModel, Secret, SerializationInfo, model_serializer, model_validator
+from pydantic import AnyUrl, BaseModel, Secret, SerializationInfo, TypeAdapter, model_serializer, model_validator
from cyberdrop_dl import env
@@ -125,3 +126,11 @@ def merge_models[M: BaseModel](default: M, new: M) -> M:
new_dict = new.model_dump(exclude_unset=True)
updated_dict = merge_dicts(default_dict, new_dict)
return default.model_validate(updated_dict)
+
+
+def type_adapter[T](cls: type[T]) -> TypeAdapter[T]:
+ """Get a type adapter for this class.
+
+ Type adapters are cached. Multiple calls return the same adapter"""
+ return TypeAdapter(cls)
diff --git a/cyberdrop_dl/program_ui.py b/cyberdrop_dl/program_ui.py
index dd10f566e..2a39d927c 100644
--- a/cyberdrop_dl/program_ui.py
+++ b/cyberdrop_dl/program_ui.py
@@ -88,8 +88,8 @@ def _edit_config(manager: Manager) -> None:
console.error(str(e))
else:
console.warning("You must restart cyberdrop-dl for config changes to take effect")
- finally:
- enter_to_continue()
+
+ enter_to_continue()
def _edit_urls(manager: Manager) -> None:
diff --git a/cyberdrop_dl/utils/__init__.py b/cyberdrop_dl/utils/__init__.py
index b389c4d9f..88a292ef6 100644
--- a/cyberdrop_dl/utils/__init__.py
+++ b/cyberdrop_dl/utils/__init__.py
@@ -8,7 +8,7 @@ import platform
import sys
from typing import TYPE_CHECKING, Any
-from cyberdrop_dl.utils._dataclasses import DictDataclass, deserialize, filter_data, type_adapter # noqa: F401
+from cyberdrop_dl.utils._dataclasses import DictDataclass, deserialize, filter_data # noqa: F401
from cyberdrop_dl.utils._errors import error_handling_context, error_handling_wrapper # noqa: F401
from cyberdrop_dl.utils._url import parse_http_url as parse_url # noqa: F401
from cyberdrop_dl.utils._url import remove_trailing_slash # noqa: F401
diff --git a/cyberdrop_dl/utils/_dataclasses.py b/cyberdrop_dl/utils/_dataclasses.py
index 59db435a7..eb70cc799 100644
--- a/cyberdrop_dl/utils/_dataclasses.py
+++ b/cyberdrop_dl/utils/_dataclasses.py
@@ -3,7 +3,6 @@ from __future__ import annotations
import dataclasses
from typing import TYPE_CHECKING, Any, ClassVar, Protocol, Self
-from pydantic import TypeAdapter
from typing_extensions import Sentinel
if TYPE_CHECKING:
@@ -15,7 +14,6 @@ class _DataClass(Protocol):
_FIELDS_CACHE: dict[type, tuple[str, ...]] = {}
-_TYPE_ADAPTER_CACHE: dict[type[_DataClass], TypeAdapter[_DataClass]] = {}
_MISSING = Sentinel("_MISSING")
@@ -40,16 +38,6 @@ def deserialize[DataClassT: _DataClass](
return cls(**data)
-def type_adapter[DataClassT: _DataClass](cls: type[DataClassT]) -> TypeAdapter[DataClassT]:
- """Get a type adapter for this class.
-
- Type adapters are cached. Multiple calls return the same adapter"""
- if adapter := _TYPE_ADAPTER_CACHE.get(cls):
- return adapter # pyright: ignore[reportReturnType]
- adapter = _TYPE_ADAPTER_CACHE[cls] = TypeAdapter(cls)
- return adapter # pyright: ignore[reportReturnType]
-
-
class DictDataclass(_DataClass, Protocol):
filter_dict = classmethod(filter_data) # pyright: ignore[reportUnannotatedClassAttribute]
diff --git a/pyproject.toml b/pyproject.toml
index f29ed38cf..7c690bda8 100644
--- a/pyproject.toml
+++ b/pyproject.toml
@@ -53,7 +53,7 @@ license-files = ["LICENSE"]
maintainers = [{name = "NTFSvolume", email = "[email protected]"}]
readme = "README.md"
requires-python = ">=3.12"
-version = "10.0.0rc2"
+version = "10.0.0rc3"
[project.optional-dependencies]
apprise = [