-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
Add typing hint to httpcache downloadermiddlewares #4243
Conversation
This commit adds typing hint to httpcache downloadermiddlewares as a test for scrapy#4041
self.storage = load_object(settings['HTTPCACHE_STORAGE'])(settings) | ||
self.ignore_missing = settings.getbool('HTTPCACHE_IGNORE_MISSING') | ||
self.policy: Union[DummyPolicy, RFC2616Policy] = load_object(settings['HTTPCACHE_POLICY'])(settings) | ||
self.storage: Union[DbmCacheStorage, FilesystemCacheStorage] = load_object(settings['HTTPCACHE_STORAGE'])(settings) |
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.
These can be any other (user-defined) classes, right?
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.
Yes, and now we have only these classes (DummpyPolicy
, RFC2616Policy
, DbmCacheStorage
, FilesystemCacheStorage
) in this project.
Tests failed because of "SyntaxError: future feature annotations is not defined" on 3.6 and "SyntaxError: invalid syntax" at |
Yes. For the classmethod |
This commit fixes the subclass typing hint with TypeVar
Codecov Report
@@ Coverage Diff @@
## master #4243 +/- ##
==========================================
+ Coverage 84.81% 84.82% +0.01%
==========================================
Files 163 163
Lines 9987 9997 +10
Branches 1487 1487
==========================================
+ Hits 8470 8480 +10
Misses 1254 1254
Partials 263 263
|
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 I'm not mistaken, you should be able to work around the classmethod issue by supplying the name of the class, i.e.: def from_crawler(cls, crawler: Crawler) -> "HttpCacheMiddleware"
Also, I don't think we are (yet) in a position to support variable annotations, we still support Python 3.5 (#4041 (comment))
Yes, in Python 3.5 the variable annotation is not supported and I have removed the typing hint for variables. Adding typing hints is not a simple work as I expected before. From this PR we have some valuable discussion and we can see there are still some certain misunderstand for the typing module in Python 3.5+. Practicing more will be much helpful. |
Since some of the members of this project have approved this commit, I think maybe we can write a guidelines about how to add the typing hint for Scrapy? |
I guess we can extend the contributing page with such guidelines. What do you have in mind? |
Sounds good. We should also think about adding a typing check step to the tox/travis configurations. |
@@ -33,19 +40,19 @@ def __init__(self, settings, stats): | |||
self.stats = stats | |||
|
|||
@classmethod | |||
def from_crawler(cls, crawler): | |||
def from_crawler(cls, crawler: Crawler) -> "HttpCacheMiddleware": |
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.
this method doesn't return HttpCacheMiddleware in a subclass, see https://stackoverflow.com/questions/44640479/mypy-annotation-for-classmethod-returning-instance for a discussion
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.
Em, I can see this will be a hot topic in this typing hint topic.
First, should all kinds of possible typing hint be covered? Using typing.Type
to cover all subclasses is not a problem. The problem is should we use typing.Type
for all classes in Scrapy, just like the previous Request
and Response
typing hint in this pull request?
Second, annotating the class itself is supported until python 3.7 with the help of __future__.annotations
. Since Scrapy is still compatible with python 3.5, what should we do for classmethod
return at this moment? A string typing hint is not a good solution in my opinion.
+1 to add automatic type checks on CI; I think this should be done even before merging any type annotations. |
From the discussion in this pull request, I can see the following points should be included in this document:
There are many things uncertain about typing hint. But at least we can start from some easy and clear places now, and keep discussing at the same time until we find a good solution.
|
Yes, this will be much helpful for the contributors to test their code before creating the pull request.
|
Typing hint update
They were introduced in PEP 526 - https://www.python.org/dev/peps/pep-0526/
Typing build added @kmike 🚀 |
Tests are failing because |
Thank you! |
…)" This reverts commit 55edf8d.
This commit adds typing hint to httpcache downloadermiddlewares as a
test for #4041
This commit is a trial for adding typing hint to this project