-
-
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
Inline typechecking for exceptions and add typechecking for collections #2115
Inline typechecking for exceptions and add typechecking for collections #2115
Conversation
Codecov Report
Continue to review full report at Codecov.
|
defect, (StartBoundaryNotFoundDefect, MultipartInvariantViolationDefect) | ||
) | ||
] | ||
|
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 figured since we're going to start protecting this with typechecking we could remove the runtime check on if attributes exist. Can revert though.
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 is all fine since we have the isinstance
check, so we don't have to worry about users sending weird objects into this function
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.
Thanks for this, some early comments more coming later.
4c6d050
to
d95d9db
Compare
Hi, took a look at |
Also, regarding making |
Are you talking about how we describe the API? If so, then not strictly speaking. If we implemented a protocol in an
Using string annotations results in a |
1dc707a
to
d80dd29
Compare
Tagging @SethMichaelLarson for re-review. I think this is ready to merge. |
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.
Thanks for the changes, more comments for you!
src/urllib3/_collections.py
Outdated
... | ||
|
||
@overload | ||
def getlist(self, key: str, default: List[str]) -> List[str]: |
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.
Wouldn't this override need to be Union[List[str], DefaultType
where DefaultType
is whatever type default
is?
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.
Yeah, if we want to support some default that isn't List[str]
, we'd need to do this:
@overload
def getlist(self, key: str, default: _DT) -> Union[List[str], _DT]]: ...
(in which case Union[List[str], _DT]
collapses to List[str]
in situations where _DT
can be proven to be List[str]
)
The question is whether we want to support passing a default that isn't List[str]
; I can take a look through the existing codebase to see what we do and where (the most likely case is that we pass in None
in some places), and at typeshed
to see what they do for builtins that accept defaults.
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.
LGTM, thanks for moving this mountain! 🎉
This is a first step towards moving towards inline types for all of
urllib3
. For now, this just fixes and extends the existing types for theexceptions
module, but adds typing for the_collections
andutils.response
modules.Note that because not all of our codebase is covered by typechecking, we can be pretty sure that these types are internally consistent, but not that they're correct. We'll need a lot more type coverage before we can be sure of that. For now, existing tests pass with minimal modifications, which is possibly the best we can do.