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
Creating an
HTTPServer, requires aRequestHandlerClassas the second parameter. According to the documentation, this class may be derived from either:BaseHTTPRequestHandlerSimpleHTTPRequestHandlerCGIHTTPRequestHandlerFrom: https://docs.python.org/3/library/http.server.html#http.server.HTTPServer
Each of these have a
serverinstance variable. Its type isBaseServer. 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
MyServerclass derived fromHTTPServer, and aMyHandlerclass derived fromBaseHTTPRequestHandleror one of the other two. AMyServerwill be created withMyHandleras the second argument and it will create possibly multiple instance of it, which will have theMyServerinstance assigned to theirservervariable. And indeed,MyHandleris typically written to expectserverto be aMyServer, 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
MyHandlermethod usingself.server:But it would be better to express it with the types. It is not possible so far, while it could be possible if
RequestHandlerClasswas 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 theRequestHandlerClassparameter would report it.I don’t know what happens under the hood, so I don’t know what need to be changed in
BaseServerto ensure it receive aRequestHandlerClasswhose generic argument match its self actual class (the one of theBaseServer).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