Skip to content

skip_nonitem_fields=True doesn't work when the page object is an attrs class #141

@BurnzZ

Description

@BurnzZ

Currently, this works fine:

import attrs
from web_poet import HttpResponse, Returns, ItemPage, field

@attrs.define
class BigItem:
    x: int
    y: int

class BigPage(ItemPage[BigItem]):
    @field
    def x(self):
        return 1

    @field
    def y(self):
        return 2

@attrs.define
class SmallItem:
    x: int

class SmallXPage(BigPage, Returns[SmallItem], skip_nonitem_fields=True):
    pass

page = SmallXPage()
item = await page.to_item()

print(page._skip_nonitem_fields)  # True
print(item)  # SmallItem(x=1)

However, if we define an attrs class to have some page dependencies, it doesn't work:

from web_poet import PageParams

@attrs.define
class SmallPage(BigPage, Returns[SmallItem], skip_nonitem_fields=True):
    params: PageParams

page = SmallPage(params=PageParams())
print(page._skip_nonitem_fields)  # False
item = await page.to_item()  # TypeError: __init__() got an unexpected keyword argument 'y'

From the examples above, this stems from page._skip_nonitem_fields being set to False when the page object is defined as an attrs class.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions