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

Improve type hints for copy() and replace() in Request and Response. #6143

Open
wants to merge 14 commits into
base: master
Choose a base branch
from

Conversation

wRAR
Copy link
Member

@wRAR wRAR commented Nov 11, 2023

As discussed in #6127

@@ -27,6 +27,7 @@ def _build_redirect_request(
redirect_request = source_request.replace(
url=url,
**kwargs,
cls=None,
Copy link
Member Author

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.

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 please add a test for this case? I guess it's **kwargs handling?

Copy link
Member Author

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.

Copy link
Member Author

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?

Copy link
Member

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

Copy link
Member Author

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.

Copy link
Member Author

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 :)

Copy link

codecov bot commented Nov 11, 2023

Codecov Report

Merging #6143 (6b75d8f) into master (198f5cf) will decrease coverage by 0.07%.
Report is 9 commits behind head on master.
The diff coverage is 68.42%.

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     
Files Coverage Δ
scrapy/downloadermiddlewares/httpcompression.py 91.07% <100.00%> (ø)
scrapy/downloadermiddlewares/redirect.py 94.38% <ø> (ø)
scrapy/http/response/__init__.py 92.47% <72.72%> (-2.88%) ⬇️
scrapy/http/request/json_request.py 90.90% <63.63%> (-9.10%) ⬇️
scrapy/http/request/__init__.py 92.92% <61.53%> (-4.88%) ⬇️

... and 8 files with indirect coverage changes

@kmike
Copy link
Member

kmike commented Nov 11, 2023

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?

@wRAR
Copy link
Member Author

wRAR commented Nov 11, 2023

What do you think should be tested, at least as a first step?

@kmike
Copy link
Member

kmike commented Nov 11, 2023

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).

@wRAR
Copy link
Member Author

wRAR commented Nov 12, 2023

Should the typing tests run on 3.8 or 3.12? We need different syntax for them.

@kmike
Copy link
Member

kmike commented Nov 12, 2023

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?

@wRAR
Copy link
Member Author

wRAR commented Nov 12, 2023

Is it because the error messages differ, or is it about something else?

Yes, mypy suggests A | B on newer Python but Union[A, B] on older Python.

Can we rely on mypy error codes instead of exact error messages more?

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
Copy link
Member

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.

Copy link
Member Author

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.

Copy link
Member Author

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)

tox.ini Show resolved Hide resolved
@kmike
Copy link
Member

kmike commented Dec 11, 2023

@wRAR

We can, though we run the test on a single Python version anyway, so we know which message will be produced.

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?

@wRAR
Copy link
Member Author

wRAR commented Dec 11, 2023

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.

@wRAR wRAR requested a review from kmike March 5, 2024 17:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants