From 80de57c2d1ef011674e514870180d3492ddf4820 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 3 Apr 2024 17:12:59 +0200 Subject: [PATCH] Micro optimizations --- channels/layers.py | 2 +- channels/security/websocket.py | 4 ++-- channels/testing/live.py | 1 + channels/worker.py | 6 +++--- tests/test_generic_websocket.py | 6 +++--- tests/test_routing.py | 10 +++++----- 6 files changed, 15 insertions(+), 14 deletions(-) diff --git a/channels/layers.py b/channels/layers.py index 12bbd2b8c..d18c7522b 100644 --- a/channels/layers.py +++ b/channels/layers.py @@ -166,7 +166,7 @@ def valid_group_name(self, name): raise TypeError(self.invalid_name_error.format("Group", name)) def valid_channel_names(self, names, receive=False): - _non_empty_list = True if names else False + _non_empty_list = bool(names) _names_type = isinstance(names, list) assert _non_empty_list and _names_type, "names must be a non-empty list" diff --git a/channels/security/websocket.py b/channels/security/websocket.py index eff745e86..00718ce87 100644 --- a/channels/security/websocket.py +++ b/channels/security/websocket.py @@ -123,10 +123,10 @@ def get_origin_port(self, origin): # Return origin.port return origin.port # if origin.port doesn`t exists - if origin.scheme == "http" or origin.scheme == "ws": + if origin.scheme in {"http", "ws"}: # Default port return for http, ws return 80 - elif origin.scheme == "https" or origin.scheme == "wss": + elif origin.scheme in {"https", "wss"}: # Default port return for https, wss return 443 else: diff --git a/channels/testing/live.py b/channels/testing/live.py index ce5ff1097..14887c7f9 100644 --- a/channels/testing/live.py +++ b/channels/testing/live.py @@ -74,3 +74,4 @@ def _is_in_memory_db(self, connection): """ if connection.vendor == "sqlite": return connection.is_in_memory_db() + return None diff --git a/channels/worker.py b/channels/worker.py index 7ea23a7ae..62802a224 100644 --- a/channels/worker.py +++ b/channels/worker.py @@ -21,9 +21,9 @@ async def handle(self): Listens on all the provided channels and handles the messages. """ # For each channel, launch its own listening coroutine - listeners = [] - for channel in self.channels: - listeners.append(asyncio.ensure_future(self.listener(channel))) + listeners = [ + asyncio.ensure_future(self.listener(channel)) for channel in self.channels + ] # Wait for them all to exit await asyncio.wait(listeners) # See if any of the listeners had an error (e.g. channel layer error) diff --git a/tests/test_generic_websocket.py b/tests/test_generic_websocket.py index 73cdb4862..346b2c94a 100644 --- a/tests/test_generic_websocket.py +++ b/tests/test_generic_websocket.py @@ -280,7 +280,7 @@ async def receive(self, text_data=None, bytes_data=None): # Test that the specific channel layer is retrieved assert channel_layer is not None - channel_name = list(channel_layer.channels.keys())[0] + channel_name = next(iter(channel_layer.channels.keys())) message = {"type": "websocket.receive", "text": "hello"} await channel_layer.send(channel_name, message) response = await communicator.receive_from() @@ -380,7 +380,7 @@ async def _my_private_handler(self, _): # Test that the specific channel layer is retrieved assert channel_layer is not None - channel_name = list(channel_layer.channels.keys())[0] + channel_name = next(iter(channel_layer.channels.keys())) # Should block call to private functions handler and raise ValueError message = {"type": "_my_private_handler", "text": "hello"} await channel_layer.send(channel_name, message) @@ -415,7 +415,7 @@ async def _my_private_handler(self, _): # Test that the specific channel layer is retrieved assert channel_layer is not None - channel_name = list(channel_layer.channels.keys())[0] + channel_name = next(iter(channel_layer.channels.keys())) # Should not replace dot by underscore and call private function (see # issue: #1430) message = {"type": ".my_private_handler", "text": "hello"} diff --git a/tests/test_routing.py b/tests/test_routing.py index 99c767909..b90bde027 100644 --- a/tests/test_routing.py +++ b/tests/test_routing.py @@ -93,18 +93,18 @@ async def test_url_router(): # Valid keyword argument matches assert await router({"type": "http", "path": "/kwarg/hello/"}, None, None) == 5 assert kwarg_app.call_args[0][0]["url_route"] == { - "args": tuple(), + "args": (), "kwargs": {"name": "hello"}, } assert await router({"type": "http", "path": "/kwarg/hellothere/"}, None, None) == 5 assert kwarg_app.call_args[0][0]["url_route"] == { - "args": tuple(), + "args": (), "kwargs": {"name": "hellothere"}, } # Valid default keyword arguments assert await router({"type": "http", "path": "/defaultkwargs/"}, None, None) == 6 assert defaultkwarg_app.call_args[0][0]["url_route"] == { - "args": tuple(), + "args": (), "kwargs": {"default": 42}, } # Valid root_path in scope @@ -246,13 +246,13 @@ async def test_url_router_path(): await router({"type": "http", "path": "/author/andrewgodwin/"}, None, None) == 3 ) assert kwarg_app.call_args[0][0]["url_route"] == { - "args": tuple(), + "args": (), "kwargs": {"name": "andrewgodwin"}, } # Named with typecasting assert await router({"type": "http", "path": "/year/2012/"}, None, None) == 3 assert kwarg_app.call_args[0][0]["url_route"] == { - "args": tuple(), + "args": (), "kwargs": {"year": 2012}, } # Invalid matches