]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
*: Add mypy tox config and get it passing
authorBen Darnell <ben@bendarnell.com>
Sat, 21 Jul 2018 17:05:53 +0000 (13:05 -0400)
committerBen Darnell <ben@bendarnell.com>
Sat, 21 Jul 2018 23:09:05 +0000 (19:09 -0400)
Touch up the old annotations; it's simpler now that we're 3.5+. But
most of the additions here are type:ignore comments.

16 files changed:
.travis.yml
tornado/autoreload.py
tornado/ioloop.py
tornado/iostream.py
tornado/log.py
tornado/platform/asyncio.py
tornado/platform/windows.py
tornado/routing.py
tornado/test/httpclient_test.py
tornado/test/routing_test.py
tornado/test/twisted_test.py
tornado/test/util.py
tornado/test/web_test.py
tornado/test/websocket_test.py
tornado/util.py
tox.ini

index af41517c6e9057227aeef657aea801ae57dd973f..71ef19382683aae19b791364d39e19c02fe5afa2 100644 (file)
@@ -48,7 +48,7 @@ install:
     # version supported. But Python 3.7 requires slower-to-start VMs,
     # so we run it on 3.6 to minimize total CI run time.
     - if [[ $TRAVIS_PYTHON_VERSION == '3.6' ]]; then travis_retry pip install sphinx sphinx_rtd_theme; fi
-    - if [[ $TRAVIS_PYTHON_VERSION == '3.6' ]]; then travis_retry pip install flake8; fi
+    - if [[ $TRAVIS_PYTHON_VERSION == '3.6' ]]; then travis_retry pip install flake8 mypy; fi
     # On travis the extension should always be built
     - if [[ $TRAVIS_PYTHON_VERSION != 'pypy'* ]]; then export TORNADO_EXTENSION=1; fi
     - travis_retry python setup.py install
@@ -91,9 +91,10 @@ script:
     # make coverage reports for Codecov to find
     - if [[ "$RUN_COVERAGE" == 1 ]]; then coverage xml; fi
     - export TORNADO_EXTENSION=0
-    - if [[ $TRAVIS_PYTHON_VERSION == 3.6 ]]; then cd ../docs && mkdir sphinx-out && sphinx-build -E -n -W -b html . sphinx-out; fi
-    - if [[ $TRAVIS_PYTHON_VERSION == 3.6 ]]; then cd ../docs && mkdir sphinx-doctest-out && sphinx-build -E -n -b doctest . sphinx-out; fi
-    - if [[ $TRAVIS_PYTHON_VERSION == '3.6' ]]; then cd .. && flake8; fi
+    - if [[ $TRAVIS_PYTHON_VERSION == 3.6 ]]; then (cd ../docs && mkdir sphinx-out && sphinx-build -E -n -W -b html . sphinx-out); fi
+    - if [[ $TRAVIS_PYTHON_VERSION == 3.6 ]]; then (cd ../docs && mkdir sphinx-doctest-out && sphinx-build -E -n -b doctest . sphinx-out); fi
+    - if [[ $TRAVIS_PYTHON_VERSION == '3.6' ]]; then (cd .. && flake8); fi
+    - if [[ $TRAVIS_PYTHON_VERSION == '3.6' ]]; then (cd .. && mypy tornado); fi
 
 after_success:
     # call codecov from project root
index 01951fccf265227ff5a8c08b7bde58d51a2d1b6a..c973fdd65ddc3faf690274fb848fe664e204614a 100644 (file)
@@ -93,7 +93,7 @@ from tornado.util import exec_in
 try:
     import signal
 except ImportError:
-    signal = None
+    signal = None  # type: ignore
 
 # os.execv is broken on Windows and can't properly parse command line
 # arguments and executable name if they contain whitespaces. subprocess
index 51dbe45401f09dc4765a0e7eafd449b000e86e54..51b5dd1e4c1472c4dba62567299ef796337f6bf6 100644 (file)
@@ -45,6 +45,8 @@ from tornado.concurrent import Future, is_future, chain_future, future_set_exc_i
 from tornado.log import app_log
 from tornado.util import Configurable, TimeoutError, unicode_type, import_object
 
+import typing  # noqa
+
 
 class IOLoop(Configurable):
     """A level-triggered I/O loop.
@@ -135,7 +137,7 @@ class IOLoop(Configurable):
     ERROR = 0x018
 
     # In Python 3, _ioloop_for_asyncio maps from asyncio loops to IOLoops.
-    _ioloop_for_asyncio = dict()
+    _ioloop_for_asyncio = dict()  # type: typing.Dict[typing.Any, typing.Any]
 
     @classmethod
     def configure(cls, impl, **kwargs):
index ba8284f3f147685e9ce3233191ff5b6fa4afa210..514c36f503cedaa37d6c7b9040a6ce1f5eaa015a 100644 (file)
@@ -42,7 +42,7 @@ from tornado.util import errno_from_exception
 try:
     from tornado.platform.posix import _set_nonblocking
 except ImportError:
-    _set_nonblocking = None
+    _set_nonblocking = None  # type: ignore
 
 # These errnos indicate that a non-blocking operation must be retried
 # at a later time.  On most platforms they're the same value, but on
index c0171ba166d3509ee55fca1e8fe9a25eec99cde3..46fcfde1342290c4285b9049fe66171c5f56e6f0 100644 (file)
@@ -35,14 +35,14 @@ from tornado.escape import _unicode
 from tornado.util import unicode_type, basestring_type
 
 try:
-    import colorama
+    import colorama  # type: ignore
 except ImportError:
     colorama = None
 
 try:
     import curses  # type: ignore
 except ImportError:
-    curses = None
+    curses = None  # type: ignore
 
 # Logger objects for internal tornado use
 access_log = logging.getLogger("tornado.access")
index b139c90029bc0b96e51e33a09b1a26d40407f077..9211e214611bd4fe6328a0fc21faadf85bdc82f3 100644 (file)
@@ -268,7 +268,7 @@ def to_asyncio_future(tornado_future):
     return convert_yielded(tornado_future)
 
 
-class AnyThreadEventLoopPolicy(asyncio.DefaultEventLoopPolicy):
+class AnyThreadEventLoopPolicy(asyncio.DefaultEventLoopPolicy):  # type: ignore
     """Event loop policy that allows loop creation on any thread.
 
     The default `asyncio` event loop policy only automatically creates
index 274937f86715f15cce1d71af19407b5123665e5f..86293a908a9e4fdc6d45709dcd879152fb1190a9 100644 (file)
@@ -1,11 +1,11 @@
 # NOTE: win32 support is currently experimental, and not recommended
 # for production use.
 
-import ctypes  # type: ignore
-import ctypes.wintypes  # type: ignore
+import ctypes
+import ctypes.wintypes
 
 # See: http://msdn.microsoft.com/en-us/library/ms724935(VS.85).aspx
-SetHandleInformation = ctypes.windll.kernel32.SetHandleInformation
+SetHandleInformation = ctypes.windll.kernel32.SetHandleInformation  # type: ignore
 SetHandleInformation.argtypes = (ctypes.wintypes.HANDLE, ctypes.wintypes.DWORD, ctypes.wintypes.DWORD)  # noqa: E501
 SetHandleInformation.restype = ctypes.wintypes.BOOL
 
index 8122faf5b9035ac223b4ba25b5999d2354a21b1f..2af2fcad53b7e1d926974c196263b1a40f255e9d 100644 (file)
@@ -373,7 +373,7 @@ class ReversibleRuleRouter(ReversibleRouter, RuleRouter):
     """
 
     def __init__(self, rules=None):
-        self.named_rules = {}  # type: typing.Dict[str]
+        self.named_rules = {}  # type: typing.Dict[str, Any]
         super(ReversibleRuleRouter, self).__init__(rules)
 
     def process_rule(self, rule):
index 578f1f39dab068b6c5fcaffda38d0ca1f1c82402..4b4e3b538361bad5a190818f87640c0f5da67d53 100644 (file)
@@ -103,7 +103,7 @@ class PatchHandler(RequestHandler):
 
 
 class AllMethodsHandler(RequestHandler):
-    SUPPORTED_METHODS = RequestHandler.SUPPORTED_METHODS + ('OTHER',)
+    SUPPORTED_METHODS = RequestHandler.SUPPORTED_METHODS + ('OTHER',)  # type: ignore
 
     def method(self):
         self.write(self.request.method)
index 722662555b2d29ee4b0d2fe2a1d6178b6adfeb06..158156082fb278d38537efdf70aa8a0cbe9ececd 100644 (file)
@@ -16,6 +16,8 @@ from tornado.testing import AsyncHTTPTestCase
 from tornado.web import Application, HTTPError, RequestHandler
 from tornado.wsgi import WSGIContainer
 
+import typing  # noqa
+
 
 class BasicRouter(Router):
     def find_handler(self, request, **kwargs):
@@ -44,7 +46,7 @@ class BasicRouterTestCase(AsyncHTTPTestCase):
         self.assertEqual(response.body, b"OK")
 
 
-resources = {}
+resources = {}  # type: typing.Dict[str, bytes]
 
 
 class GetResource(RequestHandler):
index 5d90272a890da9d56d7743880e1bc3e8849606d6..1d8d10ef24c7c5189310daebd29baa1937cdf58f 100644 (file)
@@ -33,7 +33,7 @@ from tornado.web import RequestHandler, Application
 try:
     from twisted.internet.defer import Deferred, inlineCallbacks, returnValue  # type: ignore
     from twisted.internet.protocol import Protocol  # type: ignore
-    from twisted.internet.asyncioreactor import AsyncioSelectorReactor
+    from twisted.internet.asyncioreactor import AsyncioSelectorReactor  # type: ignore
     from twisted.web.client import Agent, readBody  # type: ignore
     from twisted.web.resource import Resource  # type: ignore
     from twisted.web.server import Site  # type: ignore
index 60e787b7da62e90066fbb9ca42f6081b0e3f943e..81012c28fee73e13380ee1b11e2ff26a8136833d 100644 (file)
@@ -30,7 +30,7 @@ skipNotCPython = unittest.skipIf(platform.python_implementation() != 'CPython',
 # TODO: remove this after pypy3 5.8 is obsolete.
 skipPypy3V58 = unittest.skipIf(platform.python_implementation() == 'PyPy' and
                                sys.version_info > (3,) and
-                               sys.pypy_version_info < (5, 9),
+                               sys.pypy_version_info < (5, 9),  # type: ignore
                                'pypy3 5.8 has buggy ssl module')
 
 
index cb1ab353c23de11e31e3758975c501b5acc10914..6d79f0560650c58380fd1ec5623c06ed100eb383 100644 (file)
@@ -1938,7 +1938,7 @@ class AllHTTPMethodsTest(SimpleHandlerTestCase):
 
 class PatchMethodTest(SimpleHandlerTestCase):
     class Handler(RequestHandler):
-        SUPPORTED_METHODS = RequestHandler.SUPPORTED_METHODS + ('OTHER',)
+        SUPPORTED_METHODS = RequestHandler.SUPPORTED_METHODS + ('OTHER',)  # type: ignore
 
         def patch(self):
             self.write('patch')
index 23dc2805d864238d14eb7feaf354e48aa6d7a09e..ed025431744f0701a49113b70366182edd56ccf1 100644 (file)
@@ -30,7 +30,7 @@ from tornado.websocket import (
 try:
     from tornado import speedups
 except ImportError:
-    speedups = None
+    speedups = None  # type: ignore
 
 
 class TestWebSocketHandler(WebSocketHandler):
index e2802ac1d6289f6d22d4091594ab426e442f54c6..be86e013cc1c2a49ebbf2eaee71da6a1066ced71 100644 (file)
@@ -24,24 +24,15 @@ bytes_type = bytes
 unicode_type = str
 basestring_type = str
 
-try:
-    import typing  # noqa
-    from typing import cast
-
-    _ObjectDictBase = typing.Dict[str, typing.Any]
-except ImportError:
-    _ObjectDictBase = dict
+import typing
 
-    def cast(typ, x):
-        return x
-else:
-    # More imports that are only needed in type comments.
-    import datetime  # noqa
-    import types  # noqa
-    from typing import Any, AnyStr, Union, Optional, Dict, Mapping  # noqa
-    from typing import Tuple, Match, Callable  # noqa
+# More imports that are only needed in type annotations.
+import datetime  # noqa
+import types
+from typing import Any, AnyStr, Union, Optional, Dict, Mapping, List  # noqa
+from typing import Tuple, Match, Callable  # noqa
 
-    _BaseString = str
+_BaseString = str
 
 try:
     from sys import is_finalizing
@@ -70,7 +61,7 @@ class TimeoutError(Exception):
     """
 
 
-class ObjectDict(_ObjectDictBase):
+class ObjectDict(typing.Dict[str, typing.Any]):
     """Makes a dictionary behave like an object, with attribute-style access.
     """
     def __getattr__(self, name):
@@ -151,10 +142,10 @@ def import_object(name):
         # on python 2 a byte string is required.
         name = name.encode('utf-8')
     if name.count('.') == 0:
-        return __import__(name, None, None)
+        return __import__(name)
 
     parts = name.split('.')
-    obj = __import__('.'.join(parts[:-1]), None, None, [parts[-1]], 0)
+    obj = __import__('.'.join(parts[:-1]), fromlist=[parts[-1]])
     try:
         return getattr(obj, parts[-1])
     except AttributeError:
@@ -170,10 +161,10 @@ def exec_in(code, glob, loc=None):
     exec(code, glob, loc)
 
 
-def raise_exc_info(exc_info):
-    # type: (Tuple[type, BaseException, types.TracebackType]) -> None
+def raise_exc_info(exc_info: Optional[Tuple[type, BaseException, types.TracebackType]]) -> None:
     try:
-        raise exc_info[1].with_traceback(exc_info[2])
+        if exc_info is not None:
+            raise exc_info[1].with_traceback(exc_info[2])
     finally:
         exc_info = None
 
@@ -356,7 +347,7 @@ class ArgReplacer(object):
         # type: (Callable, str) -> None
         self.name = name
         try:
-            self.arg_pos = self._getargnames(func).index(name)
+            self.arg_pos = self._getargnames(func).index(name)  # type: Optional[int]
         except ValueError:
             # Not a positional parameter
             self.arg_pos = None
diff --git a/tox.ini b/tox.ini
index 84c631ce9a21818156efbd1e27e5c20acce7a25a..ee44788e97032af7a3df74dcb4dfee6759daa10d 100644 (file)
--- a/tox.ini
+++ b/tox.ini
@@ -38,6 +38,7 @@ envlist =
         py3-sphinx-doctest,
 
         py3-lint
+        py3-mypy
 
 [testenv]
 # Most of these are defaults, but if you specify any you can't fall back
@@ -125,3 +126,8 @@ commands =
 [testenv:py3-lint]
 commands = flake8 {posargs:}
 changedir = {toxinidir}
+
+[testenv:py3-mypy]
+commands = mypy {posargs:tornado}
+changedir = {toxinidir}
+deps = mypy