]> git.ipfire.org Git - thirdparty/tornado.git/commitdiff
*: Use set literals and comprehensions where applicable
authorBen Darnell <ben@bendarnell.com>
Thu, 13 Jun 2024 17:58:33 +0000 (13:58 -0400)
committerBen Darnell <ben@bendarnell.com>
Thu, 13 Jun 2024 17:58:33 +0000 (13:58 -0400)
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.

14 files changed:
tornado/auth.py
tornado/curl_httpclient.py
tornado/options.py
tornado/simple_httpclient.py
tornado/template.py
tornado/test/options_test.py
tornado/test/runtests.py
tornado/test/simple_httpclient_test.py
tornado/test/tcpclient_test.py
tornado/test/web_test.py
tornado/test/websocket_test.py
tornado/web.py
tornado/websocket.py
tornado/wsgi.py

index 0a98beb27393bdd67625054161400bdce59dcc25..e19912c9c09f4a3a97640d7fb5e0bced70961878 100644 (file)
@@ -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)
 
index 486ef6425f8dbd9aed0bdea2f80e0363826850ab..dde700326668d98a3fbe87ca42766f5a4d12a5ad 100644 (file)
@@ -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:
index 46c740b584574bb9bd1a8caa207b3f85eaa59b42..fe3644ef3e35eb13137c5147896d81726530caeb 100644 (file)
@@ -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.
index 5b2d4dcd98008c1b0ace732744a4442734af7455..79fe292de75d3919428458ea17bc95436e60e1f1 100644 (file)
@@ -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,
index e8be9064f304093473c9cc28fd75f4ab569fdb91..921545ccf0e5c884cb337ded878d5dcac2b95a40 100644 (file)
@@ -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
index 3ad1a9d9e898ba241b4f59c4dc314e679b76fe94..99c3c12c80bb2cc65fe72d2ad947af789b2f318d 100644 (file)
@@ -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)
index f35b3725453eb5fa81f7752d1aed90857293fcd8..ef6a503b424d8e1cd4b948be9e62942619578f16 100644 (file)
@@ -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"
index 7ee52455336bc8265b547e790d0d805970f9c13e..bcb1aaba26964eecc7615088085d94c7abcd0250 100644 (file)
@@ -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
index 2973d248d887a43c12bfe9f56c9e014148076bd9..1636a649b82b4e211eae8688d67c2752bf628b43 100644 (file)
@@ -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")
 
index c65e605410e681c3b058a9c0b261de9209829a3a..ac55aa39a18abe3a32956957c72c939542ed28cd 100644 (file)
@@ -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
index e7228ee8268abb90a3040c9db50a83caf6fe3485..d759442a2f263ff8b62949c419a6c7dfab401f8e 100644 (file)
@@ -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
index 648bdc38507b1c28117a072d3cd5f961a0f9f19a..4bcfb45d2d7cac25916f7549ea9cb993dabdfcb5 100644 (file)
@@ -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.
index 7694c41b237655d5535f3b9eb8f29c488f5af061..581a0f403ed67209c57b166f6cc7914054443b3d 100644 (file)
@@ -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)
index 8a08d56e747514082f8477beecc18306c02b2fd2..6ef2ed656d448b7f24ac6d554d6112b04ebeb8ce 100644 (file)
@@ -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: