-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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 type hints for _collections.py #2157
Add type hints for _collections.py #2157
Conversation
Codecov Report
@@ Coverage Diff @@
## main #2157 +/- ##
===========================================
- Coverage 100.00% 99.95% -0.05%
===========================================
Files 25 25
Lines 2219 2213 -6
===========================================
- Hits 2219 2212 -7
- Misses 0 1 +1
Continue to review full report at Codecov.
|
@franekmagiera Thank you for this! There's already an existing pull request for collections: #2115. Can you take a look and see what you've done differently? |
Oh, it's a shame I did not notice that PR. Not that many differences, except some things I mentioned are already implemented. In #2115
For My
Not sure what you want to do with this PR. As this is mostly duplicate work I am ok with just closing it. |
Thanks for telling us the precise differences! @haikuginger What are your thoughts on this? |
@pquentin thoughts inline:
The try/catch construct was a suggestion of @SethMichaelLarson, IIRC. I actually like this structure because it encodes the reordering of the data structure on insert more explicitly than the
I need to do some tweaking on
It looks equally correct to me...
I think the lack of Overall, there's definite overlap; I'd like to proceed with my version and get it to a mergeable state. |
I'm in complete agreement with @haikuginger, thanks all for figuring out the best way forwards! |
@franekmagiera Hopefully this doesn't discourage you one bit, your work is very high quality and we all hope you are willing to provide type coverage or other improvements! 🙌 Thanks again for this contribution!! |
No worries, things like this happen ;) It's my mistake that I did not notice that @haikuginger already started working on this. Still, it was a good chance to brush up on type hinting. |
Awesome, thank you all! @haikuginger When your PR is ready, can you ask @franekmagiera to review it? They're now an expert in collections.py type hints too. :D @franekmagiera Would you like to work on typing another part of urllib3? Not collections, exceptions or response, though! |
@pquentin I am playing around with type hints for |
@pquentin I think I can only request a review from within the organization, but I'd be happy to take a review from @franekmagiera now if they'd like to add it! |
Related to #1897
Took a stab at adding type hints for
_collections.py
.A couple of things to mention:
RecentlyUsedContainer.keys
is ignored because in order to be consistent with base classMutableMapping
and not break Liskov substitution principle (which mypy complains about) it would have to return AbstractSet which does not preserve ordering of the keys. (Same forHTTPHeaderDict.keys
)HTTPHeaderDict.__setitem__
to not break LSP (similar situation as above).HTTPHeaderDict.__contains__
it would make sense for the key to be of typestr
, but this would again violate LSP, so I set the key type toobject
and added a check inside of the method (if you will be fine with this solution I will add a test for__contains__
).pop
andgetlist
use__marker
asdefault
, which means thedefault
type has to be aUnion
ofobject
andstr
orList[str]
. Not sure if it would make sense to use""
and[]
as defaults, for now I am just casting the values.headers
would be. For now I decided to useUnion["HTTPHeaderDict", Mapping[str, str], Iterator[Tuple[str, str]]]
. Judging by the tests for this class, it would also make sense to include something likeNonMappingHeaderContainer
that does not implement everything thatMapping
does, but just has thekeys
and__getitem__
attributes. I tried defining an abstract base class to act as an interface and add it to the union but it did not do much - I still had to ignore lines 293 and 294 to not get an error. Not sure if it is connected to Type inference and hasattr python/mypy#1424 (maybe it would be better tocast
as this issue indicates), or if this is something that could be easily fixed.