Skip to content

What about RequestHandlerClass being generic for its server type? #16428

Description

@Hibou57

Creating an HTTPServer, requires a RequestHandlerClass as the second parameter. According to the documentation, this class may be derived from either:

Each of these have a server instance variable. Its type is BaseServer. But is it likely the request handler will be designed to work with a paired server class, not the base one.

Typically, one define a MyServer class derived from HTTPServer, and a MyHandler class derived from BaseHTTPRequestHandler or one of the other two. A MyServer will be created with MyHandler as the second argument and it will create possibly multiple instance of it, which will have the MyServer instance assigned to their server variable. And indeed, MyHandler is typically written to expect server to be a MyServer, they are typically designed to work together.

It cannot be expressed with typing, because the type of this variable is said to be BaseServer.

A work around is to have something like this, at the start of every MyHandler method using self.server:

if not isinstance(self.server, MyServer):
    raise TypeError()

But it would be better to express it with the types. It is not possible so far, while it could be possible if RequestHandlerClass was generic. The generic parameter would be the actual expected class of the server associated to it. Actually, this is the three base classes listed above, which should be generic and the type of the RequestHandlerClass parameter would report it.

I don’t know what happens under the hood, so I don’t know what need to be changed in BaseServer to ensure it receive a RequestHandlerClass whose generic argument match its self actual class (the one of the BaseServer).

There is the work-around above, but still, it would be cleaner if these classes was generic.

Have a nice afternoon and if it is for tomorrow, may it be for tomorrow

Activity

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