Skip to content

Commit

Permalink
Rename AnnotatedResult to AnnotatedInstance.
Browse files Browse the repository at this point in the history
  • Loading branch information
wRAR committed Feb 29, 2024
1 parent 77f9254 commit fed7c86
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 24 deletions.
8 changes: 4 additions & 4 deletions tests/test_serialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
]
)
4 changes: 2 additions & 2 deletions tests/test_testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion web_poet/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .fields import field, item_from_fields, item_from_fields_sync
from .page_inputs import (
AnnotatedResult,
AnnotatedInstance,
AnyResponse,
BrowserHtml,
BrowserResponse,
Expand Down
2 changes: 1 addition & 1 deletion web_poet/page_inputs/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from .annotated import AnnotatedResult
from .annotated import AnnotatedInstance
from .browser import BrowserHtml, BrowserResponse
from .client import HttpClient
from .http import (
Expand Down
4 changes: 2 additions & 2 deletions web_poet/page_inputs/annotated.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
14 changes: 7 additions & 7 deletions web_poet/serialization/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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()."
)
Expand Down Expand Up @@ -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:
Expand Down
14 changes: 7 additions & 7 deletions web_poet/serialization/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Any, Dict, List, Optional, Type, cast

from .. import (
AnnotatedResult,
AnnotatedInstance,
HttpClient,
HttpRequest,
HttpRequestBody,
Expand Down Expand Up @@ -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)
Expand All @@ -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 = {}
Expand All @@ -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)

0 comments on commit fed7c86

Please sign in to comment.