1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
|
import pytest
import attrs
from web_poet import (
ItemPage,
field,
item_from_fields,
item_from_fields_sync,
)
class Page(ItemPage):
@field
def name(self):
return "hello"
def process_price(value: float) -> float:
return max([0, value])
class TypedPage(ItemPage):
@field
def description(self) -> str:
return "hello"
@field(out=[str.strip])
def name(self) -> str:
return "hello"
@field(out=[process_price, str])
def price(self) -> float:
return 123.0
@field()
def currency(self) -> str:
return "$"
@field
async def adescription(self) -> str:
return "hello"
@field(out=[str.strip])
async def aname(self) -> str:
return "hello"
@field(out=[process_price, str])
async def aprice(self) -> float:
return 123.0
@field()
async def acurrency(self) -> str:
return "$"
@attrs.define
class Item:
name: str
@pytest.mark.mypy_testing
@pytest.mark.xfail
def test_field_type_no_params() -> None:
page = TypedPage()
reveal_type(page.description) # R: builtins.str
@pytest.mark.mypy_testing
@pytest.mark.xfail
def test_field_type() -> None:
page = TypedPage()
reveal_type(page.currency) # R: builtins.str
@pytest.mark.mypy_testing
@pytest.mark.xfail
def test_field_type_out() -> None:
page = TypedPage()
reveal_type(page.name) # R: builtins.str
@pytest.mark.mypy_testing
@pytest.mark.xfail
def test_field_type_changed_type() -> None:
page = TypedPage()
reveal_type(page.price) # R: builtins.str
@pytest.mark.mypy_testing
@pytest.mark.xfail
async def test_field_type_no_params_async() -> None:
page = TypedPage()
reveal_type(await page.adescription) # R: builtins.str
@pytest.mark.mypy_testing
@pytest.mark.xfail
async def test_field_type_async() -> None:
page = TypedPage()
reveal_type(await page.acurrency) # R: builtins.str
@pytest.mark.mypy_testing
@pytest.mark.xfail
async def test_field_type_out_async() -> None:
page = TypedPage()
reveal_type(await page.name) # R: builtins.str
@pytest.mark.mypy_testing
@pytest.mark.xfail
async def test_field_type_changed_type_async() -> None:
page = TypedPage()
reveal_type(await page.price) # R: builtins.str
@pytest.mark.mypy_testing
async def test_item_from_fields() -> None:
page = Page()
item1 = await item_from_fields(page, item_cls=dict)
reveal_type(item1) # R: builtins.dict[Any, Any]
item2 = await item_from_fields(page, item_cls=Item)
reveal_type(item2) # R: __main__.Item
@pytest.mark.mypy_testing
def test_item_from_fields_sync() -> None:
page = Page()
item1 = item_from_fields_sync(page, item_cls=dict)
reveal_type(item1) # R: builtins.dict[Any, Any]
item2 = item_from_fields_sync(page, item_cls=Item)
reveal_type(item2) # R: __main__.Item
@pytest.mark.mypy_testing
@pytest.mark.xfail
async def test_item_from_fields_default_item_cls() -> None:
page = Page()
item1 = await item_from_fields(page)
reveal_type(item1) # R: builtins.dict[Any, Any]
|