Skip to content

Improve static typing of 3rd party imports #7496

Merged
sigmavirus24 merged 3 commits into
psf:mainfrom
jorenham:typing/imports
Jun 4, 2026
Merged

Improve static typing of 3rd party imports #7496
sigmavirus24 merged 3 commits into
psf:mainfrom
jorenham:typing/imports

Conversation

@jorenham

@jorenham jorenham commented Jun 4, 2026

Copy link
Copy Markdown
Contributor

This adds chardet, cryptography, and PyOpenSSL to the typecheck dependency group. This way, Pyright is able to resolve these imports, removing the need for a bunch of # type: ignore comments.

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

@sigmavirus24 sigmavirus24 merged commit e50e594 into psf:main Jun 4, 2026
33 checks passed
@sigmavirus24

Copy link
Copy Markdown
Contributor

Thank you

@jorenham jorenham deleted the typing/imports branch June 4, 2026 17:20
Comment thread src/requests/compat.py
Comment on lines +50 to +53
if TYPE_CHECKING:
import chardet
else:
chardet = _resolve_char_detection()

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@jorenham jorenham Jun 8, 2026

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants