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
Improve type hints for copy() and replace() in Request and Response. #6143
base: master
Are you sure you want to change the base?
Conversation
@@ -27,6 +27,7 @@ def _build_redirect_request( | |||
redirect_request = source_request.replace( | |||
url=url, | |||
**kwargs, | |||
cls=None, |
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.
Without this line the type of redirect_request
is inferred to be Any
.
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.
Could you please add a test for this case? I guess it's **kwargs
handling?
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 it's because it cannot know that kwargs
passed to _build_redirect_request
doesn't contain cls
.
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.
Also, what test do you mean?
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.
If kwargs are passed, what's the reveal_type of the result
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've added tests that check that the result is Any
unless cls
was also passed.
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.
Not sure if it's a useful thing to check :)
Codecov Report
Additional details and impacted files@@ Coverage Diff @@
## master #6143 +/- ##
==========================================
- Coverage 88.89% 88.83% -0.07%
==========================================
Files 161 161
Lines 11776 11818 +42
Branches 1913 1924 +11
==========================================
+ Hits 10468 10498 +30
- Misses 964 967 +3
- Partials 344 353 +9
|
The approach looks good 👍 It seems the typing becomes more tricky now, especially as we have py.typed in scrapy now - it affects the user code, not just the internal type consistency. What do you think about adding some basic typing tests, at least for the important public interfaces like Response? |
What do you think should be tested, at least as a first step? |
I think we can do it gradually - start with the fixes, as compared to the previous implementation. E.g. that text_response.replace(...).text works properly for different scenarios (raises or doesn't raise mypy errors, as appropriate). |
Should the typing tests run on 3.8 or 3.12? We need different syntax for them. |
Not sure. Is it because the error messages differ, or is it about something else? Can we rely on mypy error codes instead of exact error messages more? |
Yes, mypy suggests
We can, though we run the test on a single Python version anyway, so we know which message will be produced. |
@@ -18,6 +18,9 @@ jobs: | |||
- python-version: 3.8 | |||
env: | |||
TOXENV: typing | |||
- python-version: 3.8 |
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 wonder if we should run it in the latest Python instead.
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.
Should we also run the typing
check on the latest Python then, as I think they check the same thing? In that case we will stop checking (and so supporting) that the type hints work as expected on older Python versions and I thought it's a nice thing to support.
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 mean, for users that use Scrapy on older Python versions and run mypy on their code)
This works. Do you think thought that the more detailed matching of the error messages help us to catch more issues, as compared to just checking the mypy error code? |
Yeah, I think error codes can be too broad to be useful. |
As discussed in #6127