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

Some callback annotations violate LSP #179

Open
wRAR opened this issue Dec 11, 2023 · 3 comments
Open

Some callback annotations violate LSP #179

wRAR opened this issue Dec 11, 2023 · 3 comments

Comments

@wRAR
Copy link
Member

wRAR commented Dec 11, 2023

scrapy.Spider.parse has a Response type hint for the response argument, and custom callbacks may also have hints for response and for some kwargs, and then in subclasses the callback with the same name can have different hints for the same arguments. In the simplest case it's just response: DummyResponse.

All of these violate the Liskov substitution principle and mypy will produce errors for the overridden callbacks. This should already happen when the base class is user-defined and will also happen for any parse() with e.g. response: DummyResponse after Scrapy with py.typed is released.

I don't currently have any suggestions for resolving this.

@kmike
Copy link
Member

kmike commented Dec 12, 2023

I wonder if we can register DummyResponse as a Response subclass. Or maybe we should have a sepatate type for annotation in Scrapy itself, to communicate what is the interface of the Response which the callbacks can expect, if it's not the Response.

@wRAR
Copy link
Member Author

wRAR commented Dec 12, 2023

DummyResponse is a Response subclass but you cannot narrow argument types in subclasses.

@Gallaecio
Copy link
Member

Annotated[Response, "dummy"]? 🙂

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

3 participants