Thanks to visit codestin.com
Credit goes to github.com

Skip to content

sessions: fix hooks type#7425

Merged
nateprewitt merged 1 commit into
psf:mainfrom
bastimeyer:typing/baserequestkwargs-hooksinputtype
May 11, 2026
Merged

sessions: fix hooks type#7425
nateprewitt merged 1 commit into
psf:mainfrom
bastimeyer:typing/baserequestkwargs-hooksinputtype

Conversation

@bastimeyer
Copy link
Copy Markdown
Contributor

The hook keyword in Session.request() should be of type HooksInputType | None, not HooksType.

This is already correctly defined in models.Request.__init__():

  • class Request(RequestHooksMixin):
    """A user-created :class:`Request <Request>` object.
    Used to prepare a :class:`PreparedRequest <PreparedRequest>`, which is sent to the server.
    :param method: HTTP method to use.
    :param url: URL to send.
    :param headers: dictionary of headers to send.
    :param files: dictionary of {filename: fileobject} files to multipart upload.
    :param data: the body to attach to the request. If a dictionary or
    list of tuples ``[(key, value)]`` is provided, form-encoding will
    take place.
    :param json: json for the body to attach to the request (if files or data is not specified).
    :param params: URL parameters to append to the URL. If a dictionary or
    list of tuples ``[(key, value)]`` is provided, form-encoding will
    take place.
    :param auth: Auth handler or (user, pass) tuple.
    :param cookies: dictionary or CookieJar of cookies to attach to this request.
    :param hooks: dictionary of callback hooks, for internal usage.
    Usage::
    >>> import requests
    >>> req = requests.Request('GET', 'https://httpbin.org/get')
    >>> req.prepare()
    <PreparedRequest [GET]>
    """
    method: str | None
    url: _t.UriType | None
    headers: CaseInsensitiveDict[str] | Mapping[str, str | bytes] | None
    files: _t.FilesType
    data: _t.DataType
    json: _t.JsonType
    params: _t.ParamsType
    auth: _t.AuthType
    cookies: RequestsCookieJar | CookieJar | dict[str, str] | None
    def __init__(
    self,
    method: str | None = None,
    url: _t.UriType | None = None,
    headers: Mapping[str, str | bytes] | None = None,
    files: _t.FilesType = None,
    data: _t.DataType = None,
    params: _t.ParamsType = None,
    auth: _t.AuthType = None,
    cookies: RequestsCookieJar | CookieJar | dict[str, str] | None = None,
    hooks: _t.HooksInputType | None = None,
    json: _t.JsonType = None,
    ) -> None:

But sessions.Session.request() and the associated _types.BaseRequestKwargs are wrong:

  • def request(
    self,
    method: str,
    url: _t.UriType,
    params: _t.ParamsType = None,
    data: _t.DataType = None,
    headers: Mapping[str, str | bytes] | None = None,
    cookies: RequestsCookieJar | CookieJar | dict[str, str] | None = None,
    files: _t.FilesType = None,
    auth: _t.AuthType = None,
    timeout: _t.TimeoutType = None,
    allow_redirects: bool = True,
    proxies: dict[str, str] | None = None,
    hooks: _t.HooksType = None,
    stream: bool | None = None,
    verify: _t.VerifyType | None = None,
    cert: _t.CertType = None,
    json: _t.JsonType = None,
    ) -> Response:
    """Constructs a :class:`Request <Request>`, prepares it and sends it.
    Returns :class:`Response <Response>` object.
    :param method: method for the new :class:`Request` object.
    :param url: URL for the new :class:`Request` object.
    :param params: (optional) Dictionary or bytes to be sent in the query
    string for the :class:`Request`.
    :param data: (optional) Dictionary, list of tuples, bytes, or file-like
    object to send in the body of the :class:`Request`.
    :param json: (optional) json to send in the body of the
    :class:`Request`.
    :param headers: (optional) Dictionary of HTTP Headers to send with the
    :class:`Request`.
    :param cookies: (optional) Dict or CookieJar object to send with the
    :class:`Request`.
    :param files: (optional) Dictionary of ``'filename': file-like-objects``
    for multipart encoding upload.
    :param auth: (optional) Auth tuple or callable to enable
    Basic/Digest/Custom HTTP Auth.
    :param timeout: (optional) How many seconds to wait for the server to send
    data before giving up, as a float, or a :ref:`(connect timeout,
    read timeout) <timeouts>` tuple.
    :type timeout: float or tuple
    :param allow_redirects: (optional) Set to True by default.
    :type allow_redirects: bool
    :param proxies: (optional) Dictionary mapping protocol or protocol and
    hostname to the URL of the proxy.
    :param hooks: (optional) Dictionary mapping hook name to one event or
    list of events, event must be callable.
    :param stream: (optional) whether to immediately download the response
    content. Defaults to ``False``.
    :param verify: (optional) Either a boolean, in which case it controls whether we verify
    the server's TLS certificate, or a string, in which case it must be a path
    to a CA bundle to use. Defaults to ``True``. When set to
    ``False``, requests will accept any TLS certificate presented by
    the server, and will ignore hostname mismatches and/or expired
    certificates, which will make your application vulnerable to
    man-in-the-middle (MitM) attacks. Setting verify to ``False``
    may be useful during local development or testing.
    :param cert: (optional) if String, path to ssl client cert file (.pem).
    If Tuple, ('cert', 'key') pair.
    :rtype: requests.Response
    """
  • class BaseRequestKwargs(TypedDict, total=False):
    headers: Mapping[str, str | bytes] | None
    cookies: RequestsCookieJar | CookieJar | dict[str, str] | None
    files: FilesType
    auth: AuthType
    timeout: TimeoutType
    allow_redirects: bool
    proxies: dict[str, str] | None
    hooks: HooksType
    stream: bool | None
    verify: VerifyType | None
    cert: CertType

@nateprewitt
Copy link
Copy Markdown
Member

I'm on mobile but this seems right based on what you described. I'll take a look at some point today. Thanks, @bastimeyer!

@nateprewitt nateprewitt merged commit b684dcb into psf:main May 11, 2026
33 checks passed
@bastimeyer bastimeyer deleted the typing/baserequestkwargs-hooksinputtype branch May 11, 2026 16:15
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