Skip to content
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

web_poet.page_object decorator #44

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft

Conversation

kmike
Copy link
Member

@kmike kmike commented May 25, 2022

This is an experiment, to see how "functional" Page Objects could look like, if it's possible to implement them, and to get feedback about the idea.

@codecov
Copy link

codecov bot commented May 25, 2022

Codecov Report

Merging #44 (116c618) into master (4f03f0b) will decrease coverage by 3.85%.
The diff coverage is 23.52%.

❗ Current head 116c618 differs from pull request most recent head e8a03c8. Consider uploading reports for the commit e8a03c8 to get more accurate results

@@             Coverage Diff             @@
##            master      #44      +/-   ##
===========================================
- Coverage   100.00%   96.14%   -3.86%     
===========================================
  Files           14       15       +1     
  Lines          320      337      +17     
===========================================
+ Hits           320      324       +4     
- Misses           0       13      +13     
Impacted Files Coverage Δ
web_poet/functional.py 18.75% <18.75%> (ø)
web_poet/__init__.py 100.00% <100.00%> (ø)


@web_poet.page_object
def MyPage(resp: HttpResponse):
return {"title": resp.css("title::text").get()}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

alternative syntax, which doesn't destroy the function:

def parse_foo(resp: HttpResponse):
    return {"title": resp.css("title::text").get()}

FooPage = web_poet.page_object(parse_foo)

"""
class PageObject(ItemPage):
def __init__(self, *args, **kwargs):
# FIXME: save arguments as properly named attributes
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you elaborate?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is optional, to keep the generated class as close as possible to the hand-written class. See example above - MyPage has self.resp attribute.

PageObject.__init__.__annotations__ = func.__annotations__

# TODO: check that type annotations work properly
# TODO: preserve the original function as an attribute?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it to keep the implementation closer to functools.update_wrapper? Maybe we could leave this for a future change.

Copy link
Member Author

@kmike kmike May 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was thinking more about this: if you decorate function, you can't use it anymore, it becomes a class with a completely different API; this is weird. https://github.com/scrapinghub/web-poet/pull/44/files#r882977954 could be another way to address it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants