From 7eab7d805e3841f3ddc82fdcef681e4b3e9bf37b Mon Sep 17 00:00:00 2001 From: Thomas Grainger Date: Fri, 18 Mar 2022 20:17:50 +0000 Subject: [PATCH] upgrade mypy to 0.941 (#3123) --- maint/requirements.in | 3 ++- maint/requirements.txt | 6 +++-- tornado/httputil.py | 2 +- tornado/test/__init__.py | 0 .../test/gettext_translations/extract_me.py | 10 +++---- tornado/test/httpclient_test.py | 6 ++--- tornado/test/ioloop_test.py | 10 +++---- tornado/test/iostream_test.py | 5 +++- tornado/test/tcpserver_test.py | 27 +++++++++---------- tornado/test/testing_test.py | 7 +++-- tornado/testing.py | 2 +- tornado/wsgi.py | 9 ++++++- 12 files changed, 51 insertions(+), 36 deletions(-) create mode 100644 tornado/test/__init__.py diff --git a/maint/requirements.in b/maint/requirements.in index 3f704d00..ea67be01 100644 --- a/maint/requirements.in +++ b/maint/requirements.in @@ -9,5 +9,6 @@ black flake8 -mypy +mypy>=0.941 +types-pycurl tox diff --git a/maint/requirements.txt b/maint/requirements.txt index d4ddf7cc..e2b3564e 100644 --- a/maint/requirements.txt +++ b/maint/requirements.txt @@ -5,8 +5,8 @@ distlib==0.3.1 filelock==3.0.12 flake8==3.8.4 mccabe==0.6.1 +mypy==0.941 mypy-extensions==0.4.3 -mypy==0.790 packaging==20.4 pathspec==0.8.0 pluggy==0.13.1 @@ -17,7 +17,9 @@ pyparsing==2.4.7 regex==2020.10.28 six==1.15.0 toml==0.10.1 +tomli==2.0.1 tox==3.20.1 typed-ast==1.4.1 -typing-extensions==3.7.4.3 +types-pycurl==7.44.7 +typing-extensions==4.1.1 virtualenv==20.1.0 diff --git a/tornado/httputil.py b/tornado/httputil.py index 12313f35..9c341d47 100644 --- a/tornado/httputil.py +++ b/tornado/httputil.py @@ -408,7 +408,7 @@ class HTTPServerRequest(object): def full_url(self) -> str: """Reconstructs the full URL for this request.""" - return self.protocol + "://" + self.host + self.uri + return self.protocol + "://" + self.host + self.uri # type: ignore[operator] def request_time(self) -> float: """Returns the amount of time it took for this request to execute.""" diff --git a/tornado/test/__init__.py b/tornado/test/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tornado/test/gettext_translations/extract_me.py b/tornado/test/gettext_translations/extract_me.py index 08b29bc5..860e3d1b 100644 --- a/tornado/test/gettext_translations/extract_me.py +++ b/tornado/test/gettext_translations/extract_me.py @@ -8,8 +8,8 @@ # 3) msgfmt tornado_test.po -o tornado_test.mo # 4) Put the file in the proper location: $LANG/LC_MESSAGES -_("school") -pgettext("law", "right") -pgettext("good", "right") -pgettext("organization", "club", "clubs", 1) -pgettext("stick", "club", "clubs", 1) +_("school") # type: ignore[name-defined] +pgettext("law", "right") # type: ignore[name-defined] +pgettext("good", "right") # type: ignore[name-defined] +pgettext("organization", "club", "clubs", 1) # type: ignore[name-defined] +pgettext("stick", "club", "clubs", 1) # type: ignore[name-defined] diff --git a/tornado/test/httpclient_test.py b/tornado/test/httpclient_test.py index af5e0189..ab93067f 100644 --- a/tornado/test/httpclient_test.py +++ b/tornado/test/httpclient_test.py @@ -139,15 +139,15 @@ class SetHeaderHandler(RequestHandler): class InvalidGzipHandler(RequestHandler): - def get(self): + def get(self) -> None: # set Content-Encoding manually to avoid automatic gzip encoding self.set_header("Content-Type", "text/plain") self.set_header("Content-Encoding", "gzip") # Triggering the potential bug seems to depend on input length. # This length is taken from the bad-response example reported in # https://github.com/tornadoweb/tornado/pull/2875 (uncompressed). - body = "".join("Hello World {}\n".format(i) for i in range(9000))[:149051] - body = gzip.compress(body.encode(), compresslevel=6) + b"\00" + text = "".join("Hello World {}\n".format(i) for i in range(9000))[:149051] + body = gzip.compress(text.encode(), compresslevel=6) + b"\00" self.write(body) diff --git a/tornado/test/ioloop_test.py b/tornado/test/ioloop_test.py index c2851650..825f5a2a 100644 --- a/tornado/test/ioloop_test.py +++ b/tornado/test/ioloop_test.py @@ -1,5 +1,6 @@ from concurrent.futures import ThreadPoolExecutor from concurrent import futures +from collections.abc import Generator import contextlib import datetime import functools @@ -28,6 +29,7 @@ from tornado.test.util import ( skipIfNonUnix, skipOnTravis, ) +from tornado.concurrent import Future import typing @@ -725,12 +727,11 @@ class TestPeriodicCallbackAsync(AsyncTestCase): pc.stop() self.assertEqual(count, 3) - def test_periodic_coro(self): + def test_periodic_coro(self) -> None: counts = [0, 0] - pc = None @gen.coroutine - def callback() -> None: + def callback() -> "Generator[Future[None], object, None]": counts[0] += 1 yield gen.sleep(0.025) counts[1] += 1 @@ -744,9 +745,8 @@ class TestPeriodicCallbackAsync(AsyncTestCase): self.assertEqual(counts[0], 3) self.assertEqual(counts[1], 3) - def test_periodic_async(self): + def test_periodic_async(self) -> None: counts = [0, 0] - pc = None async def callback() -> None: counts[0] += 1 diff --git a/tornado/test/iostream_test.py b/tornado/test/iostream_test.py index b098c9aa..e22e83e6 100644 --- a/tornado/test/iostream_test.py +++ b/tornado/test/iostream_test.py @@ -816,7 +816,10 @@ class TestIOStreamMixin(TestReadWriteMixin): # windows, making this check redundant with skipIfNonUnix, but # we sometimes enable it on other platforms for testing. io_loop = IOLoop.current() - if isinstance(io_loop.selector_loop, AddThreadSelectorEventLoop): + if isinstance( + io_loop.selector_loop, # type: ignore[attr-defined] + AddThreadSelectorEventLoop, + ): self.skipTest("AddThreadSelectorEventLoop not supported") server, client = yield self.make_iostream_pair() try: diff --git a/tornado/test/tcpserver_test.py b/tornado/test/tcpserver_test.py index 7c75acf6..fc9e98a2 100644 --- a/tornado/test/tcpserver_test.py +++ b/tornado/test/tcpserver_test.py @@ -4,7 +4,6 @@ import sys import textwrap import unittest -from tornado.escape import utf8, to_unicode from tornado import gen from tornado.iostream import IOStream from tornado.log import app_log @@ -122,20 +121,20 @@ class TestMultiprocess(unittest.TestCase): # processes, each of which prints its task id to stdout (a single # byte, so we don't have to worry about atomicity of the shared # stdout stream) and then exits. - def run_subproc(self, code): - proc = subprocess.Popen( - sys.executable, stdin=subprocess.PIPE, stdout=subprocess.PIPE - ) - proc.stdin.write(utf8(code)) - proc.stdin.close() - proc.wait() - stdout = proc.stdout.read() - proc.stdout.close() - if proc.returncode != 0: - raise RuntimeError( - "Process returned %d. stdout=%r" % (proc.returncode, stdout) + def run_subproc(self, code: str) -> str: + try: + result = subprocess.run( + sys.executable, + capture_output=True, + input=code, + encoding="utf8", + check=True, ) - return to_unicode(stdout) + except subprocess.CalledProcessError as e: + raise RuntimeError( + f"Process returned {e.returncode} stdout={e.stdout}" + ) from e + return result.stdout def test_single(self): # As a sanity check, run the single-process version through this test diff --git a/tornado/test/testing_test.py b/tornado/test/testing_test.py index 487622a7..d75079bd 100644 --- a/tornado/test/testing_test.py +++ b/tornado/test/testing_test.py @@ -179,7 +179,10 @@ class AsyncTestCaseWrapperTest(unittest.TestCase): pass test = Test("test_foo") - self.assertIs(inspect.unwrap(test.test_foo), test.test_foo.orig_method) + self.assertIs( + inspect.unwrap(test.test_foo), + test.test_foo.orig_method, # type: ignore[attr-defined] + ) class SetUpTearDownTest(unittest.TestCase): @@ -347,7 +350,7 @@ class GetNewIOLoopTest(AsyncTestCase): try: self.orig_loop = asyncio.get_event_loop() except RuntimeError: - self.orig_loop = None + self.orig_loop = None # type: ignore[assignment] self.new_loop = asyncio.new_event_loop() asyncio.set_event_loop(self.new_loop) super().setUp() diff --git a/tornado/testing.py b/tornado/testing.py index eb8140d4..1ed6e858 100644 --- a/tornado/testing.py +++ b/tornado/testing.py @@ -649,7 +649,7 @@ def gen_test( # noqa: F811 if inspect.iscoroutinefunction(f): coro = pre_coroutine else: - coro = gen.coroutine(pre_coroutine) + coro = gen.coroutine(pre_coroutine) # type: ignore[assignment] @functools.wraps(coro) def post_coroutine(self, *args, **kwargs): diff --git a/tornado/wsgi.py b/tornado/wsgi.py index 55ece9a2..cf4eba92 100644 --- a/tornado/wsgi.py +++ b/tornado/wsgi.py @@ -191,7 +191,14 @@ class WSGIContainer(object): request_time = 1000.0 * request.request_time() assert request.method is not None assert request.uri is not None - summary = request.method + " " + request.uri + " (" + request.remote_ip + ")" + summary = ( + request.method # type: ignore[operator] + + " " + + request.uri + + " (" + + request.remote_ip + + ")" + ) log_method("%d %s %.2fms", status_code, summary, request_time)