From: Ben Darnell Date: Thu, 19 Mar 2026 19:50:32 +0000 (-0400) Subject: gen,test: Remove conditional imports of modules that are now standard X-Git-Url: http://git.ipfire.org/gitweb/index.cgi?a=commitdiff_plain;h=refs%2Fpull%2F3597%2Fhead;p=thirdparty%2Ftornado.git gen,test: Remove conditional imports of modules that are now standard --- diff --git a/tornado/gen.py b/tornado/gen.py index 4e5e4cde..d3b69441 100644 --- a/tornado/gen.py +++ b/tornado/gen.py @@ -66,6 +66,7 @@ import builtins import collections from collections.abc import Generator import concurrent.futures +import contextvars import datetime import functools from functools import singledispatch @@ -85,11 +86,6 @@ from tornado.ioloop import IOLoop from tornado.log import app_log from tornado.util import TimeoutError -try: - import contextvars -except ImportError: - contextvars = None # type: ignore - import typing from typing import ( Union, @@ -207,10 +203,7 @@ def coroutine( # This function is type-annotated with a comment to work around # https://bitbucket.org/pypy/pypy/issues/2868/segfault-with-args-type-annotation-in future = _create_future() - if contextvars is not None: - ctx_run: Callable = contextvars.copy_context().run - else: - ctx_run = _fake_ctx_run + ctx_run: Callable = contextvars.copy_context().run try: result = ctx_run(func, *args, **kwargs) except (Return, StopIteration) as e: diff --git a/tornado/test/auth_test.py b/tornado/test/auth_test.py index 834f04ea..f361d1b7 100644 --- a/tornado/test/auth_test.py +++ b/tornado/test/auth_test.py @@ -3,7 +3,7 @@ # and ensure that it doesn't blow up (e.g. with unicode/bytes issues in # python 3) -import unittest +from unittest import mock from tornado.auth import ( OpenIdMixin, @@ -21,11 +21,6 @@ from tornado.log import app_log from tornado.testing import AsyncHTTPTestCase, ExpectLog from tornado.web import RequestHandler, Application, HTTPError -try: - from unittest import mock -except ImportError: - mock = None # type: ignore - class OpenIdClientLoginHandler(RequestHandler, OpenIdMixin): def initialize(self, test): @@ -403,7 +398,6 @@ class AuthTest(AsyncHTTPTestCase): response.headers["Set-Cookie"], ) - @unittest.skipIf(mock is None, "mock package not present") def test_oauth10a_redirect_error(self): with mock.patch.object(OAuth1ServerRequestTokenHandler, "get") as get: get.side_effect = Exception("boom") diff --git a/tornado/test/gen_test.py b/tornado/test/gen_test.py index 66043e74..598b2389 100644 --- a/tornado/test/gen_test.py +++ b/tornado/test/gen_test.py @@ -1,5 +1,6 @@ import asyncio from concurrent import futures +import contextvars import gc import datetime import platform @@ -16,11 +17,6 @@ from tornado.web import Application, RequestHandler, HTTPError from tornado import gen -try: - import contextvars -except ImportError: - contextvars = None # type: ignore - class GenBasicTest(AsyncTestCase): @gen.coroutine @@ -1043,11 +1039,9 @@ class RunnerGCTest(AsyncTestCase): self.assertEqual(result, [None, None]) -if contextvars is not None: - ctx_var: contextvars.ContextVar[int] = contextvars.ContextVar("ctx_var") +ctx_var: contextvars.ContextVar[int] = contextvars.ContextVar("ctx_var") -@unittest.skipIf(contextvars is None, "contextvars module not present") class ContextVarsTest(AsyncTestCase): async def native_root(self, x): ctx_var.set(x)