Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it MyPy and isort-friendly #131

Merged
merged 12 commits into from
Jul 6, 2020
1 change: 1 addition & 0 deletions changelog.d/131.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed MyPy errors so it can be enabled in CI and gradually be increased in coverage.
32 changes: 32 additions & 0 deletions mypy.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[mypy]
check_untyped_defs = True
show_error_codes = True
show_traceback = True
mypy_path = stubs

[mypy-idna]
ignore_missing_imports = True

[mypy-OpenSSL]
ignore_missing_imports = True

[mypy-opentracing.*]
ignore_missing_imports = True

[mypy-twisted.*]
ignore_missing_imports = True

[mypy-jaeger_client]
ignore_missing_imports = True

[mypy-aioapns]
ignore_missing_imports = True

[mypy-service_identity.*]
ignore_missing_imports = True

[mypy-prometheus_client]
ignore_missing_imports = True

[mypy-importlib_metadata]
ignore_missing_imports = True
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ max-line-length = 88
ignore=W503,W504,E203

[isort]
line_length = 80
line_length = 88
not_skip = __init__.py
sections=FUTURE,STDLIB,THIRDPARTY,FIRSTPARTY,TESTS,LOCALFOLDER
default_section=THIRDPARTY
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import os

from setuptools import setup, find_packages
from setuptools import find_packages, setup


# Utility function to read the README file.
Expand Down
2 changes: 1 addition & 1 deletion sygnal/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.

from importlib_metadata import version, PackageNotFoundError
from importlib_metadata import PackageNotFoundError, version

try:
__version__ = version(__name__)
Expand Down
11 changes: 6 additions & 5 deletions sygnal/apnspushkin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,27 +17,28 @@
import asyncio
import base64
import copy
from datetime import timezone
import logging
import os
from datetime import timezone
from typing import Dict
from uuid import uuid4

import aioapns
from aioapns import APNs, NotificationRequest
from cryptography.hazmat.backends import default_backend
from cryptography.x509 import load_pem_x509_certificate
from opentracing import logs, tags
from prometheus_client import Histogram, Counter, Gauge
from prometheus_client import Counter, Gauge, Histogram
from twisted.internet.defer import Deferred

from sygnal import apnstruncate
from sygnal.exceptions import (
NotificationDispatchException,
PushkinSetupException,
TemporaryNotificationDispatchException,
NotificationDispatchException,
)
from sygnal.notifications import Pushkin
from sygnal.utils import twisted_sleep, NotificationLoggerAdapter
from sygnal.utils import NotificationLoggerAdapter, twisted_sleep

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -220,7 +221,7 @@ async def dispatch_notification(self, n, device, context):
# The pushkey is kind of secret because you can use it to send push
# to someone.
# span_tags = {"pushkey": device.pushkey}
span_tags = {}
span_tags: Dict[str, int] = {}

with self.sygnal.tracer.start_span(
"apns_dispatch", tags=span_tags, child_of=context.opentracing_span
Expand Down
3 changes: 2 additions & 1 deletion sygnal/apnstruncate.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
# Copied and adapted from
# https://raw.githubusercontent.com/matrix-org/pushbaby/master/pushbaby/truncate.py
import json
from typing import List, Tuple, Union


def json_encode(payload):
Expand Down Expand Up @@ -85,7 +86,7 @@ def truncate(payload, max_length=2048):


def _choppables_for_aps(aps):
ret = []
ret: List[Union[Tuple[str], Tuple[str, int]]] = []
clokep marked this conversation as resolved.
Show resolved Hide resolved
if "alert" not in aps:
return ret

Expand Down
4 changes: 3 additions & 1 deletion sygnal/gcmpushkin.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

from opentracing import logs, tags
from prometheus_client import Counter, Gauge, Histogram
from twisted.enterprise.adbapi import ConnectionPool
from twisted.internet.defer import DeferredSemaphore
from twisted.web.client import Agent, FileBodyProducer, HTTPConnectionPool, readBody
from twisted.web.http_headers import Headers
Expand Down Expand Up @@ -447,7 +448,8 @@ class CanonicalRegIdStore(object):
"""

def __init__(self):
self.db = None
self.db: ConnectionPool = None
self.engine = None

async def setup(self, db, engine):
"""
Expand Down
2 changes: 1 addition & 1 deletion sygnal/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
# limitations under the License.
import json
import logging
import re
import sys
import time
import traceback
import re
from uuid import uuid4

from opentracing import Format, logs, tags
Expand Down
31 changes: 13 additions & 18 deletions sygnal/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import Any, Dict, Optional

from .exceptions import InvalidNotificationException

Expand Down Expand Up @@ -63,24 +64,18 @@ def __init__(self, raw):

class Notification:
def __init__(self, notif):
optional_attrs = [
"room_name",
"room_alias",
"prio",
"membership",
"sender_display_name",
"content",
"event_id",
"room_id",
"user_is_target",
"type",
"sender",
]
for a in optional_attrs:
if a in notif:
self.__dict__[a] = notif[a]
else:
self.__dict__[a] = None
# optional attributes
self.room_name: Optional[str] = notif.get("room_name")
self.room_alias: Optional[str] = notif.get("room_alias")
self.prio: Optional[str] = notif.get("prio")
self.membership: Optional[str] = notif.get("membership")
self.sender_display_name: Optional[str] = notif.get("sender_display_name")
self.content: Optional[Dict[str, Any]] = notif.get("content")
self.event_id: Optional[str] = notif.get("event_id")
self.room_id: Optional[str] = notif.get("room_id")
self.user_is_target: Optional[bool] = notif.get("user_is_target")
self.type: Optional[str] = notif.get("type")
self.sender: Optional[str] = notif.get("sender")

if "devices" not in notif or not isinstance(notif["devices"], list):
raise InvalidNotificationException("Expected list in 'devices' key")
Expand Down
2 changes: 0 additions & 2 deletions sygnal/sygnal.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@

import opentracing
import prometheus_client

# import twisted.internet.reactor
import yaml
from opentracing.scope_managers.asyncio import AsyncioScopeManager
from twisted.enterprise.adbapi import ConnectionPool
Expand Down
3 changes: 2 additions & 1 deletion tests/test_apns.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,12 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from unittest.mock import patch, MagicMock
from unittest.mock import MagicMock, patch

from aioapns.common import NotificationResult

from sygnal import apnstruncate

from tests import testutils

PUSHKIN_ID = "com.example.apns"
Expand Down
2 changes: 1 addition & 1 deletion tests/test_apnstruncate.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import string
import unittest

from sygnal.apnstruncate import truncate, json_encode
from sygnal.apnstruncate import json_encode, truncate


def simplestring(length, offset=0):
Expand Down
1 change: 1 addition & 0 deletions tests/test_gcm.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import json

from sygnal.gcmpushkin import GcmPushkin

from tests import testutils
from tests.testutils import DummyResponse

Expand Down
2 changes: 1 addition & 1 deletion tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from unittest.mock import patch, MagicMock
from unittest.mock import MagicMock, patch

from aioapns.common import NotificationResult

Expand Down
7 changes: 3 additions & 4 deletions tests/testutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,20 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import json
from os import environ
from time import time_ns
from io import BytesIO
from os import environ
from threading import Condition
from time import time_ns
from typing import BinaryIO, Optional, Union

import attr
import psycopg2
from twisted.internet.defer import ensureDeferred
from twisted.test.proto_helpers import MemoryReactorClock
from twisted.trial import unittest
from twisted.web.http_headers import Headers
from twisted.web.server import Request

import psycopg2

from sygnal.http import PushGatewayApiServer
from sygnal.sygnal import CONFIG_DEFAULTS, Sygnal, merge_left_with_defaults

Expand Down