Skip to content

Incorrect type declaration for http.server.BaseHTTPRequestHandler.__init__ #7160

Closed

Description

@iamahuman

The type of the first parameter of http.server.BaseHTTPRequestHandler.__init__ shall be socketserver._RequestType, not bytes.

To fix this issue, simply remove the __init__ method and make BaseHTTPRequestHandler inherit from StreamRequestHandler.

Activity

  1. Akuli commented on Feb 8, 2022

    @Akuli
    Collaborator

    To me, it seems to always be a socket, so _RequestType isn't great either as that would mean it's sometimes tuple[bytes, socket].

  2. kkirsche commented on Aug 18, 2022

    @kkirsche
    Contributor

    Walking backwards through the cpython code:

    1. http.server.BaseHTTPRequestHandler doesn't define an __init__ method, rather it inherits it from socketserver.StreamRequestHandler
    2. socketserver.StreamRequestHandler also does not define an __init__ method, inheriting it from socketserver.BaseRequestHandler
    3. socketserver.BaseRequestHandler defines the __init__ method.

    So, with that in mind, looking at socketserver.BaseRequestHandler in typeshed, the signature is defined as:

        def __init__(self, request: _RequestType, client_address: _AddressType, server: BaseServer) -> None: ...

    The aliases here are defined as follows, while BaseServer is a socketserver class:

    _RequestType: TypeAlias = Union[_socket, tuple[bytes, _socket]]
    _AddressType: TypeAlias = Union[tuple[str, int], str]

    So with this in mind, if we look at the Python page's documentation, the request attribute isn't mentioned in any way:
    https://docs.python.org/3/library/http.server.html#http.server.BaseHTTPRequestHandler

    This attribute is also not used in any of the defined methods on the BaseHTTPRequestHandler, it is used though in the setup method of the StreamRequestHandler seen here where it gets aliased to self.connection:
    https://github.com/python/cpython/blob/main/Lib/socketserver.py#L798

    Since this attempts to call instance methods on this value without any form of type checking, I would say this is correctly typed even though it's not necessarily correctly modeled:
    https://github.com/python/cpython/blob/main/Lib/socketserver.py#L801

  3. Hibou57 commented on Sep 22, 2026

    @Hibou57

    The type of the first parameter of http.server.BaseHTTPRequestHandler.__init__ shall be socketserver._RequestType, not bytes.

    It seems to be like this in typeshed now. The issue is a bit more than four years from now, but still marked as opened. Indeed, it was likely bytes before. Reviewing something written about seven years ago, I had a BaseHTTPRequestHandler declaring to expect bytes for the request parameters. It was typechecked with MyPy, at that time. Now, reviewing everything, PyRight complained the type is not correct and say it should be as some people said in this thread : socket | tuple[bytes, socket].

    May be the issue has been solved and it was forgotten to tag it as solved?

  4. iamahuman commented on Sep 22, 2026

    @iamahuman
    Author

    Fixed in #9055.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions