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/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/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 07661cba..298814b6 100644 --- a/sygnal/gcmpushkin.py +++ b/sygnal/gcmpushkin.py @@ -35,8 +35,8 @@ from sygnal.helper.proxy.proxyagent_twisted import ProxyAgent from sygnal.utils import NotificationLoggerAdapter, json_decoder, twisted_sleep -from .exceptions import PushkinSetupException -from .notifications import ( +from sygnal.exceptions import PushkinSetupException +from sygnal.notifications import ( ConcurrencyLimitedPushkin, Device, Notification, @@ -109,7 +109,7 @@ class GcmPushkin(ConcurrencyLimitedPushkin): } | ConcurrencyLimitedPushkin.UNDERSTOOD_CONFIG_FIELDS def __init__(self, name: str, sygnal: "Sygnal", config: Dict[str, Any]) -> None: - 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/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 743a1fc7..4bbe2153 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( @@ -344,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 73605f3c..06f0a593 100644 --- a/sygnal/notifications.py +++ b/sygnal/notifications.py @@ -19,9 +19,8 @@ from opentracing import Span from prometheus_client import Counter -from typing_extensions import Type - -from .exceptions import ( +from typing_extensions import TYPE_CHECKING, Type +from sygnal.exceptions import ( InvalidNotificationException, NotificationDispatchException, PushkinSetupException, @@ -109,7 +108,6 @@ def __init__(self, notif): self.devices = [Device(d) for d in notif["devices"]] - class Pushkin(abc.ABC): def __init__(self, name: str, sygnal: "Sygnal", config: Dict[str, Any]): self.name = name @@ -184,7 +182,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/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/sygnal/webpushpushkin.py b/sygnal/webpushpushkin.py index 34676e6d..035daed8 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", @@ -77,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" } 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."""