-
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
@handle_urls() with item return type #84
Merged
Merged
Changes from 11 commits
Commits
Show all changes
48 commits
Select commit
Hold shift + click to select a range
aa8698c
update handle_urls() and OverrideRule to accept data types
BurnzZ cc5ec17
update flake8 to ignore D102 in tests/po_lib_data_type
BurnzZ c135b2d
update mypy config to ignore tests.po_lib_data_type
BurnzZ 7837b3c
rename 'data_type' into 'to_return' in handle_urls() and OverrideRule
BurnzZ eb7e1b2
rename 'overrides' into 'instead_of' in @handle_urls
BurnzZ 904dd8c
rename and deprecate OverrideRule into ApplyRule
BurnzZ 03f5656
fix tests
BurnzZ 3ed8415
update CHANGELOG with regards to ApplyRule and @handle_urls changes
BurnzZ 3a5499c
create from_apply_rules method in PageObjectRegistry; deprecate from_…
BurnzZ 01112e7
rename 'web_poet.overrides' into 'web_poet.rules'
BurnzZ b1c7e14
rename PageObjectRegistry's methods: get_overrides → get_rules, searc…
BurnzZ 1afddc9
import * from 'rules' in 'overrides'
BurnzZ 144e39e
prioritize 'to_return' parameter compared to derived item_cls
BurnzZ fee63a5
fix the deprecated 'overrides' parameter not being used if present
BurnzZ 33a48ac
enable auto-conversion to url_matcher.Patterns on ApplyRules.for_patt…
BurnzZ 9b6f9c4
update all arguments of ApplyRule to be keyword-only except 'for_patt…
BurnzZ 8264565
fix mypy issue in ApplyRule tests
BurnzZ 3287881
improve tests
BurnzZ c5faf38
update docstrings/tutorials regarding the new 'to_return' parameter
BurnzZ 9729e8f
clean-up CHANGELOG formatting
BurnzZ 8efa813
update CHANGELOG to soften the value of the 'to_return' param
BurnzZ 52a6f47
update override docs to change the tone about the 'to_return' parameter
BurnzZ 2cb518b
Apply naming and grammar suggestions
BurnzZ 88c511d
test improvements
BurnzZ 928188e
remove 'preferred' param of get_item_cls()
BurnzZ 3679b59
update default behavior of @handle_urls to return dict instead of None
BurnzZ fc0ba50
improve the docstring of handle_urls()
BurnzZ 5967e34
update docs by removing tick mark chars in anchors
BurnzZ 0626e57
rename some *.com URLs into *.example in docs and tests
BurnzZ 5fdf4a1
Merge branch 'master' of ssh://github.com/scrapinghub/web-poet into h…
BurnzZ de15a86
revert default 'to_return=dict' and use 'None' instead
BurnzZ f354c4a
improve docs and code comments
BurnzZ bce97be
improve docstring of 'search_rules()'
BurnzZ 4e00ea8
Improve the docs
BurnzZ 59381a5
add reference link to Page Objects in Overrides tutorial
BurnzZ 076e7bb
remove mention of 'to_return' in @handle_url doc examples
BurnzZ 1419f7a
improve tests
BurnzZ 776cf0d
improve docstrings and warning messages
BurnzZ 42bd123
Fix test case when ensuring that ApplyRule is frozen
BurnzZ 36cd866
update tests to check each param change on hash()
BurnzZ 383b4f7
update 'Item Class' to 'item class'
BurnzZ 4755ce9
add an Overview section to the Overrides docs; rename them to Apply R…
kmike 7240c9e
bump Sphinx version
kmike 85b9b7b
simplify ApplyRule docstring
kmike ddaed2f
typo fix
kmike 660a8cd
Apply suggestions from code review
kmike 3e23c51
mention str -> Patterns conversion
kmike 2c570e2
Merge pull request #90 from scrapinghub/handle_urls-docs
kmike File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,160 @@ | ||
import attrs | ||
from url_matcher import Patterns | ||
|
||
from web_poet import Injectable, ItemPage, Returns, field, handle_urls, item_from_fields | ||
|
||
|
||
@attrs.define | ||
class Product: | ||
name: str | ||
price: float | ||
|
||
|
||
@attrs.define | ||
class ProductSimilar: | ||
name: str | ||
price: float | ||
|
||
|
||
@attrs.define | ||
class ProductMoreFields(Product): | ||
brand: str | ||
|
||
|
||
@attrs.define | ||
class ProductLessFields: | ||
BurnzZ marked this conversation as resolved.
Show resolved
Hide resolved
|
||
name: str | ||
|
||
|
||
@handle_urls("example.com") | ||
class ProductPage(ItemPage[Product]): | ||
"""A base PO to populate the Product item's fields.""" | ||
|
||
expected_instead_of = None | ||
expected_patterns = Patterns(["example.com"]) | ||
expected_to_return = Product | ||
expected_meta = {} | ||
|
||
@field | ||
def name(self) -> str: | ||
return "name" | ||
|
||
@field | ||
def price(self) -> float: | ||
return 12.99 | ||
|
||
|
||
@handle_urls("example.com", instead_of=ProductPage) | ||
class ImprovedProductPage(ProductPage): | ||
"""A custom PO inheriting from a base PO which alters some field values.""" | ||
|
||
expected_instead_of = ProductPage | ||
expected_patterns = Patterns(["example.com"]) | ||
expected_to_return = Product | ||
expected_meta = {} | ||
|
||
@field | ||
def name(self) -> str: | ||
return "improved name" | ||
|
||
|
||
@handle_urls("example.com", instead_of=ProductPage) | ||
class SimilarProductPage(ProductPage, Returns[ProductSimilar]): | ||
"""A custom PO inheriting from a base PO returning the same fields but in | ||
a different item class. | ||
""" | ||
|
||
expected_instead_of = ProductPage | ||
expected_patterns = Patterns(["example.com"]) | ||
expected_to_return = ProductSimilar | ||
expected_meta = {} | ||
|
||
|
||
@handle_urls("example.com", instead_of=ProductPage) | ||
class MoreProductPage(ProductPage, Returns[ProductMoreFields]): | ||
"""A custom PO inheriting from a base PO returning more items using a | ||
different item class. | ||
""" | ||
|
||
expected_instead_of = ProductPage | ||
expected_patterns = Patterns(["example.com"]) | ||
expected_to_return = ProductMoreFields | ||
expected_meta = {} | ||
|
||
@field | ||
def brand(self) -> str: | ||
return "brand" | ||
|
||
|
||
@handle_urls("example.com", instead_of=ProductPage) | ||
class LessProductPage( | ||
ProductPage, Returns[ProductLessFields], skip_nonitem_fields=True | ||
): | ||
"""A custom PO inheriting from a base PO returning less items using a | ||
different item class. | ||
""" | ||
|
||
expected_instead_of = ProductPage | ||
expected_patterns = Patterns(["example.com"]) | ||
expected_to_return = ProductLessFields | ||
expected_meta = {} | ||
|
||
@field | ||
def brand(self) -> str: | ||
return "brand" | ||
|
||
|
||
@handle_urls("example.com", instead_of=ProductPage, to_return=ProductSimilar) | ||
class CustomProductPage(ProductPage, Returns[Product]): | ||
"""A custom PO inheriting from a base PO returning the same fields but in | ||
a different item class. | ||
|
||
This PO is the same with ``SimilarProductPage`` but passes a ``to_return`` | ||
in the ``@handle_urls`` decorator. | ||
|
||
This tests the case that the type inside ``Returns`` should be followed and | ||
the ``to_return`` parameter from ``@handle_urls`` is ignored. | ||
""" | ||
|
||
expected_instead_of = ProductPage | ||
expected_patterns = Patterns(["example.com"]) | ||
expected_to_return = Product | ||
expected_meta = {} | ||
|
||
|
||
@handle_urls("example.com", instead_of=ProductPage, to_return=ProductSimilar) | ||
class CustomProductPageNoReturns(ProductPage): | ||
"""Same case as with ``CustomProductPage`` but doesn't inherit from | ||
``Returns[Product]``. | ||
""" | ||
|
||
expected_instead_of = ProductPage | ||
expected_patterns = Patterns(["example.com"]) | ||
expected_to_return = Product | ||
expected_meta = {} | ||
|
||
|
||
@handle_urls("example.com", to_return=Product) | ||
class CustomProductPageDataTypeOnly(Injectable): | ||
"""A PO that doesn't inherit from ``ItemPage`` and ``WebPage`` which means | ||
it doesn't inherit from the ``Returns`` class. | ||
|
||
This tests the case that the ``to_return`` parameter in ``@handle_urls`` | ||
should properly use it in the rules. | ||
""" | ||
|
||
expected_instead_of = None | ||
expected_patterns = Patterns(["example.com"]) | ||
expected_to_return = Product | ||
expected_meta = {} | ||
|
||
@field | ||
def name(self) -> str: | ||
return "name" | ||
|
||
@field | ||
def price(self) -> float: | ||
return 12.99 | ||
|
||
async def to_item(self) -> Product: | ||
return await item_from_fields(self, item_cls=Product) | ||
Gallaecio marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 think we should downplay it a bit, because most users shouldn't use to_return argument directly. What do you think about documenting it this way?
to_return
argument ofhandle_urls
decoratorThere 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.
Makes sense. Updated it in 8efa813.