From 028b7592a034a02ca48aa362eda31638d58429d2 Mon Sep 17 00:00:00 2001 From: Martin Vrachev Date: Fri, 3 Dec 2021 17:23:42 +0200 Subject: [PATCH] Apply linters on tests/simple_server.py Apply all 4 linters on tests/simple_server.py, so we can lint it in the future and not rename and exclude it. As we are going to use part or most of tests/utils.py after we remove the test files testing the old code, then we would need to keep the simple_server.py file. It's currently used in tests/updater_ng.py testing the new updater implementation. Black and isort changes where automatically made. The only manual changes are: - pylint disable once in can_connect - add type annotations - simplifications around setting up the handler variable - function docstrings Signed-off-by: Martin Vrachev --- tests/simple_server.py | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/tests/simple_server.py b/tests/simple_server.py index 74e84f0d..45d481be 100755 --- a/tests/simple_server.py +++ b/tests/simple_server.py @@ -25,17 +25,21 @@ http://docs.python.org/library/simplehttpserver.html#module-SimpleHTTPServer """ -import sys -import random import socketserver +import sys from http.server import SimpleHTTPRequestHandler +from typing import Type, Union class QuietHTTPRequestHandler(SimpleHTTPRequestHandler): - """A SimpleHTTPRequestHandler that does not write incoming requests to - stderr. """ - def log_request(self, code='-', size='-'): - pass + """A SimpleHTTPRequestHandler that does not write incoming requests to + stderr.""" + + def log_request( + self, code: Union[int, str] = "-", size: Union[int, str] = "-" + ) -> None: + pass + # NOTE: On Windows/Python2 tests that use this simple_server.py in a # subprocesses hang after a certain amount of requests (~68), if a PIPE is @@ -43,22 +47,20 @@ def log_request(self, code='-', size='-'): # we silence the HTTP messages. # If you decide to receive the HTTP messages, then this bug # could reappear. -use_quiet_http_request_handler = True -if len(sys.argv) > 2: - use_quiet_http_request_handler = sys.argv[2] +# pylint: disable=invalid-name +handler: Type[Union[SimpleHTTPRequestHandler, QuietHTTPRequestHandler]] -if use_quiet_http_request_handler: - handler = QuietHTTPRequestHandler +if len(sys.argv) > 2 and sys.argv[2]: + handler = QuietHTTPRequestHandler else: - handler = SimpleHTTPRequestHandler + handler = SimpleHTTPRequestHandler # Allow re-use so you can re-run tests as often as you want even if the # tests re-use ports. Otherwise TCP TIME-WAIT prevents reuse for ~1 minute socketserver.TCPServer.allow_reuse_address = True -httpd = socketserver.TCPServer(('localhost', 0), handler) -port_message = 'bind succeeded, server port is: ' \ - + str(httpd.server_address[1]) +httpd = socketserver.TCPServer(("localhost", 0), handler) +port_message = "bind succeeded, server port is: " + str(httpd.server_address[1]) print(port_message) httpd.serve_forever()