Skip to content

Commit 8e78430

Browse files
committed
remove swap_item_cls for now since it could be more complex
1 parent 9601862 commit 8e78430

File tree

3 files changed

+1
-38
lines changed

3 files changed

+1
-38
lines changed

tests/test_fields.py

Lines changed: 0 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -642,14 +642,6 @@ class BigItem:
642642
z: Optional[int] = None
643643

644644

645-
@attrs.define
646-
class SmallItem:
647-
"""Same with ``BigItem`` but removes the required ``x`` field."""
648-
649-
y: Optional[int] = None
650-
z: Optional[int] = None
651-
652-
653645
@attrs.define
654646
class BigPage(ItemPage[BigItem]):
655647
select_fields: Optional[SelectFields] = None
@@ -911,27 +903,3 @@ async def test_select_fields_include_exclude() -> None:
911903
assert item == BigItem(x=1, y=None, z=3)
912904
assert page.fields_to_extract == {"x", "z"}
913905
assert page.call_counter == {"x": 1, "z": 1}
914-
915-
916-
@pytest.mark.asyncio
917-
async def test_select_fields_swap_item_cls() -> None:
918-
# Basic case
919-
page = BigPage(SelectFields(exclude=["x"], swap_item_cls=SmallItem))
920-
item = await page.to_item()
921-
assert item == SmallItem(y=2, z=3)
922-
assert page.fields_to_extract == {"y", "z"}
923-
assert page.call_counter == {"y": 1, "z": 1}
924-
925-
page = BigPage(SelectFields(include=["y", "z"], swap_item_cls=SmallItem))
926-
item = await page.to_item()
927-
assert item == SmallItem(y=2, z=3)
928-
assert page.fields_to_extract == {"y", "z"}
929-
assert page.call_counter == {"y": 1, "z": 1}
930-
931-
# If page object supplies the new item class with unknown fields, it should
932-
# raise an error
933-
expected_type_error_msg = r"__init__\(\) got an unexpected keyword argument 'x'"
934-
page = BigPage(SelectFields(swap_item_cls=SmallItem))
935-
with pytest.raises(TypeError, match=expected_type_error_msg):
936-
await page.to_item()
937-
assert page.fields_to_extract == {"x", "y", "z"}

web_poet/fields.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,3 @@ class SelectFields:
285285
#: Setting it to ``"raise"`` would raise an :class:`AttributeError`,
286286
#: ``"warn"`` produces a :class:`UserWarning`, while ``"ignore"`` does nothing.
287287
on_unknown_field: UnknownFieldActions = "raise"
288-
289-
#: Swaps the item class that the page object returns. Use this to prevent
290-
#: errors when excluding a required field by swapping the assigned item class
291-
#: without the required field.
292-
swap_item_cls: Optional[Type] = None

web_poet/pages.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ async def _partial_item(self) -> Optional[Any]:
120120

121121
return await item_from_fields(
122122
self,
123-
item_cls=select_fields.swap_item_cls or self.item_cls,
123+
item_cls=self.item_cls,
124124
skip_nonitem_fields=self._skip_nonitem_fields,
125125
field_names=self.fields_to_extract,
126126
on_unknown_field=select_fields.on_unknown_field,

0 commit comments

Comments
 (0)