From: Ben Darnell Date: Thu, 13 Jun 2024 17:58:33 +0000 (-0400) Subject: *: Use set literals and comprehensions where applicable X-Git-Tag: v6.5.0b1~47^2~7 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=50358ac952c34de3085d8721c2289491c44968d4;p=thirdparty%2Ftornado.git *: Use set literals and comprehensions where applicable These features have been available since Python 2.7. This change was made with a version of pyupgrade modified to perform only this change, then postprocessed with black. --- diff --git a/tornado/auth.py b/tornado/auth.py index 0a98beb2..e19912c9 100644 --- a/tornado/auth.py +++ b/tornado/auth.py @@ -185,7 +185,7 @@ class OpenIdMixin: ax_attrs = set(ax_attrs) required = [] # type: List[str] if "name" in ax_attrs: - ax_attrs -= set(["name", "firstname", "fullname", "lastname"]) + ax_attrs -= {"name", "firstname", "fullname", "lastname"} required += ["firstname", "fullname", "lastname"] args.update( { @@ -1047,9 +1047,7 @@ class FacebookGraphMixin(OAuth2Mixin): "client_secret": client_secret, } - fields = set( - ["id", "name", "first_name", "last_name", "locale", "picture", "link"] - ) + fields = {"id", "name", "first_name", "last_name", "locale", "picture", "link"} if extra_fields: fields.update(extra_fields) diff --git a/tornado/curl_httpclient.py b/tornado/curl_httpclient.py index 486ef642..dde70032 100644 --- a/tornado/curl_httpclient.py +++ b/tornado/curl_httpclient.py @@ -448,7 +448,7 @@ class CurlAsyncHTTPClient(AsyncHTTPClient): "PUT": pycurl.UPLOAD, "HEAD": pycurl.NOBODY, } - custom_methods = set(["DELETE", "OPTIONS", "PATCH"]) + custom_methods = {"DELETE", "OPTIONS", "PATCH"} for o in curl_options.values(): curl.setopt(o, False) if request.method in curl_options: diff --git a/tornado/options.py b/tornado/options.py index 46c740b5..fe3644ef 100644 --- a/tornado/options.py +++ b/tornado/options.py @@ -188,7 +188,7 @@ class OptionParser: .. versionadded:: 3.1 """ - return set(opt.group_name for opt in self._options.values()) + return {opt.group_name for opt in self._options.values()} def group_dict(self, group: str) -> Dict[str, Any]: """The names and values of options in a group. diff --git a/tornado/simple_httpclient.py b/tornado/simple_httpclient.py index 5b2d4dcd..79fe292d 100644 --- a/tornado/simple_httpclient.py +++ b/tornado/simple_httpclient.py @@ -250,9 +250,7 @@ class SimpleAsyncHTTPClient(AsyncHTTPClient): class _HTTPConnection(httputil.HTTPMessageDelegate): - _SUPPORTED_METHODS = set( - ["GET", "HEAD", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"] - ) + _SUPPORTED_METHODS = {"GET", "HEAD", "POST", "PUT", "DELETE", "PATCH", "OPTIONS"} def __init__( self, diff --git a/tornado/template.py b/tornado/template.py index e8be9064..921545cc 100644 --- a/tornado/template.py +++ b/tornado/template.py @@ -936,10 +936,10 @@ def _parse( # Intermediate ("else", "elif", etc) blocks intermediate_blocks = { - "else": set(["if", "for", "while", "try"]), - "elif": set(["if"]), - "except": set(["try"]), - "finally": set(["try"]), + "else": {"if", "for", "while", "try"}, + "elif": {"if"}, + "except": {"try"}, + "finally": {"try"}, } allowed_parents = intermediate_blocks.get(operator) if allowed_parents is not None: @@ -1038,7 +1038,7 @@ def _parse( elif operator in ("break", "continue"): if not in_loop: reader.raise_parse_error( - "%s outside %s block" % (operator, set(["for", "while"])) + "%s outside %s block" % (operator, {"for", "while"}) ) body.chunks.append(_Statement(contents, line)) continue diff --git a/tornado/test/options_test.py b/tornado/test/options_test.py index 3ad1a9d9..99c3c12c 100644 --- a/tornado/test/options_test.py +++ b/tornado/test/options_test.py @@ -135,7 +135,7 @@ class OptionsTest(unittest.TestCase): def test_iter(self): options = self._sample_options() # OptionParsers always define 'help'. - self.assertEqual(set(["a", "b", "help"]), set(iter(options))) + self.assertEqual({"a", "b", "help"}, set(iter(options))) def test_getitem(self): options = self._sample_options() @@ -166,7 +166,7 @@ class OptionsTest(unittest.TestCase): frame = sys._getframe(0) this_file = frame.f_code.co_filename - self.assertEqual(set(["b_group", "", this_file]), options.groups()) + self.assertEqual({"b_group", "", this_file}, options.groups()) b_group_dict = options.group_dict("b_group") self.assertEqual({"b": 2}, b_group_dict) diff --git a/tornado/test/runtests.py b/tornado/test/runtests.py index f35b3725..ef6a503b 100644 --- a/tornado/test/runtests.py +++ b/tornado/test/runtests.py @@ -68,7 +68,7 @@ def test_runner_factory(stderr): def run(self, test): result = super().run(test) if result.skipped: - skip_reasons = set(reason for (test, reason) in result.skipped) + skip_reasons = {reason for (test, reason) in result.skipped} self.stream.write( # type: ignore textwrap.fill( "Some tests were skipped because: %s" diff --git a/tornado/test/simple_httpclient_test.py b/tornado/test/simple_httpclient_test.py index 7ee52455..bcb1aaba 100644 --- a/tornado/test/simple_httpclient_test.py +++ b/tornado/test/simple_httpclient_test.py @@ -215,14 +215,14 @@ class SimpleHTTPClientTestMixin: self.triggers.popleft()() self.triggers.popleft()() self.wait(condition=lambda: (len(self.triggers) == 2 and len(seen) == 2)) - self.assertEqual(set(seen), set([0, 1])) + self.assertEqual(set(seen), {0, 1}) self.assertEqual(len(client.queue), 0) # Finish all the pending requests self.triggers.popleft()() self.triggers.popleft()() self.wait(condition=lambda: len(seen) == 4) - self.assertEqual(set(seen), set([0, 1, 2, 3])) + self.assertEqual(set(seen), {0, 1, 2, 3}) self.assertEqual(len(self.triggers), 0) @gen_test diff --git a/tornado/test/tcpclient_test.py b/tornado/test/tcpclient_test.py index 2973d248..1636a649 100644 --- a/tornado/test/tcpclient_test.py +++ b/tornado/test/tcpclient_test.py @@ -83,7 +83,7 @@ class TCPClientTest(AsyncTestCase): # The port used here doesn't matter, but some systems require it # to be non-zero if we do not also pass AI_PASSIVE. addrinfo = self.io_loop.run_sync(lambda: Resolver().resolve("localhost", 80)) - families = set(addr[0] for addr in addrinfo) + families = {addr[0] for addr in addrinfo} if socket.AF_INET6 not in families: self.skipTest("localhost does not resolve to ipv6") diff --git a/tornado/test/web_test.py b/tornado/test/web_test.py index c65e6054..ac55aa39 100644 --- a/tornado/test/web_test.py +++ b/tornado/test/web_test.py @@ -1151,7 +1151,7 @@ class StaticFileTest(WebTestCase): # make sure the uncompressed file still has the correct type response = self.fetch("/static/sample.xml") self.assertIn( - response.headers.get("Content-Type"), set(("text/xml", "application/xml")) + response.headers.get("Content-Type"), {"text/xml", "application/xml"} ) def test_static_url(self): @@ -2937,7 +2937,7 @@ class XSRFTest(SimpleHandlerTestCase): def test_refresh_token(self): token = self.xsrf_token - tokens_seen = set([token]) + tokens_seen = {token} # A user's token is stable over time. Refreshing the page in one tab # might update the cookie while an older tab still has the old cookie # in its DOM. Simulate this scenario by passing a constant token diff --git a/tornado/test/websocket_test.py b/tornado/test/websocket_test.py index e7228ee8..d759442a 100644 --- a/tornado/test/websocket_test.py +++ b/tornado/test/websocket_test.py @@ -570,7 +570,7 @@ class WebSocketTest(WebSocketBaseTestCase): # server is only running on ipv4. Test for this edge case and skip # the test if it happens. addrinfo = yield Resolver().resolve("localhost", port) - families = set(addr[0] for addr in addrinfo) + families = {addr[0] for addr in addrinfo} if socket.AF_INET not in families: self.skipTest("localhost does not resolve to ipv4") return diff --git a/tornado/web.py b/tornado/web.py index 648bdc38..4bcfb45d 100644 --- a/tornado/web.py +++ b/tornado/web.py @@ -3224,17 +3224,15 @@ class GZipContentEncoding(OutputTransform): # Whitelist of compressible mime types (in addition to any types # beginning with "text/"). - CONTENT_TYPES = set( - [ - "application/javascript", - "application/x-javascript", - "application/xml", - "application/atom+xml", - "application/json", - "application/xhtml+xml", - "image/svg+xml", - ] - ) + CONTENT_TYPES = { + "application/javascript", + "application/x-javascript", + "application/xml", + "application/atom+xml", + "application/json", + "application/xhtml+xml", + "image/svg+xml", + } # Python's GzipFile defaults to level 9, while most other gzip # tools (including gzip itself) default to 6, which is probably a # better CPU/size tradeoff. diff --git a/tornado/websocket.py b/tornado/websocket.py index 7694c41b..581a0f40 100644 --- a/tornado/websocket.py +++ b/tornado/websocket.py @@ -998,14 +998,12 @@ class WebSocketProtocol13(WebSocketProtocol): compression_options: Optional[Dict[str, Any]] = None, ) -> None: # TODO: handle invalid parameters gracefully - allowed_keys = set( - [ - "server_no_context_takeover", - "client_no_context_takeover", - "server_max_window_bits", - "client_max_window_bits", - ] - ) + allowed_keys = { + "server_no_context_takeover", + "client_no_context_takeover", + "server_max_window_bits", + "client_max_window_bits", + } for key in agreed_parameters: if key not in allowed_keys: raise ValueError("unsupported compression parameter %r" % key) diff --git a/tornado/wsgi.py b/tornado/wsgi.py index 8a08d56e..6ef2ed65 100644 --- a/tornado/wsgi.py +++ b/tornado/wsgi.py @@ -185,7 +185,7 @@ class WSGIContainer: status_code_str, reason = data["status"].split(" ", 1) status_code = int(status_code_str) headers = data["headers"] # type: List[Tuple[str, str]] - header_set = set(k.lower() for (k, v) in headers) + header_set = {k.lower() for (k, v) in headers} body = escape.utf8(body) if status_code != 304: if "content-length" not in header_set: