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

type hints wanted! #5845

Closed
xdewx opened this issue Mar 8, 2023 · 6 comments
Closed

type hints wanted! #5845

xdewx opened this issue Mar 8, 2023 · 6 comments

Comments

@xdewx
Copy link

xdewx commented Mar 8, 2023

Summary

scrapy.Item is just like a placeholder, neither item.field=value nor v=item.field is supported.

It would be nice if scrapy.Item is based on pydantic.BaseModel.

Motivation

For nice coding experience.

@wRAR
Copy link
Member

wRAR commented Mar 8, 2023

You can already use pydantic (or other libraries) via ItemAdapter.

@wRAR wRAR closed this as completed Mar 8, 2023
@xdewx
Copy link
Author

xdewx commented Mar 8, 2023

You can already use pydantic (or other libraries) via ItemAdapter.

Thank you very much. However, i am curious that why need scrapy.Item? It seems that use pydantic.BaseModel directly is totally fine, then there is no need to use ItemAdapter.

@wRAR
Copy link
Member

wRAR commented Mar 8, 2023

I don't think changing scrapy.Item to make it a pydantic.BaseModel is backwards-compatible.

@xdewx
Copy link
Author

xdewx commented Mar 8, 2023

I don't think changing scrapy.Item to make it a pydantic.BaseModel is backwards-compatible.

Oh ,i see. That's to say it is feasible to declare pydantic model in items.py and yield it from Spider at present, but maybe fail in the future.

And I would suggest that :

  1. scrapy could generate code with extra option for example, --item-class=[pydantic|scapy|dataclass].
  2. code generated by scrapy can have typing hits, for example:
class XxxxSpider(Spider):
    def parse(self,response: Response)->Generator:
        pass

@Gallaecio
Copy link
Member

Adding typing hints to Scrapy is an on-going effort.

@xdewx xdewx changed the title type hits wanted! type hints wanted! Mar 8, 2023
@felipeboffnunes
Copy link
Member

@xdewx may not be the best solution, but I have done a little of work using Pydantic on Scrapy to validate this idea scrapy-pydantic-loader-poc it isn't perfect but I have used this here and there

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

No branches or pull requests

4 participants