-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
New ItemPage #74
New ItemPage #74
Conversation
Codecov Report
@@ Coverage Diff @@
## master #74 +/- ##
==========================================
+ Coverage 99.80% 99.81% +0.01%
==========================================
Files 17 18 +1
Lines 509 538 +29
==========================================
+ Hits 508 537 +29
Misses 1 1
|
web_poet/pages.py
Outdated
super().__init_subclass__(**kwargs) | ||
cls._skip_nonitem_fields = skip_nonitem_fields | ||
|
||
@property |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems this can be made a "class property", but it requires a bit more work
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd propose to have it out of scope for the current PR>
# Conflicts: # docs/advanced/fields.rst # tests/test_fields.py # web_poet/pages.py
* use ``item_cls_fields=True`` argument of :func:`item_from_fields`: | ||
when ``item_cls_fields`` parameter is True, ``@fields`` which | ||
are not defined in the item are skipped. | ||
Alternatively, you can use ``skip_nonitem_fields=True`` class argument - it tells |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively, you can use ``skip_nonitem_fields=True`` class argument - it tells | |
Alternatively, you can use ``skip_nonitem_fields=True`` class argument (*default*: ``False``) - it tells |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm on fence about this suggestion :) On one hand, it makes sense. On the other hand, class arguments are so rarely used, so that thinking about default values of class arguments could be too much. I think for most users it'd be just "copy-paste skip_nonitem_fields=True
". No strong opinon on it though.
Co-authored-by: Adrián Chaves <[email protected]> Co-authored-by: Kevin Lloyd Bernal <[email protected]>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🚀
Thanks for the review @Gallaecio @BurnzZ! |
This PR is built on top of #62 and #70, and should be only considered for merging after these 2 PRs. It could also make sense to review those 2 first.
The idea here is to extend ItemPage class, so that it provides a default to_item implementation, which uses web_poet fields if they're available.
By default, dicts are created, but ItemPage can be parametrized with the item type.
In the example above,
await product_page.to_item()
would output a Product item, not a dict. Mypy is inferring types correctly for this case, i.e. it knows that item would be Product.It's possible to change the item type in a subclass, by inheriting from
Returns[MyItem]
(see the tutorial). This feature works from the output point of view, but I was unable to make typing work properly in this case (commented inline). It could be a mypy bug though, I'm not sure.Use cases with extended items (adding new fields) and with custom items (removing / changing fields) are covered in tests. There is
skip_nonitem_fields=True
argument exposed for the latter use case. #63 is a related PR which covers a remaining case (extra fields in the base class), but I'm not sure if that's a good pattern or not; I think it's better to discuss extra fields in the base classes separately..The change is almost backwards compatible. I don't expect it to break any code, but it might break some type validation using mypy. There are 2 slight incompatibilities:
super().to_item()
. It might break typing though, because mypy may issue an error (or a warning, not sure), if a regulardef to_item(self)
is defined inItemPage
subclass.TODO: