From fed7c86aaadf74c998cb2c402e52adcc8855a17c Mon Sep 17 00:00:00 2001 From: Andrey Rakhmatullin Date: Thu, 29 Feb 2024 17:15:24 +0500 Subject: [PATCH] Rename AnnotatedResult to AnnotatedInstance. --- tests/test_serialization.py | 8 ++++---- tests/test_testing.py | 4 ++-- web_poet/__init__.py | 2 +- web_poet/page_inputs/__init__.py | 2 +- web_poet/page_inputs/annotated.py | 4 ++-- web_poet/serialization/api.py | 14 +++++++------- web_poet/serialization/functions.py | 14 +++++++------- 7 files changed, 24 insertions(+), 24 deletions(-) diff --git a/tests/test_serialization.py b/tests/test_serialization.py index 97a743b8..1911defb 100644 --- a/tests/test_serialization.py +++ b/tests/test_serialization.py @@ -15,7 +15,7 @@ Stats, WebPage, ) -from web_poet.page_inputs.annotated import AnnotatedResult +from web_poet.page_inputs.annotated import AnnotatedInstance from web_poet.page_inputs.url import _Url from web_poet.serialization import ( SerializedDataFileStorage, @@ -233,7 +233,7 @@ class MyWebPage(ItemPage): url = ResponseUrl(url_str) serialized_deps = serialize( - [AnnotatedResult(book_list_html_response, ("foo", 42)), url] + [AnnotatedInstance(book_list_html_response, ("foo", 42)), url] ) po = MyWebPage( book_list_html_response, @@ -255,8 +255,8 @@ def test_annotated_duplicate(book_list_html_response) -> None: ): serialize( [ - AnnotatedResult(book_list_html_response, ("foo", 42)), - AnnotatedResult(book_list_html_response, ("bar",)), + AnnotatedInstance(book_list_html_response, ("foo", 42)), + AnnotatedInstance(book_list_html_response, ("bar",)), url, ] ) diff --git a/tests/test_testing.py b/tests/test_testing.py index 757a166d..f7289a6a 100644 --- a/tests/test_testing.py +++ b/tests/test_testing.py @@ -15,7 +15,7 @@ from web_poet import HttpClient, HttpRequest, HttpResponse, WebPage, field from web_poet.exceptions import HttpRequestError, HttpResponseError, Retry, UseFallback -from web_poet.page_inputs import AnnotatedResult +from web_poet.page_inputs import AnnotatedInstance from web_poet.page_inputs.client import _SavedResponseData from web_poet.testing import Fixture from web_poet.testing.__main__ import main as cli_main @@ -558,7 +558,7 @@ def test_annotated(pytester, book_list_html_response) -> None: _save_fixture( pytester, page_cls=MyAnnotatedItemPage, - page_inputs=[AnnotatedResult(book_list_html_response, ("foo", 42))], + page_inputs=[AnnotatedInstance(book_list_html_response, ("foo", 42))], expected_output={"foo": "bar"}, ) result = pytester.runpytest() diff --git a/web_poet/__init__.py b/web_poet/__init__.py index bfdde255..76933fab 100644 --- a/web_poet/__init__.py +++ b/web_poet/__init__.py @@ -1,6 +1,6 @@ from .fields import field, item_from_fields, item_from_fields_sync from .page_inputs import ( - AnnotatedResult, + AnnotatedInstance, AnyResponse, BrowserHtml, BrowserResponse, diff --git a/web_poet/page_inputs/__init__.py b/web_poet/page_inputs/__init__.py index a4bcdeb4..270d2836 100644 --- a/web_poet/page_inputs/__init__.py +++ b/web_poet/page_inputs/__init__.py @@ -1,4 +1,4 @@ -from .annotated import AnnotatedResult +from .annotated import AnnotatedInstance from .browser import BrowserHtml, BrowserResponse from .client import HttpClient from .http import ( diff --git a/web_poet/page_inputs/annotated.py b/web_poet/page_inputs/annotated.py index d4554c83..25d257ed 100644 --- a/web_poet/page_inputs/annotated.py +++ b/web_poet/page_inputs/annotated.py @@ -3,8 +3,8 @@ @dataclass -class AnnotatedResult: - """Wrapper for annotated dependencies. +class AnnotatedInstance: + """Wrapper for instances of annotated dependencies. It is used when both the dependency value and the dependency annotation are needed. diff --git a/web_poet/serialization/api.py b/web_poet/serialization/api.py index e9ede36c..afdc6c42 100644 --- a/web_poet/serialization/api.py +++ b/web_poet/serialization/api.py @@ -8,7 +8,7 @@ from andi.typeutils import strip_annotated import web_poet -from web_poet import AnnotatedResult, Injectable +from web_poet import AnnotatedInstance, Injectable from web_poet.pages import is_injectable from web_poet.utils import get_fq_class_name @@ -136,15 +136,15 @@ def serialize(deps: Iterable[Any]) -> SerializedData: cls = dep.__class__ if is_injectable(cls): raise ValueError(f"Injectable type {cls} passed to serialize()") - if cls is AnnotatedResult: - key = f"AnnotatedResult {_get_name_for_class(dep.result.__class__)}" + if cls is AnnotatedInstance: + key = f"AnnotatedInstance {_get_name_for_class(dep.result.__class__)}" else: key = _get_name_for_class(cls) if key in result: cls_name = cls.__name__ - if cls is AnnotatedResult: - cls_name = f"AnnotatedResult for {dep.result.__class__.__name__}" + if cls is AnnotatedInstance: + cls_name = f"AnnotatedInstance for {dep.result.__class__.__name__}" raise ValueError( f"Several instances of {cls_name} were passed to serialize()." ) @@ -192,8 +192,8 @@ def deserialize(cls: Type[InjectableT], data: SerializedData) -> InjectableT: deps: Dict[Callable, Any] = {} for dep_type_name, dep_data in data.items(): - if dep_type_name.startswith("AnnotatedResult "): - annotated_result = deserialize_leaf(AnnotatedResult, dep_data) + if dep_type_name.startswith("AnnotatedInstance "): + annotated_result = deserialize_leaf(AnnotatedInstance, dep_data) dep_type = annotated_result.get_annotated_cls() deserialized_dep = annotated_result.result else: diff --git a/web_poet/serialization/functions.py b/web_poet/serialization/functions.py index 89b088fd..69a54400 100644 --- a/web_poet/serialization/functions.py +++ b/web_poet/serialization/functions.py @@ -2,7 +2,7 @@ from typing import Any, Dict, List, Optional, Type, cast from .. import ( - AnnotatedResult, + AnnotatedInstance, HttpClient, HttpRequest, HttpRequestBody, @@ -198,9 +198,9 @@ def _deserialize_Stats(cls: Type[Stats], data: SerializedLeafData) -> Stats: register_serialization(_serialize_Stats, _deserialize_Stats) -def _serialize_AnnotatedResult(o: AnnotatedResult) -> SerializedLeafData: +def _serialize_AnnotatedInstance(o: AnnotatedInstance) -> SerializedLeafData: serialized_data: SerializedLeafData = { - "metadata.json": json.dumps(o.metadata).encode(), + "metadata.json": _format_json(o.metadata).encode(), "result_type.txt": _get_name_for_class(type(o.result)).encode(), } serialized_result = serialize_leaf(o.result) @@ -209,9 +209,9 @@ def _serialize_AnnotatedResult(o: AnnotatedResult) -> SerializedLeafData: return serialized_data -def _deserialize_AnnotatedResult( - cls: Type[AnnotatedResult], data: SerializedLeafData -) -> AnnotatedResult: +def _deserialize_AnnotatedInstance( + cls: Type[AnnotatedInstance], data: SerializedLeafData +) -> AnnotatedInstance: metadata = json.loads(data["metadata.json"]) result_type = load_class(data["result_type.txt"].decode()) serialized_result = {} @@ -224,4 +224,4 @@ def _deserialize_AnnotatedResult( return cls(result=result, metadata=metadata) -register_serialization(_serialize_AnnotatedResult, _deserialize_AnnotatedResult) +register_serialization(_serialize_AnnotatedInstance, _deserialize_AnnotatedInstance)