From: Shantanu <12621235+hauntsaninja@users.noreply.github.com> Date: Wed, 18 Mar 2026 19:36:28 +0000 (-0700) Subject: Use variable annotations to avoid None defaults (#3575) X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=f59bb3772f4c32541b6dfa6284f213aa8ed7fee2;p=thirdparty%2Ftornado.git Use variable annotations to avoid None defaults (#3575) PEP 526 landed in Python 3.5 which is quite dead now --- diff --git a/tornado/httpclient.py b/tornado/httpclient.py index 488fe6de..e3023bae 100644 --- a/tornado/httpclient.py +++ b/tornado/httpclient.py @@ -176,7 +176,7 @@ class AsyncHTTPClient(Configurable): """ - _instance_cache = None # type: Dict[IOLoop, AsyncHTTPClient] + _instance_cache: Dict[IOLoop, "AsyncHTTPClient"] @classmethod def configurable_base(cls) -> Type[Configurable]: @@ -339,7 +339,7 @@ class AsyncHTTPClient(Configurable): class HTTPRequest: """HTTP client request object.""" - _headers = None # type: Union[Dict[str, str], httputil.HTTPHeaders] + _headers: Union[Dict[str, str], httputil.HTTPHeaders] # Default values for HTTPRequest parameters. # Merged with the values on the request object by AsyncHTTPClient @@ -626,7 +626,7 @@ class HTTPResponse: # I'm not sure why these don't get type-inferred from the references in __init__. error = None # type: Optional[BaseException] _error_is_response_code = False - request = None # type: HTTPRequest + request: HTTPRequest def __init__( self, diff --git a/tornado/httputil.py b/tornado/httputil.py index 38d9f879..3ffba7ec 100644 --- a/tornado/httputil.py +++ b/tornado/httputil.py @@ -475,11 +475,11 @@ class HTTPServerRequest: temporarily restored in 6.5.2. """ - path = None # type: str - query = None # type: str + path: str + query: str # HACK: Used for stream_request_body - _body_future = None # type: Future[None] + _body_future: "Future[None]" def __init__( self, diff --git a/tornado/iostream.py b/tornado/iostream.py index dd2111e3..44e96cd5 100644 --- a/tornado/iostream.py +++ b/tornado/iostream.py @@ -1324,7 +1324,7 @@ class SSLIOStream(IOStream): wrapped when `IOStream.connect` is finished. """ - socket = None # type: ssl.SSLSocket + socket: ssl.SSLSocket def __init__(self, *args: Any, **kwargs: Any) -> None: """The ``ssl_options`` keyword argument may either be an diff --git a/tornado/netutil.py b/tornado/netutil.py index 3ec76af7..a426c346 100644 --- a/tornado/netutil.py +++ b/tornado/netutil.py @@ -515,8 +515,8 @@ class ThreadedResolver(ExecutorResolver): of this class. """ - _threadpool = None # type: ignore - _threadpool_pid = None # type: int + _threadpool: Optional[concurrent.futures.ThreadPoolExecutor] = None + _threadpool_pid: Optional[int] = None def initialize(self, num_threads: int = 10) -> None: # type: ignore threadpool = ThreadedResolver._create_threadpool(num_threads) diff --git a/tornado/test/web_test.py b/tornado/test/web_test.py index a7886795..f3d6013b 100644 --- a/tornado/test/web_test.py +++ b/tornado/test/web_test.py @@ -223,7 +223,7 @@ class SecureCookieV2Test(unittest.TestCase): class FinalReturnTest(WebTestCase): - final_return = None # type: Future + final_return: Future def get_handlers(self): test = self diff --git a/tornado/util.py b/tornado/util.py index 19df4913..ee721b2e 100644 --- a/tornado/util.py +++ b/tornado/util.py @@ -236,8 +236,8 @@ class Configurable: # There may be a clever way to use generics here to get more # precise types (i.e. for a particular Configurable subclass T, # all the types are subclasses of T, not just Configurable). - __impl_class = None # type: Optional[Type[Configurable]] - __impl_kwargs = None # type: Dict[str, Any] + __impl_class: Optional[Type["Configurable"]] = None + __impl_kwargs: Optional[Dict[str, Any]] = None def __new__(cls, *args: Any, **kwargs: Any) -> Any: base = cls.configurable_base() @@ -324,13 +324,13 @@ class Configurable: @classmethod def _save_configuration(cls): - # type: () -> Tuple[Optional[Type[Configurable]], Dict[str, Any]] + # type: () -> Tuple[Optional[Type[Configurable]], Optional[Dict[str, Any]]] base = cls.configurable_base() return (base.__impl_class, base.__impl_kwargs) @classmethod def _restore_configuration(cls, saved): - # type: (Tuple[Optional[Type[Configurable]], Dict[str, Any]]) -> None + # type: (Tuple[Optional[Type[Configurable]], Optional[Dict[str, Any]]]) -> None base = cls.configurable_base() base.__impl_class = saved[0] base.__impl_kwargs = saved[1] diff --git a/tornado/web.py b/tornado/web.py index c3efdbd5..48b3560e 100644 --- a/tornado/web.py +++ b/tornado/web.py @@ -205,9 +205,9 @@ class RequestHandler: _stream_request_body = False # Will be set in _execute. - _transforms = None # type: List[OutputTransform] - path_args = None # type: List[str] - path_kwargs = None # type: Dict[str, str] + _transforms: List["OutputTransform"] + path_args: List[str] + path_kwargs: Dict[str, str] def __init__( self, diff --git a/tornado/websocket.py b/tornado/websocket.py index ab9b76f5..4e5843cb 100644 --- a/tornado/websocket.py +++ b/tornado/websocket.py @@ -834,7 +834,7 @@ class WebSocketProtocol13(WebSocketProtocol): RSV_MASK = RSV1 | RSV2 | RSV3 OPCODE_MASK = 0x0F - stream = None # type: IOStream + stream: IOStream def __init__( self, @@ -1396,7 +1396,7 @@ class WebSocketClientConnection(simple_httpclient._HTTPConnection): `websocket_connect` function instead. """ - protocol = None # type: WebSocketProtocol + protocol: Optional[WebSocketProtocol] = None def __init__( self, @@ -1620,6 +1620,7 @@ class WebSocketClientConnection(simple_httpclient._HTTPConnection): .. versionadded:: 5.1 """ + assert self.protocol is not None return self.protocol.selected_subprotocol def log_exception(