Skip to content

Update vendored Requests to 2.34.2#13982

Open
nateprewitt wants to merge 6 commits into
pypa:mainfrom
nateprewitt:requests_typing
Open

Update vendored Requests to 2.34.2#13982
nateprewitt wants to merge 6 commits into
pypa:mainfrom
nateprewitt:requests_typing

Conversation

@nateprewitt
Copy link
Copy Markdown
Member

This PR is meant to prepare a patch ahead of time for pip with Requests' migration to inline typing. This was originally raised in #13966.

Some types within pip will need to be updated to match what's being released in Requests. This PR will start in draft with only the necessary vendoring updates and then have the types added as a second commit to isolate the changes.

@nateprewitt nateprewitt force-pushed the requests_typing branch 2 times, most recently from 50c73b6 to ed0104f Compare May 7, 2026 02:53
@notatallshaw
Copy link
Copy Markdown
Member

Thanks for this!

@nateprewitt
Copy link
Copy Markdown
Member Author

nateprewitt commented May 7, 2026

I'll push up one more quick cleanup piece, but then can take it out of draft. There's a couple open questions about how I solved the typing conflicts but otherwise should be set for when we cut 2.34.0. Will just need to update version numbers.

Copy link
Copy Markdown
Member Author

@nateprewitt nateprewitt left a comment

Choose a reason for hiding this comment

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

Ok, should be good for a look.

return

raise _NotAPIContent(content_type, response.request.method)
raise _NotAPIContent(content_type, response.request.method) # type: ignore[arg-type]
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

response.request.method is potentially None here but that will never happen in pip's usage. The easiest option here is to ignore the None case. Otherwise we could handle None plumbing in the exception or add an or "" fallback.

Comment on lines +277 to +280
) -> None:
if self._ssl_context is not None:
pool_kwargs.setdefault("ssl_context", self._ssl_context)
return super().init_poolmanager( # type: ignore[misc, no-any-return]
super().init_poolmanager( # type: ignore[misc]
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This gets set on the Session and shouldn't return. It looks like this got added when truststore was added but isn't actually used.

request: MockRequest
connection: MockConnection
url: str
class MockResponse(Response):
Copy link
Copy Markdown
Member Author

@nateprewitt nateprewitt May 7, 2026

Choose a reason for hiding this comment

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

I've subclassed the mock to satisfy typing. The changes here are centralized, the other option is we'll need to suppress usage in ~6 places.

Comment thread tests/lib/wheel.py


def message_from_dict(headers: dict[str, HeaderValue]) -> Message:
def message_from_dict(headers: Mapping[str, HeaderValue]) -> Message:
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is because CaseInsensitiveDict isn't a dict, we need a wider type. Mapping or MutableMapping works, since we're only reading I went with the most specific.

Comment thread tests/lib/wheel.py
"Tag": ["-".join(parts) for parts in tags],
}
)
wheel_info: dict[str, HeaderValue] = {
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

mypy infers typing here to be dict[str, str | Sequence[str]] because of "Tag". We know it's HeaderValue (str | list[str]) but need to coerce the ambiguity.

resp = MockResponse(b"")
resp.url = url
resp.headers = headers
resp.headers.update(headers)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

{Prepared}Request and Response both use CaseInsensitiveDict for their headers. Setting it to dict works but has some subtle failure modes when casing differs. The correct way to do this is either update the existing instance or create a new one.

_real_session = PipSession()

def _fake_session_get(*args: Any, **kwargs: Any) -> dict[str, str]:
def _fake_session_get(*args: Any, **kwargs: Any) -> Response:
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Not sure why this was typed dict[str, str]. It looks like it's always been a Response.

Comment on lines -121 to -143
--- a/src/pip/_vendor/requests/compat.py
+++ b/src/pip/_vendor/requests/compat.py
@@ -7,7 +7,6 @@ between Python 2 and Python 3. It remains for backwards
compatibility until the next major version.
"""

-import importlib
import sys

# -------
@@ -30,12 +29,6 @@ import sys
def _resolve_char_detection():
"""Find supported character detection libraries."""
chardet = None
- for lib in ("chardet", "charset_normalizer"):
- if chardet is None:
- try:
- chardet = importlib.import_module(lib)
- except ImportError:
- pass
return chardet


Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Some of the patches to the same files are spread out. I had this issue last time I updated the vendored copy where you needed to do multiple diffs. I've consolidated them together for each file for simplicity of future updates since I was already touching everything. Let me know if you'd prefer I go back to the old way.

@nateprewitt nateprewitt marked this pull request as ready for review May 7, 2026 03:45
@nateprewitt nateprewitt changed the title Update vendored Requests to 2.34.0 Update vendored Requests to 2.34.1 May 13, 2026
@nateprewitt nateprewitt changed the title Update vendored Requests to 2.34.1 Update vendored Requests to 2.34.2 May 14, 2026
@ichard26 ichard26 self-requested a review May 30, 2026 01:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants