From a10f63adea34f9a4a1e3a4f00ac03932108d1034 Mon Sep 17 00:00:00 2001 From: Sean Quah <8349537+squahtx@users.noreply.github.com> Date: Fri, 5 Nov 2021 13:26:23 +0000 Subject: [PATCH 1/3] Use absolute imports for consistency (#265) --- changelog.d/265.misc | 1 + sygnal/gcmpushkin.py | 5 ++--- sygnal/http.py | 9 +++++---- sygnal/notifications.py | 7 +++++-- sygnal/webpushpushkin.py | 7 +++---- 5 files changed, 16 insertions(+), 13 deletions(-) create mode 100644 changelog.d/265.misc diff --git a/changelog.d/265.misc b/changelog.d/265.misc new file mode 100644 index 00000000..af8d3f62 --- /dev/null +++ b/changelog.d/265.misc @@ -0,0 +1 @@ +Use absolute imports for consistency. diff --git a/sygnal/gcmpushkin.py b/sygnal/gcmpushkin.py index 457ea249..13f58715 100644 --- a/sygnal/gcmpushkin.py +++ b/sygnal/gcmpushkin.py @@ -27,15 +27,14 @@ from sygnal.exceptions import ( NotificationDispatchException, + PushkinSetupException, TemporaryNotificationDispatchException, ) from sygnal.helper.context_factory import ClientTLSOptionsFactory from sygnal.helper.proxy.proxyagent_twisted import ProxyAgent +from sygnal.notifications import ConcurrencyLimitedPushkin from sygnal.utils import NotificationLoggerAdapter, json_decoder, twisted_sleep -from .exceptions import PushkinSetupException -from .notifications import ConcurrencyLimitedPushkin - QUEUE_TIME_HISTOGRAM = Histogram( "sygnal_gcm_queue_time", "Time taken waiting for a connection to GCM" ) diff --git a/sygnal/http.py b/sygnal/http.py index 743a1fc7..68dd8eb7 100644 --- a/sygnal/http.py +++ b/sygnal/http.py @@ -34,12 +34,13 @@ from twisted.web.resource import Resource from twisted.web.server import NOT_DONE_YET -from sygnal.notifications import NotificationContext +from sygnal.exceptions import ( + InvalidNotificationException, + NotificationDispatchException, +) +from sygnal.notifications import Notification, NotificationContext from sygnal.utils import NotificationLoggerAdapter, json_decoder -from .exceptions import InvalidNotificationException, NotificationDispatchException -from .notifications import Notification - logger = logging.getLogger(__name__) NOTIFS_RECEIVED_COUNTER = Counter( diff --git a/sygnal/notifications.py b/sygnal/notifications.py index 65769ef3..cb32a94b 100644 --- a/sygnal/notifications.py +++ b/sygnal/notifications.py @@ -19,10 +19,13 @@ from prometheus_client import Counter -from .exceptions import InvalidNotificationException, NotificationDispatchException +from sygnal.exceptions import ( + InvalidNotificationException, + NotificationDispatchException, +) if typing.TYPE_CHECKING: - from .sygnal import Sygnal + from sygnal.sygnal import Sygnal class Tweaks: diff --git a/sygnal/webpushpushkin.py b/sygnal/webpushpushkin.py index 34676e6d..7886cced 100644 --- a/sygnal/webpushpushkin.py +++ b/sygnal/webpushpushkin.py @@ -28,12 +28,11 @@ from twisted.web.client import FileBodyProducer, HTTPConnectionPool, readBody from twisted.web.http_headers import Headers +from sygnal.exceptions import PushkinSetupException from sygnal.helper.context_factory import ClientTLSOptionsFactory from sygnal.helper.proxy.proxyagent_twisted import ProxyAgent - -from .exceptions import PushkinSetupException -from .notifications import ConcurrencyLimitedPushkin -from .utils import glob_to_regex +from sygnal.notifications import ConcurrencyLimitedPushkin +from sygnal.utils import glob_to_regex QUEUE_TIME_HISTOGRAM = Histogram( "sygnal_webpush_queue_time", From 6e91581920b81ae9b8741473dba362bd3c4e9121 Mon Sep 17 00:00:00 2001 From: Sean Quah <8349537+squahtx@users.noreply.github.com> Date: Fri, 5 Nov 2021 13:26:32 +0000 Subject: [PATCH 2/3] Remove explicit inheritance from `object`, left over from Python 2 (#266) --- changelog.d/266.misc | 1 + sygnal/helper/context_factory.py | 6 +++--- sygnal/helper/proxy/connectproxyclient_twisted.py | 2 +- sygnal/http.py | 2 +- sygnal/notifications.py | 4 ++-- sygnal/sygnal.py | 2 +- tests/testutils.py | 6 +++--- tests/twisted_test_helpers.py | 4 ++-- 8 files changed, 14 insertions(+), 13 deletions(-) create mode 100644 changelog.d/266.misc diff --git a/changelog.d/266.misc b/changelog.d/266.misc new file mode 100644 index 00000000..09309765 --- /dev/null +++ b/changelog.d/266.misc @@ -0,0 +1 @@ +Remove explicit inheritance from `object` that was left over from Python 2. diff --git a/sygnal/helper/context_factory.py b/sygnal/helper/context_factory.py index 7949d83a..9de4a114 100644 --- a/sygnal/helper/context_factory.py +++ b/sygnal/helper/context_factory.py @@ -34,7 +34,7 @@ @implementer(IPolicyForHTTPS) -class ClientTLSOptionsFactory(object): +class ClientTLSOptionsFactory: """Factory for Twisted SSLClientConnectionCreators that are used to make connections to remote servers for federation. Uses one of two OpenSSL context objects for all connections, depending on whether @@ -91,7 +91,7 @@ def creatorForNetloc(self, hostname, port): @implementer(IOpenSSLClientConnectionCreator) -class SSLClientConnectionCreator(object): +class SSLClientConnectionCreator: """Creates openssl connection objects for client connections. Replaces twisted.internet.ssl.ClientTLSOptions @@ -116,7 +116,7 @@ def clientConnectionForTLS(self, tls_protocol): return connection -class ConnectionVerifier(object): +class ConnectionVerifier: """Set the SNI, and do cert verification This is a thing which is attached to the TLSMemoryBIOProtocol, and is called by diff --git a/sygnal/helper/proxy/connectproxyclient_twisted.py b/sygnal/helper/proxy/connectproxyclient_twisted.py index 5a4b386b..dfe80f99 100644 --- a/sygnal/helper/proxy/connectproxyclient_twisted.py +++ b/sygnal/helper/proxy/connectproxyclient_twisted.py @@ -34,7 +34,7 @@ @implementer(IStreamClientEndpoint) -class HTTPConnectProxyEndpoint(object): +class HTTPConnectProxyEndpoint: """An Endpoint implementation which will send a CONNECT request to an http proxy Wraps an existing HostnameEndpoint for the proxy. diff --git a/sygnal/http.py b/sygnal/http.py index 68dd8eb7..4bbe2153 100644 --- a/sygnal/http.py +++ b/sygnal/http.py @@ -345,7 +345,7 @@ def log(self, request): self.logger.info("Handled request: %s", line) -class PushGatewayApiServer(object): +class PushGatewayApiServer: def __init__(self, sygnal): """ Initialises the /_matrix/push/* (Push Gateway API) server. diff --git a/sygnal/notifications.py b/sygnal/notifications.py index cb32a94b..bf86dcbe 100644 --- a/sygnal/notifications.py +++ b/sygnal/notifications.py @@ -97,7 +97,7 @@ def __init__(self, notif): self.devices = [Device(d) for d in notif["devices"]] -class Pushkin(object): +class Pushkin: def __init__(self, name: str, sygnal: "Sygnal", config: Dict[str, Any]): self.name = name self.cfg = config @@ -191,7 +191,7 @@ async def _dispatch_notification_unlimited( raise NotImplementedError -class NotificationContext(object): +class NotificationContext: def __init__(self, request_id, opentracing_span, start_time): """ Args: diff --git a/sygnal/sygnal.py b/sygnal/sygnal.py index 05b11d07..718f85aa 100644 --- a/sygnal/sygnal.py +++ b/sygnal/sygnal.py @@ -52,7 +52,7 @@ } -class Sygnal(object): +class Sygnal: def __init__(self, config, custom_reactor, tracer=opentracing.tracer): """ Object that holds state for the entirety of a Sygnal instance. diff --git a/tests/testutils.py b/tests/testutils.py index 668eb2b3..16f0f534 100644 --- a/tests/testutils.py +++ b/tests/testutils.py @@ -220,7 +220,7 @@ def __init__(self): self.lookups: Dict[str, str] = {} @implementer(IResolverSimple) - class FakeResolver(object): + class FakeResolver: @staticmethod def getHostByName(name, timeout=None): if name not in self.lookups: @@ -269,7 +269,7 @@ def wait_for_work(self, early_stop=lambda: False): self.work_notifier.release() -class DummyResponse(object): +class DummyResponse: def __init__(self, code): self.code = code @@ -292,7 +292,7 @@ class HTTPResult: @attr.s -class FakeChannel(object): +class FakeChannel: """ A fake Twisted Web Channel (the part that interfaces with the wire). diff --git a/tests/twisted_test_helpers.py b/tests/twisted_test_helpers.py index 687341a6..37f1e0b9 100644 --- a/tests/twisted_test_helpers.py +++ b/tests/twisted_test_helpers.py @@ -15,7 +15,7 @@ @attr.s(cmp=False) -class FakeTransport(object): +class FakeTransport: """ A twisted.internet.interfaces.ITransport implementation which sends all its data straight into an IProtocol object: it exists to connect two IProtocols together. @@ -274,7 +274,7 @@ def create_test_cert_file(sanlist): @implementer(IOpenSSLServerConnectionCreator) -class TestServerTLSConnectionFactory(object): +class TestServerTLSConnectionFactory: """An SSL connection creator which returns connections which present a certificate signed by our test CA.""" From 59df2b7715a0beea90bfc560053b2e448fb30d00 Mon Sep 17 00:00:00 2001 From: Sean Quah <8349537+squahtx@users.noreply.github.com> Date: Fri, 5 Nov 2021 13:26:43 +0000 Subject: [PATCH 3/3] Use Python 3-style super calls (#267) --- changelog.d/267.misc | 1 + sygnal/gcmpushkin.py | 2 +- sygnal/notifications.py | 2 +- sygnal/webpushpushkin.py | 2 +- tests/test_apns.py | 4 ++-- tests/test_concurrency_limit.py | 2 +- tests/test_http.py | 4 ++-- tests/test_httpproxy_twisted.py | 2 +- tests/test_pushgateway_api_v1.py | 2 +- 9 files changed, 11 insertions(+), 10 deletions(-) create mode 100644 changelog.d/267.misc diff --git a/changelog.d/267.misc b/changelog.d/267.misc new file mode 100644 index 00000000..fb871ed6 --- /dev/null +++ b/changelog.d/267.misc @@ -0,0 +1 @@ +Use Python 3-style super calls. diff --git a/sygnal/gcmpushkin.py b/sygnal/gcmpushkin.py index 13f58715..412b4c37 100644 --- a/sygnal/gcmpushkin.py +++ b/sygnal/gcmpushkin.py @@ -98,7 +98,7 @@ class GcmPushkin(ConcurrencyLimitedPushkin): } | ConcurrencyLimitedPushkin.UNDERSTOOD_CONFIG_FIELDS def __init__(self, name, sygnal, config): - super(GcmPushkin, self).__init__(name, sygnal, config) + super().__init__(name, sygnal, config) nonunderstood = set(self.cfg.keys()).difference(self.UNDERSTOOD_CONFIG_FIELDS) if len(nonunderstood) > 0: diff --git a/sygnal/notifications.py b/sygnal/notifications.py index bf86dcbe..f05a6efc 100644 --- a/sygnal/notifications.py +++ b/sygnal/notifications.py @@ -155,7 +155,7 @@ class ConcurrencyLimitedPushkin(Pushkin): ) def __init__(self, name: str, sygnal: "Sygnal", config: Dict[str, Any]): - super(ConcurrencyLimitedPushkin, self).__init__(name, sygnal, config) + super().__init__(name, sygnal, config) self._concurrent_limit = config.get( "inflight_request_limit", ConcurrencyLimitedPushkin.DEFAULT_CONCURRENCY_LIMIT, diff --git a/sygnal/webpushpushkin.py b/sygnal/webpushpushkin.py index 7886cced..035daed8 100644 --- a/sygnal/webpushpushkin.py +++ b/sygnal/webpushpushkin.py @@ -76,7 +76,7 @@ class WebpushPushkin(ConcurrencyLimitedPushkin): } | ConcurrencyLimitedPushkin.UNDERSTOOD_CONFIG_FIELDS def __init__(self, name, sygnal, config): - super(WebpushPushkin, self).__init__(name, sygnal, config) + super().__init__(name, sygnal, config) nonunderstood = self.cfg.keys() - self.UNDERSTOOD_CONFIG_FIELDS if nonunderstood: diff --git a/tests/test_apns.py b/tests/test_apns.py index d94b6bf0..8413922a 100644 --- a/tests/test_apns.py +++ b/tests/test_apns.py @@ -52,13 +52,13 @@ def setUp(self): patch("sygnal.apnspushkin.ApnsPushkin._report_certificate_expiration").start() self.addCleanup(patch.stopall) - super(ApnsTestCase, self).setUp() + super().setUp() self.apns_pushkin_snotif = MagicMock() self.sygnal.pushkins[PUSHKIN_ID]._send_notification = self.apns_pushkin_snotif def config_setup(self, config): - super(ApnsTestCase, self).config_setup(config) + super().config_setup(config) config["apps"][PUSHKIN_ID] = {"type": "apns", "certfile": TEST_CERTFILE_PATH} def test_payload_truncation(self): diff --git a/tests/test_concurrency_limit.py b/tests/test_concurrency_limit.py index 335822fc..eefcf0e8 100644 --- a/tests/test_concurrency_limit.py +++ b/tests/test_concurrency_limit.py @@ -47,7 +47,7 @@ async def _dispatch_notification_unlimited(self, n, device, context): class ConcurrencyLimitTestCase(TestCase): def config_setup(self, config): - super(ConcurrencyLimitTestCase, self).config_setup(config) + super().config_setup(config) config["apps"]["com.example.gcm"] = { "type": "tests.test_concurrency_limit.SlowConcurrencyLimitedDummyPushkin", "inflight_request_limit": 1, diff --git a/tests/test_http.py b/tests/test_http.py index 41d7210e..63dc5c0a 100644 --- a/tests/test_http.py +++ b/tests/test_http.py @@ -58,14 +58,14 @@ def setUp(self): patch("sygnal.apnspushkin.ApnsPushkin._report_certificate_expiration").start() self.addCleanup(patch.stopall) - super(HttpTestCase, self).setUp() + super().setUp() self.apns_pushkin_snotif = MagicMock() for key, value in self.sygnal.pushkins.items(): value._send_notification = self.apns_pushkin_snotif def config_setup(self, config): - super(HttpTestCase, self).config_setup(config) + super().config_setup(config) config["apps"][PUSHKIN_ID_1] = {"type": "apns", "certfile": TEST_CERTFILE_PATH} config["apps"][PUSHKIN_ID_2] = {"type": "apns", "certfile": TEST_CERTFILE_PATH} config["apps"][PUSHKIN_ID_3] = {"type": "apns", "certfile": TEST_CERTFILE_PATH} diff --git a/tests/test_httpproxy_twisted.py b/tests/test_httpproxy_twisted.py index a99e2d5f..08418a20 100644 --- a/tests/test_httpproxy_twisted.py +++ b/tests/test_httpproxy_twisted.py @@ -36,7 +36,7 @@ class SygnalTwistedProxyTests(TestCase): def config_setup(self, config): - super(SygnalTwistedProxyTests, self).config_setup(config) + super().config_setup(config) config["apps"]["com.example.gcm"] = { "type": "tests.test_gcm.TestGcmPushkin", "api_key": "kii", diff --git a/tests/test_pushgateway_api_v1.py b/tests/test_pushgateway_api_v1.py index 7c81758a..dd672710 100644 --- a/tests/test_pushgateway_api_v1.py +++ b/tests/test_pushgateway_api_v1.py @@ -79,7 +79,7 @@ def config_setup(self, config): """ Set up a TestPushkin for the test. """ - super(PushGatewayApiV1TestCase, self).config_setup(config) + super().config_setup(config) config["apps"]["com.example.spqr"] = { "type": "tests.test_pushgateway_api_v1.TestPushkin" }