Skip to content

Commit b699824

Browse files
committed
Add type: ignore comments to make mypy happy
1 parent 9f72321 commit b699824

30 files changed

+153
-148
lines changed

tornado/auth.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,11 +92,6 @@ def get(self):
9292
import urlparse
9393
import urllib as urllib_parse
9494

95-
try:
96-
long # py2
97-
except NameError:
98-
long = int # py3
99-
10095

10196
class AuthError(Exception):
10297
pass

tornado/autoreload.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@
8383
import functools
8484
import logging
8585
import os
86-
import pkgutil
86+
import pkgutil # type: ignore
8787
import sys
8888
import traceback
8989
import types
@@ -112,7 +112,7 @@
112112
_watched_files = set()
113113
_reload_hooks = []
114114
_reload_attempted = False
115-
_io_loops = weakref.WeakKeyDictionary()
115+
_io_loops = weakref.WeakKeyDictionary() # type: ignore
116116

117117

118118
def start(io_loop=None, check_time=500):

tornado/curl_httpclient.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
import collections
2222
import functools
2323
import logging
24-
import pycurl
24+
import pycurl # type: ignore
2525
import threading
2626
import time
2727
from io import BytesIO

tornado/gen.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,8 @@ def get(self):
9292

9393
try:
9494
try:
95-
from functools import singledispatch # py34+
95+
# py34+
96+
from functools import singledispatch # type: ignore
9697
except ImportError:
9798
from singledispatch import singledispatch # backport
9899
except ImportError:
@@ -108,9 +109,10 @@ def get(self):
108109

109110
try:
110111
try:
111-
from collections.abc import Generator as GeneratorType # py35+
112+
# py35+
113+
from collections.abc import Generator as GeneratorType # type: ignore
112114
except ImportError:
113-
from backports_abc import Generator as GeneratorType
115+
from backports_abc import Generator as GeneratorType # type: ignore
114116

115117
try:
116118
from inspect import isawaitable # py35+
@@ -121,7 +123,7 @@ def get(self):
121123
raise
122124
from types import GeneratorType
123125

124-
def isawaitable(x):
126+
def isawaitable(x): # type: ignore
125127
return False
126128

127129
if PY3:

tornado/httpclient.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -572,16 +572,15 @@ def __init__(self, request, code, headers=None, buffer=None,
572572
self.request_time = request_time
573573
self.time_info = time_info or {}
574574

575-
def _get_body(self):
575+
@property
576+
def body(self):
576577
if self.buffer is None:
577578
return None
578579
elif self._body is None:
579580
self._body = self.buffer.getvalue()
580581

581582
return self._body
582583

583-
body = property(_get_body)
584-
585584
def rethrow(self):
586585
"""If there was an error on the request, raise an `HTTPError`."""
587586
if self.error:

tornado/httputil.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,11 @@
5353
from ssl import SSLError
5454
except ImportError:
5555
# ssl is unavailable on app engine.
56-
class SSLError(Exception):
56+
class _SSLError(Exception):
5757
pass
58-
58+
# Hack around a mypy limitation. We can't simply put "type: ignore"
59+
# on the class definition itself; must go through an assignment.
60+
SSLError = _SSLError # type: ignore
5961

6062
# RFC 7230 section 3.5: a recipient MAY recognize a single LF as a line
6163
# terminator and ignore any preceding CR.
@@ -738,7 +740,7 @@ def parse_multipart_form_data(boundary, data, arguments, files):
738740
name = disp_params["name"]
739741
if disp_params.get("filename"):
740742
ctype = headers.get("Content-Type", "application/unknown")
741-
files.setdefault(name, []).append(HTTPFile(
743+
files.setdefault(name, []).append(HTTPFile( # type: ignore
742744
filename=disp_params["filename"], body=value,
743745
content_type=ctype))
744746
else:

tornado/iostream.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,29 +58,29 @@
5858
_ERRNO_WOULDBLOCK = (errno.EWOULDBLOCK, errno.EAGAIN)
5959

6060
if hasattr(errno, "WSAEWOULDBLOCK"):
61-
_ERRNO_WOULDBLOCK += (errno.WSAEWOULDBLOCK,)
61+
_ERRNO_WOULDBLOCK += (errno.WSAEWOULDBLOCK,) # type: ignore
6262

6363
# These errnos indicate that a connection has been abruptly terminated.
6464
# They should be caught and handled less noisily than other errors.
6565
_ERRNO_CONNRESET = (errno.ECONNRESET, errno.ECONNABORTED, errno.EPIPE,
6666
errno.ETIMEDOUT)
6767

6868
if hasattr(errno, "WSAECONNRESET"):
69-
_ERRNO_CONNRESET += (errno.WSAECONNRESET, errno.WSAECONNABORTED, errno.WSAETIMEDOUT)
69+
_ERRNO_CONNRESET += (errno.WSAECONNRESET, errno.WSAECONNABORTED, errno.WSAETIMEDOUT) # type: ignore
7070

7171
if sys.platform == 'darwin':
7272
# OSX appears to have a race condition that causes send(2) to return
7373
# EPROTOTYPE if called while a socket is being torn down:
7474
# http://erickt.github.io/blog/2014/11/19/adventures-in-debugging-a-potential-osx-kernel-bug/
7575
# Since the socket is being closed anyway, treat this as an ECONNRESET
7676
# instead of an unexpected error.
77-
_ERRNO_CONNRESET += (errno.EPROTOTYPE,)
77+
_ERRNO_CONNRESET += (errno.EPROTOTYPE,) # type: ignore
7878

7979
# More non-portable errnos:
8080
_ERRNO_INPROGRESS = (errno.EINPROGRESS,)
8181

8282
if hasattr(errno, "WSAEINPROGRESS"):
83-
_ERRNO_INPROGRESS += (errno.WSAEINPROGRESS,)
83+
_ERRNO_INPROGRESS += (errno.WSAEINPROGRESS,) # type: ignore
8484

8585

8686
class StreamClosedError(IOError):

tornado/locale.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
from tornado._locale_data import LOCALE_NAMES
5656

5757
_default_locale = "en_US"
58-
_translations = {}
58+
_translations = {} # type: dict
5959
_supported_locales = frozenset([_default_locale])
6060
_use_gettext = False
6161
CONTEXT_SEPARATOR = "\x04"

tornado/netutil.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@
5151
ssl_match_hostname = ssl.match_hostname
5252
SSLCertificateError = ssl.CertificateError
5353
elif ssl is None:
54-
ssl_match_hostname = SSLCertificateError = None
54+
ssl_match_hostname = SSLCertificateError = None # type: ignore
5555
else:
5656
import backports.ssl_match_hostname
5757
ssl_match_hostname = backports.ssl_match_hostname.match_hostname
58-
SSLCertificateError = backports.ssl_match_hostname.CertificateError
58+
SSLCertificateError = backports.ssl_match_hostname.CertificateError # type: ignore
5959

6060
if hasattr(ssl, 'SSLContext'):
6161
if hasattr(ssl, 'create_default_context'):
@@ -102,7 +102,7 @@
102102
_ERRNO_WOULDBLOCK = (errno.EWOULDBLOCK, errno.EAGAIN)
103103

104104
if hasattr(errno, "WSAEWOULDBLOCK"):
105-
_ERRNO_WOULDBLOCK += (errno.WSAEWOULDBLOCK,)
105+
_ERRNO_WOULDBLOCK += (errno.WSAEWOULDBLOCK,) # type: ignore
106106

107107
# Default backlog used when calling sock.listen()
108108
_DEFAULT_BACKLOG = 128
@@ -411,8 +411,8 @@ class ThreadedResolver(ExecutorResolver):
411411
All ``ThreadedResolvers`` share a single thread pool, whose
412412
size is set by the first one to be created.
413413
"""
414-
_threadpool = None
415-
_threadpool_pid = None
414+
_threadpool = None # type: ignore
415+
_threadpool_pid = None # type: int
416416

417417
def initialize(self, io_loop=None, num_threads=10):
418418
threadpool = ThreadedResolver._create_threadpool(num_threads)
@@ -516,4 +516,4 @@ def ssl_wrap_socket(socket, ssl_options, server_hostname=None, **kwargs):
516516
else:
517517
return context.wrap_socket(socket, **kwargs)
518518
else:
519-
return ssl.wrap_socket(socket, **dict(context, **kwargs))
519+
return ssl.wrap_socket(socket, **dict(context, **kwargs)) # type: ignore

tornado/platform/asyncio.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
except ImportError as e:
3535
# Asyncio itself isn't available; see if trollius is (backport to py26+).
3636
try:
37-
import trollius as asyncio
37+
import trollius as asyncio # type: ignore
3838
except ImportError:
3939
# Re-raise the original asyncio error, not the trollius one.
4040
raise e
@@ -213,4 +213,4 @@ def to_asyncio_future(tornado_future):
213213
return af
214214

215215
if hasattr(convert_yielded, 'register'):
216-
convert_yielded.register(asyncio.Future, to_tornado_future)
216+
convert_yielded.register(asyncio.Future, to_tornado_future) # type: ignore

tornado/platform/caresresolver.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
from __future__ import absolute_import, division, print_function, with_statement
2-
import pycares
2+
import pycares # type: ignore
33
import socket
44

55
from tornado import gen

tornado/platform/interface.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,6 @@ def consume(self):
6161
def close(self):
6262
"""Closes the waker's file descriptor(s)."""
6363
raise NotImplementedError()
64+
65+
def monotonic_time():
66+
raise NotImplementedError()

tornado/platform/twisted.py

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,19 +29,18 @@
2929
import socket
3030
import sys
3131

32-
import twisted.internet.abstract
33-
from twisted.internet.defer import Deferred
34-
from twisted.internet.posixbase import PosixReactorBase
35-
from twisted.internet.interfaces import \
36-
IReactorFDSet, IDelayedCall, IReactorTime, IReadDescriptor, IWriteDescriptor
37-
from twisted.python import failure, log
38-
from twisted.internet import error
39-
import twisted.names.cache
40-
import twisted.names.client
41-
import twisted.names.hosts
42-
import twisted.names.resolve
43-
44-
from zope.interface import implementer
32+
import twisted.internet.abstract # type: ignore
33+
from twisted.internet.defer import Deferred # type: ignore
34+
from twisted.internet.posixbase import PosixReactorBase # type: ignore
35+
from twisted.internet.interfaces import IReactorFDSet, IDelayedCall, IReactorTime, IReadDescriptor, IWriteDescriptor # type: ignore
36+
from twisted.python import failure, log # type: ignore
37+
from twisted.internet import error # type: ignore
38+
import twisted.names.cache # type: ignore
39+
import twisted.names.client # type: ignore
40+
import twisted.names.hosts # type: ignore
41+
import twisted.names.resolve # type: ignore
42+
43+
from zope.interface import implementer # type: ignore
4544

4645
from tornado.concurrent import Future
4746
from tornado.escape import utf8
@@ -354,7 +353,7 @@ def install(io_loop=None):
354353
if not io_loop:
355354
io_loop = tornado.ioloop.IOLoop.current()
356355
reactor = TornadoReactor(io_loop)
357-
from twisted.internet.main import installReactor
356+
from twisted.internet.main import installReactor # type: ignore
358357
installReactor(reactor)
359358
return reactor
360359

@@ -412,7 +411,7 @@ class TwistedIOLoop(tornado.ioloop.IOLoop):
412411
def initialize(self, reactor=None, **kwargs):
413412
super(TwistedIOLoop, self).initialize(**kwargs)
414413
if reactor is None:
415-
import twisted.internet.reactor
414+
import twisted.internet.reactor # type: ignore
416415
reactor = twisted.internet.reactor
417416
self.reactor = reactor
418417
self.fds = {}
@@ -570,7 +569,7 @@ def resolve(self, host, port, family=0):
570569
raise gen.Return(result)
571570

572571
if hasattr(gen.convert_yielded, 'register'):
573-
@gen.convert_yielded.register(Deferred)
572+
@gen.convert_yielded.register(Deferred) # type: ignore
574573
def _(d):
575574
f = Future()
576575

tornado/platform/windows.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33

44

55
from __future__ import absolute_import, division, print_function, with_statement
6-
import ctypes
7-
import ctypes.wintypes
6+
import ctypes # type: ignore
7+
import ctypes.wintypes # type: ignore
88

99
# See: http://msdn.microsoft.com/en-us/library/ms724935(VS.85).aspx
1010
SetHandleInformation = ctypes.windll.kernel32.SetHandleInformation

tornado/process.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ class Subprocess(object):
207207
STREAM = object()
208208

209209
_initialized = False
210-
_waiting = {}
210+
_waiting = {} # type: ignore
211211

212212
def __init__(self, *args, **kwargs):
213213
self.io_loop = kwargs.pop('io_loop', None) or ioloop.IOLoop.current()

tornado/template.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -207,12 +207,12 @@ class (and specifically its ``render`` method) and will not work
207207

208208
from tornado import escape
209209
from tornado.log import app_log
210-
from tornado.util import ObjectDict, exec_in, unicode_type
210+
from tornado.util import ObjectDict, exec_in, unicode_type, PY3
211211

212-
try:
213-
from cStringIO import StringIO # py2
214-
except ImportError:
215-
from io import StringIO # py3
212+
if PY3:
213+
from io import StringIO
214+
else:
215+
from cStringIO import StringIO
216216

217217
_DEFAULT_AUTOESCAPE = "xhtml_escape"
218218
_UNSET = object()
@@ -335,7 +335,7 @@ def generate(self, **kwargs):
335335
# __name__ and __loader__ allow the traceback mechanism to find
336336
# the generated source code.
337337
"__name__": self.name.replace('.', '_'),
338-
"__loader__": ObjectDict(get_source=lambda name: self.code),
338+
"__loader__": ObjectDict(get_source=lambda name: self.code), # type: ignore
339339
}
340340
namespace.update(self.namespace)
341341
namespace.update(kwargs)

tornado/test/concurrent_test.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -275,15 +275,15 @@ def capitalize(self, request_data, callback):
275275

276276
class ClientTestMixin(object):
277277
def setUp(self):
278-
super(ClientTestMixin, self).setUp()
278+
super(ClientTestMixin, self).setUp() # type: ignore
279279
self.server = CapServer(io_loop=self.io_loop)
280280
sock, port = bind_unused_port()
281281
self.server.add_sockets([sock])
282282
self.client = self.client_class(io_loop=self.io_loop, port=port)
283283

284284
def tearDown(self):
285285
self.server.stop()
286-
super(ClientTestMixin, self).tearDown()
286+
super(ClientTestMixin, self).tearDown() # type: ignore
287287

288288
def test_callback(self):
289289
self.client.capitalize("hello", callback=self.stop)

tornado/test/curl_httpclient_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414

1515
try:
16-
import pycurl
16+
import pycurl # type: ignore
1717
except ImportError:
1818
pycurl = None
1919

tornado/test/httpserver_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def get_app(self):
8686

8787
class SSLTestMixin(object):
8888
def get_ssl_options(self):
89-
return dict(ssl_version=self.get_ssl_version(),
89+
return dict(ssl_version=self.get_ssl_version(), # type: ignore
9090
**AsyncHTTPSTestCase.get_ssl_options())
9191

9292
def get_ssl_version(self):

tornado/test/import_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def test_import_everything(self):
4040

4141
def test_import_pycurl(self):
4242
try:
43-
import pycurl
43+
import pycurl # type: ignore
4444
except ImportError:
4545
pass
4646
else:

tornado/test/iostream_test.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,10 @@
2020
import sys
2121

2222
try:
23-
from unittest import mock # python 3.3
23+
from unittest import mock # type: ignore
2424
except ImportError:
2525
try:
26-
import mock # third-party mock package
26+
import mock # type: ignore
2727
except ImportError:
2828
mock = None
2929

@@ -258,7 +258,7 @@ def test_gaierror(self):
258258
stream = IOStream(s, io_loop=self.io_loop)
259259
stream.set_close_callback(self.stop)
260260
with mock.patch('socket.socket.connect',
261-
side_effect=socket.gaierror('boom')):
261+
side_effect=socket.gaierror(errno.EIO, 'boom')):
262262
with ExpectLog(gen_log, "Connect error"):
263263
stream.connect(('localhost', 80), callback=self.stop)
264264
self.wait()

0 commit comments

Comments
 (0)