Skip to content

Improve type annotations for several HTTPAdapter methods#15778

Merged
srittau merged 10 commits into
python:mainfrom
mrhx01:fix-requests-adapters-types
May 12, 2026
Merged

Improve type annotations for several HTTPAdapter methods#15778
srittau merged 10 commits into
python:mainfrom
mrhx01:fix-requests-adapters-types

Conversation

@mrhx01
Copy link
Copy Markdown
Contributor

@mrhx01 mrhx01 commented May 12, 2026

improve type annotations for several HTTPAdapter methods
This PR improves the type hints for several methods in requests.adapters.HTTPAdapter to better reflect the actual runtime behavior and improve static type checking with tools such as mypy, pyright, and pylance.

The following methods now include more precise type annotations:

init_poolmanager
proxy_manager_for
add_headers
request_url
proxy_headers
These methods previously used untyped parameters or lacked explicit return types in the type stubs, which could lead to incomplete or inaccurate type inference in downstream projects.

Key improvements include:

Adding explicit parameter types (e.g. PreparedRequest, Mapping[str, str] | None)
Adding explicit return types such as None, str, and dict[str, str]
Using Any for flexible keyword arguments (**pool_kwargs, **proxy_kwargs, **kwargs) where the accepted parameters depend on urllib3 internals.
These changes do not modify runtime behavior and are strictly improvements to typing clarity and developer tooling support.

@github-actions

This comment has been minimized.

@srittau
Copy link
Copy Markdown
Collaborator

srittau commented May 12, 2026

A general remark: We need a comment for each Any, explaining what types are allowed, why it's used instead of a concrete type, or how it's used.

@github-actions

This comment has been minimized.

@github-actions
Copy link
Copy Markdown
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@mrhx01
Copy link
Copy Markdown
Contributor Author

mrhx01 commented May 12, 2026

I've addressed the review feedback: added explanatory comments for every Any usage in the stub file, including **pool_kwargs, **proxy_kwargs, return type of proxy_manager_for, **kwargs in add_headers, and the conn parameter in cert_verify. No functional changes, only type hint improvements.

Please let me know if anything else is needed.

A general remark: We need a comment for each Any, explaining what types are allowed, why it's used instead of a concrete type, or how it's used.

Copy link
Copy Markdown
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

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

Thanks!

@srittau srittau merged commit 0c702b5 into python:main May 12, 2026
42 checks passed
@mrhx01 mrhx01 deleted the fix-requests-adapters-types branch May 12, 2026 11:40
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.

2 participants