Improve static typing of 3rd party imports #7496
Merged
Merged
Conversation
sigmavirus24
approved these changes
Jun 4, 2026
Contributor
|
Thank you |
nateprewitt
reviewed
Jun 8, 2026
Comment on lines
+50
to
+53
| if TYPE_CHECKING: | ||
| import chardet | ||
| else: | ||
| chardet = _resolve_char_detection() |
Member
There was a problem hiding this comment.
I think this change isn't quite right. We're now type checking against the wrong character detection library and losing type nuance for what actually gets returned by _resolve_char_detection at runtime.
That's not the end of the world, because I don't think we're truly making use of the 3p types, but is probably something we want to align with the actual default behavior of Requests in the future.
Contributor
Author
There was a problem hiding this comment.
Sharp. But unfortunately it's not possible to express the union of specific module types in Python's type system, i.e.
chardet: typing.Module["chardet"] | typing.Module["charset_normalizer"]isn't possible.
So this is the best we can do here, I'm afraid.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This adds
chardet,cryptography, andPyOpenSSLto thetypecheckdependency group. This way, Pyright is able to resolve these imports, removing the need for a bunch of# type: ignorecomments.I verified that mypy (2.1.0) doesn't report any new errors.
And in case anyone is wondering: I didn't use any AI for this (I'm pretty sure I'm a human).