From: Ben Darnell Date: Thu, 19 Mar 2026 19:03:46 +0000 (-0400) Subject: *: Remove most typing.TYPE_CHECKING guards and F401 noqa comments X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=b4e805147af27354ff4f65ed910ce498ae2f1f20;p=thirdparty%2Ftornado.git *: Remove most typing.TYPE_CHECKING guards and F401 noqa comments Flake8 now understands type annotations and no longer emits "unused import" warnings for type imports. Most imports that were previously behind TYPE_CHECKING guards are no longer needed, or can be moved to unguarded imports. --- diff --git a/tornado/curl_httpclient.py b/tornado/curl_httpclient.py index fd20fc09..8d0b8814 100644 --- a/tornado/curl_httpclient.py +++ b/tornado/curl_httpclient.py @@ -41,10 +41,6 @@ from tornado.log import app_log from typing import Any from collections.abc import Callable -import typing - -if typing.TYPE_CHECKING: - from typing import Deque, Tuple # noqa: F401 curl_log = logging.getLogger("tornado.curl_httpclient") @@ -62,7 +58,7 @@ class CurlAsyncHTTPClient(AsyncHTTPClient): self._multi.setopt(pycurl.M_SOCKETFUNCTION, self._handle_socket) self._curls = [self._curl_create() for i in range(max_clients)] self._free_list = self._curls[:] - self._requests: Deque[ + self._requests: collections.deque[ tuple[HTTPRequest, Callable[[HTTPResponse], None], float] ] = collections.deque() self._fds: dict[int, int] = {} diff --git a/tornado/gen.py b/tornado/gen.py index efa03d2f..4e5e4cde 100644 --- a/tornado/gen.py +++ b/tornado/gen.py @@ -100,13 +100,9 @@ from typing import ( Dict, overload, ) -from collections.abc import Callable +from collections.abc import Callable, Iterable from collections.abc import Mapping, Awaitable, Sequence -if typing.TYPE_CHECKING: - from typing import Deque, Optional, Set # noqa: F401 - from collections.abc import Iterable - _T = typing.TypeVar("_T") _Yieldable = Union[ @@ -375,7 +371,7 @@ class WaitIterator: self._unfinished = {f: i for (i, f) in enumerate(args)} futures = args - self._finished: Deque[Future] = collections.deque() + self._finished: collections.deque[Future] = collections.deque() self.current_index: str | int | None = None self.current_future: Future | None = None self._running_future: Future | None = None diff --git a/tornado/httpserver.py b/tornado/httpserver.py index ea2e2ee6..3f94a43d 100644 --- a/tornado/httpserver.py +++ b/tornado/httpserver.py @@ -41,9 +41,6 @@ from typing import Any from collections.abc import Callable from collections.abc import Awaitable -if typing.TYPE_CHECKING: - from typing import Set # noqa: F401 - class HTTPServer(TCPServer, Configurable, httputil.HTTPServerConnectionDelegate): r"""A non-blocking, single-threaded HTTP server. diff --git a/tornado/httputil.py b/tornado/httputil.py index ce239adc..66fa156b 100644 --- a/tornado/httputil.py +++ b/tornado/httputil.py @@ -50,9 +50,10 @@ from typing import ( from collections.abc import Iterable, Mapping, Iterator, Awaitable, Generator if typing.TYPE_CHECKING: - from typing import Deque # noqa: F401 - from asyncio import Future # noqa: F401 - import unittest # noqa: F401 + # These are relatively heavy imports and aren't needed in this file + # unless we're type-checking. + from asyncio import Future + import unittest # To be used with str.strip() and related methods. HTTP_WHITESPACE = " \t" diff --git a/tornado/ioloop.py b/tornado/ioloop.py index 1e5b42aa..e7564de7 100644 --- a/tornado/ioloop.py +++ b/tornado/ioloop.py @@ -49,17 +49,10 @@ from tornado.log import app_log from tornado.util import Configurable, TimeoutError, import_object import typing -from typing import Any, TypeVar +from typing import Any, TypeVar, TypedDict, Protocol from collections.abc import Callable from collections.abc import Awaitable -if typing.TYPE_CHECKING: - from typing import Dict, List, Set, TypedDict # noqa: F401 - - from typing_extensions import Protocol -else: - Protocol = object - class _Selectable(Protocol): def fileno(self) -> int: @@ -493,12 +486,11 @@ class IOLoop(Configurable): .. versionchanged:: 6.2 ``tornado.util.TimeoutError`` is now an alias to ``asyncio.TimeoutError``. """ - if typing.TYPE_CHECKING: - class FutureCell(TypedDict): - # noqa: F841 - future: Future | None - timeout_called: bool + class FutureCell(TypedDict): + # noqa: F841 + future: Future | None + timeout_called: bool future_cell: FutureCell = {"future": None, "timeout_called": False} diff --git a/tornado/iostream.py b/tornado/iostream.py index d632fe1b..6585f88e 100644 --- a/tornado/iostream.py +++ b/tornado/iostream.py @@ -42,7 +42,6 @@ from tornado.util import errno_from_exception import typing from typing import ( - Optional, Any, TypeVar, ) @@ -51,9 +50,6 @@ from collections.abc import Awaitable from re import Pattern from types import TracebackType -if typing.TYPE_CHECKING: - from typing import Deque, List, Type # noqa: F401 - _IOStreamType = TypeVar("_IOStreamType", bound="IOStream") # These errnos indicate that a connection has been abruptly terminated. @@ -119,7 +115,9 @@ class _StreamBuffer: def __init__(self) -> None: # A sequence of (False, bytearray) and (True, memoryview) objects - self._buffers: Deque[tuple[bool, bytearray | memoryview]] = collections.deque() + self._buffers: collections.deque[tuple[bool, bytearray | memoryview]] = ( + collections.deque() + ) # Position in the first buffer self._first_pos = 0 self._size = 0 @@ -260,7 +258,9 @@ class BaseIOStream: self._read_partial = False self._read_until_close = False self._read_future: Future | None = None - self._write_futures: Deque[tuple[int, Future[None]]] = collections.deque() + self._write_futures: collections.deque[tuple[int, Future[None]]] = ( + collections.deque() + ) self._close_callback: Callable[[], None] | None = None self._connect_future: Future[IOStream] | None = None # _ssl_connect_future should be defined in SSLIOStream @@ -557,7 +557,7 @@ class BaseIOStream: | bool | BaseException | tuple[ - "Optional[Type[BaseException]]", + type[BaseException] | None, BaseException | None, TracebackType | None, ] diff --git a/tornado/locks.py b/tornado/locks.py index 9cc30276..fb42d73f 100644 --- a/tornado/locks.py +++ b/tornado/locks.py @@ -21,10 +21,6 @@ from tornado.concurrent import Future, future_set_result_unless_cancelled from typing import Optional, Type, Any from collections.abc import Awaitable -import typing - -if typing.TYPE_CHECKING: - from typing import Deque, Set # noqa: F401 __all__ = ["Condition", "Event", "Semaphore", "BoundedSemaphore", "Lock"] @@ -40,7 +36,7 @@ class _TimeoutGarbageCollector: """ def __init__(self) -> None: - self._waiters: Deque[Future] = collections.deque() + self._waiters: collections.deque[Future] = collections.deque() self._timeouts = 0 def _garbage_collect(self) -> None: diff --git a/tornado/platform/caresresolver.py b/tornado/platform/caresresolver.py index fb2658e2..1e71da94 100644 --- a/tornado/platform/caresresolver.py +++ b/tornado/platform/caresresolver.py @@ -7,10 +7,8 @@ from tornado.ioloop import IOLoop from tornado.netutil import Resolver, is_valid_ip import typing - -if typing.TYPE_CHECKING: - from typing import Any, List, Tuple, Dict # noqa: F401 - from collections.abc import Generator +from typing import Any +from collections.abc import Generator class CaresResolver(Resolver): @@ -63,7 +61,7 @@ class CaresResolver(Resolver): @gen.coroutine def resolve( self, host: str, port: int, family: int = 0 - ) -> "Generator[Any, Any, List[Tuple[int, Any]]]": + ) -> "Generator[Any, Any, list[tuple[int, Any]]]": if is_valid_ip(host): addresses = [host] else: diff --git a/tornado/platform/twisted.py b/tornado/platform/twisted.py index 2d28de07..70df475f 100644 --- a/tornado/platform/twisted.py +++ b/tornado/platform/twisted.py @@ -19,7 +19,7 @@ from twisted.python import failure # type: ignore from tornado.concurrent import Future, future_set_exc_info from tornado import gen -import typing # noqa: F401 +import typing def install() -> None: diff --git a/tornado/process.py b/tornado/process.py index df4eb3eb..db797e9e 100644 --- a/tornado/process.py +++ b/tornado/process.py @@ -36,13 +36,9 @@ from tornado import ioloop from tornado.iostream import PipeIOStream from tornado.log import gen_log -import typing from typing import Any from collections.abc import Callable -if typing.TYPE_CHECKING: - from typing import List # noqa: F401 - # Re-export this exception for convenience. CalledProcessError = subprocess.CalledProcessError diff --git a/tornado/queues.py b/tornado/queues.py index ddaa8dce..22373a55 100644 --- a/tornado/queues.py +++ b/tornado/queues.py @@ -35,12 +35,8 @@ from tornado import gen, ioloop from tornado.concurrent import Future, future_set_result_unless_cancelled from tornado.locks import Event -from typing import TypeVar, Generic +from typing import TypeVar, Generic, Any from collections.abc import Awaitable -import typing - -if typing.TYPE_CHECKING: - from typing import Deque, Tuple, Any # noqa: F401 _T = TypeVar("_T") @@ -160,8 +156,10 @@ class Queue(Generic[_T]): self._maxsize = maxsize self._init() - self._getters: Deque[Future[_T]] = collections.deque([]) - self._putters: Deque[tuple[_T, Future[None]]] = collections.deque([]) + self._getters: collections.deque[Future[_T]] = collections.deque([]) + self._putters: collections.deque[tuple[_T, Future[None]]] = collections.deque( + [] + ) self._unfinished_tasks = 0 self._finished = Event() self._finished.set() diff --git a/tornado/simple_httpclient.py b/tornado/simple_httpclient.py index d16b1dc0..7bcd6885 100644 --- a/tornado/simple_httpclient.py +++ b/tornado/simple_httpclient.py @@ -37,10 +37,6 @@ from typing import Any, Optional, Type from collections.abc import Callable from collections.abc import Awaitable from types import TracebackType -import typing - -if typing.TYPE_CHECKING: - from typing import Deque, Tuple, List # noqa: F401 class HTTPTimeoutError(HTTPError): @@ -129,7 +125,7 @@ class SimpleAsyncHTTPClient(AsyncHTTPClient): ) -> None: super().initialize(defaults=defaults) self.max_clients = max_clients - self.queue: Deque[ + self.queue: collections.deque[ tuple[object, HTTPRequest, Callable[[HTTPResponse], None]] ] = collections.deque() self.active: dict[ diff --git a/tornado/tcpclient.py b/tornado/tcpclient.py index 014fec68..590ef7f1 100644 --- a/tornado/tcpclient.py +++ b/tornado/tcpclient.py @@ -20,7 +20,6 @@ import socket import numbers import datetime import ssl -import typing from tornado.concurrent import Future, future_add_done_callback from tornado.ioloop import IOLoop @@ -33,9 +32,6 @@ from typing import Any, Tuple from collections.abc import Callable from collections.abc import Iterator -if typing.TYPE_CHECKING: - from typing import Set # noqa(F401) - _INITIAL_CONNECT_TIMEOUT = 0.3 diff --git a/tornado/tcpserver.py b/tornado/tcpserver.py index 25fab8ba..7081cd75 100644 --- a/tornado/tcpserver.py +++ b/tornado/tcpserver.py @@ -33,13 +33,8 @@ from tornado.netutil import ( from tornado import process from tornado.util import errno_from_exception -import typing from typing import Any -from collections.abc import Iterable, Awaitable - -if typing.TYPE_CHECKING: - from typing import List # noqa: F401 - from collections.abc import Callable +from collections.abc import Iterable, Awaitable, Callable class TCPServer: diff --git a/tornado/template.py b/tornado/template.py index 30952c77..dfcff330 100644 --- a/tornado/template.py +++ b/tornado/template.py @@ -208,14 +208,11 @@ from tornado import escape from tornado.log import app_log from tornado.util import ObjectDict, exec_in, unicode_type -from typing import Any, Optional, TextIO +from typing import Any, Optional, TextIO, ContextManager from collections.abc import Callable from collections.abc import Iterable import typing -if typing.TYPE_CHECKING: - from typing import Tuple, ContextManager # noqa: F401 - _DEFAULT_AUTOESCAPE = "xhtml_escape" diff --git a/tornado/test/escape_test.py b/tornado/test/escape_test.py index 7331e64f..c3f3638b 100644 --- a/tornado/test/escape_test.py +++ b/tornado/test/escape_test.py @@ -15,7 +15,7 @@ from tornado.escape import ( ) from tornado.util import unicode_type -from typing import List, Tuple, Union, Dict, Any # noqa: F401 +from typing import Any linkify_tests: list[tuple[str | bytes, dict[str, Any], str]] = [ # (input, linkify_kwargs, expected_output) diff --git a/tornado/test/gen_test.py b/tornado/test/gen_test.py index 514dcd0d..66043e74 100644 --- a/tornado/test/gen_test.py +++ b/tornado/test/gen_test.py @@ -21,11 +21,6 @@ try: except ImportError: contextvars = None # type: ignore -import typing - -if typing.TYPE_CHECKING: - from typing import List, Optional # noqa: F401 - class GenBasicTest(AsyncTestCase): @gen.coroutine diff --git a/tornado/test/http1connection_test.py b/tornado/test/http1connection_test.py index d7acfb58..abb5e6cf 100644 --- a/tornado/test/http1connection_test.py +++ b/tornado/test/http1connection_test.py @@ -1,5 +1,4 @@ import socket -import typing # noqa(F401) from tornado.http1connection import HTTP1Connection from tornado.httputil import HTTPMessageDelegate diff --git a/tornado/test/httpclient_test.py b/tornado/test/httpclient_test.py index e19db0a1..82110fc7 100644 --- a/tornado/test/httpclient_test.py +++ b/tornado/test/httpclient_test.py @@ -9,7 +9,6 @@ from io import BytesIO import subprocess import sys import time -import typing # noqa: F401 import unicodedata import unittest diff --git a/tornado/test/httpserver_test.py b/tornado/test/httpserver_test.py index 99777099..6f519303 100644 --- a/tornado/test/httpserver_test.py +++ b/tornado/test/httpserver_test.py @@ -48,9 +48,6 @@ from io import BytesIO import typing -if typing.TYPE_CHECKING: - from typing import Dict, List # noqa: F401 - async def read_stream_body(stream): """Reads an HTTP response from `stream` and returns a tuple of its diff --git a/tornado/test/ioloop_test.py b/tornado/test/ioloop_test.py index 98c432ab..fc7c2855 100644 --- a/tornado/test/ioloop_test.py +++ b/tornado/test/ioloop_test.py @@ -31,11 +31,6 @@ from tornado.test.util import ( ) from tornado.concurrent import Future -import typing - -if typing.TYPE_CHECKING: - from typing import List # noqa: F401 - class TestIOLoop(AsyncTestCase): def test_add_callback_return_sequence(self): diff --git a/tornado/test/locks_test.py b/tornado/test/locks_test.py index b1f2a8e8..394600ba 100644 --- a/tornado/test/locks_test.py +++ b/tornado/test/locks_test.py @@ -12,7 +12,6 @@ import asyncio from datetime import timedelta -import typing # noqa: F401 import unittest from tornado import gen, locks diff --git a/tornado/test/options_test.py b/tornado/test/options_test.py index 998ef6ec..3e4d1b33 100644 --- a/tornado/test/options_test.py +++ b/tornado/test/options_test.py @@ -8,11 +8,6 @@ import unittest from tornado.options import OptionParser, Error from tornado.util import basestring_type -import typing - -if typing.TYPE_CHECKING: - from typing import List # noqa: F401 - class Email: def __init__(self, value): diff --git a/tornado/test/routing_test.py b/tornado/test/routing_test.py index bf18eac4..efb9d4b3 100644 --- a/tornado/test/routing_test.py +++ b/tornado/test/routing_test.py @@ -28,7 +28,7 @@ from tornado.testing import AsyncHTTPTestCase from tornado.web import Application, HTTPError, RequestHandler from tornado.wsgi import WSGIContainer -import typing # noqa: F401 +import typing class BasicRouter(Router): diff --git a/tornado/test/simple_httpclient_test.py b/tornado/test/simple_httpclient_test.py index 227d4c70..e897f79c 100644 --- a/tornado/test/simple_httpclient_test.py +++ b/tornado/test/simple_httpclient_test.py @@ -7,7 +7,7 @@ import re import socket import ssl import sys -import typing # noqa: F401 +import typing from tornado.escape import to_unicode, utf8 from tornado import gen, version diff --git a/tornado/test/tcpclient_test.py b/tornado/test/tcpclient_test.py index 1730a55f..f90b885d 100644 --- a/tornado/test/tcpclient_test.py +++ b/tornado/test/tcpclient_test.py @@ -18,6 +18,7 @@ import socket import unittest from tornado.concurrent import Future +from tornado.iostream import IOStream from tornado.netutil import bind_sockets, Resolver from tornado.queues import Queue from tornado.tcpclient import TCPClient, _Connector @@ -28,10 +29,6 @@ from tornado.gen import TimeoutError import typing -if typing.TYPE_CHECKING: - from tornado.iostream import IOStream # noqa: F401 - from typing import List, Dict, Tuple # noqa: F401 - # Fake address families for testing. Used in place of AF_INET # and AF_INET6 because some installations do not have AF_INET6. AF1, AF2 = 1, 2 diff --git a/tornado/test/template_test.py b/tornado/test/template_test.py index d58d2088..d6f3d024 100644 --- a/tornado/test/template_test.py +++ b/tornado/test/template_test.py @@ -6,8 +6,6 @@ from tornado.escape import utf8, native_str, to_unicode from tornado.template import Template, DictLoader, ParseError, Loader from tornado.util import ObjectDict -import typing # noqa: F401 - class TemplateTest(unittest.TestCase): def test_simple(self): diff --git a/tornado/testing.py b/tornado/testing.py index 3cdbb9a5..4de6626d 100644 --- a/tornado/testing.py +++ b/tornado/testing.py @@ -39,10 +39,9 @@ from collections.abc import Callable from collections.abc import Coroutine from types import TracebackType -if typing.TYPE_CHECKING: - _ExcInfoTuple = tuple[ - Optional[type[BaseException]], Optional[BaseException], Optional[TracebackType] - ] +_ExcInfoTuple = tuple[ + type[BaseException] | None, BaseException | None, TracebackType | None +] _NON_OWNED_IOLOOPS = AsyncIOMainLoop diff --git a/tornado/util.py b/tornado/util.py index 24da9509..9b670cfd 100644 --- a/tornado/util.py +++ b/tornado/util.py @@ -30,10 +30,9 @@ from re import Match if typing.TYPE_CHECKING: # Additional imports only used in type comments. # This lets us make these imports lazy. - import datetime # noqa: F401 - from types import TracebackType # noqa: F401 - from typing import Union # noqa: F401 - import unittest # noqa: F401 + import datetime + from types import TracebackType + import unittest # Aliases for types that are spelled differently in different Python # versions. bytes_type is deprecated and no longer used in Tornado diff --git a/tornado/web.py b/tornado/web.py index 1f756eef..63765eed 100644 --- a/tornado/web.py +++ b/tornado/web.py @@ -118,11 +118,6 @@ from typing import ( from collections.abc import Callable from collections.abc import Awaitable, Iterable, Generator from types import TracebackType -import typing - -if typing.TYPE_CHECKING: - from typing import Set # noqa: F401 - # The following types are accepted by RequestHandler.set_header # and related methods. diff --git a/tornado/websocket.py b/tornado/websocket.py index 5755c938..2578c92e 100644 --- a/tornado/websocket.py +++ b/tornado/websocket.py @@ -38,62 +38,62 @@ from tornado.tcpclient import TCPClient from tornado.util import _websocket_mask from typing import ( - TYPE_CHECKING, cast, Any, Optional, Union, Type, + Protocol, ) from collections.abc import Callable from collections.abc import Awaitable from types import TracebackType -if TYPE_CHECKING: - from typing_extensions import Protocol - - # The zlib compressor types aren't actually exposed anywhere - # publicly, so declare protocols for the portions we use. - class _Compressor(Protocol): - def compress(self, data: bytes) -> bytes: - pass - - def flush(self, mode: int) -> bytes: - pass - - class _Decompressor(Protocol): - @property - def unconsumed_tail(self) -> bytes: - pass - - def decompress(self, data: bytes, max_length: int) -> bytes: - pass - - class _WebSocketDelegate(Protocol): - # The common base interface implemented by WebSocketHandler on - # the server side and WebSocketClientConnection on the client - # side. - def on_ws_connection_close( - self, close_code: int | None = None, close_reason: str | None = None - ) -> None: - pass - - def on_message(self, message: str | bytes) -> Optional["Awaitable[None]"]: - pass - - def on_ping(self, data: bytes) -> None: - pass - - def on_pong(self, data: bytes) -> None: - pass - - def log_exception( - self, - typ: type[BaseException] | None, - value: BaseException | None, - tb: TracebackType | None, - ) -> None: - pass + +# The zlib compressor types aren't actually exposed anywhere +# publicly, so declare protocols for the portions we use. +class _Compressor(Protocol): + def compress(self, data: bytes) -> bytes: + pass + + def flush(self, mode: int) -> bytes: + pass + + +class _Decompressor(Protocol): + @property + def unconsumed_tail(self) -> bytes: + pass + + def decompress(self, data: bytes, max_length: int) -> bytes: + pass + + +class _WebSocketDelegate(Protocol): + # The common base interface implemented by WebSocketHandler on + # the server side and WebSocketClientConnection on the client + # side. + def on_ws_connection_close( + self, close_code: int | None = None, close_reason: str | None = None + ) -> None: + pass + + def on_message(self, message: str | bytes) -> Optional["Awaitable[None]"]: + pass + + def on_ping(self, data: bytes) -> None: + pass + + def on_pong(self, data: bytes) -> None: + pass + + def log_exception( + self, + typ: type[BaseException] | None, + value: BaseException | None, + tb: TracebackType | None, + ) -> None: + pass _default_max_message_size = 10 * 1024 * 1024 diff --git a/tornado/wsgi.py b/tornado/wsgi.py index e94cfc8c..692c69e4 100644 --- a/tornado/wsgi.py +++ b/tornado/wsgi.py @@ -38,14 +38,13 @@ from tornado import httputil from tornado.ioloop import IOLoop from tornado.log import access_log -from typing import Optional, Any +from typing import Any from collections.abc import Callable from types import TracebackType import typing if typing.TYPE_CHECKING: - from typing import Type # noqa: F401 - from _typeshed.wsgi import WSGIApplication as WSGIAppType # noqa: F401 + from _typeshed.wsgi import WSGIApplication as WSGIAppType # PEP 3333 specifies that WSGI on python 3 generally deals with byte strings @@ -143,7 +142,7 @@ class WSGIContainer: headers: list[tuple[str, str]], exc_info: None | ( tuple[ - "Optional[Type[BaseException]]", + type[BaseException] | None, BaseException | None, TracebackType | None, ]