From 76f383152cf356ba86ca4ace01ee20d4e8901606 Mon Sep 17 00:00:00 2001 From: Patrick Cloke Date: Fri, 12 Nov 2021 15:03:35 -0500 Subject: [PATCH] Add missing type hints. --- synapse/appservice/__init__.py | 30 ++++++++++++++++-------------- synapse/config/appservice.py | 3 +-- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/synapse/appservice/__init__.py b/synapse/appservice/__init__.py index 6504c6bd3f59..8dfc29c13737 100644 --- a/synapse/appservice/__init__.py +++ b/synapse/appservice/__init__.py @@ -13,7 +13,9 @@ # limitations under the License. import logging import re -from typing import TYPE_CHECKING, Iterable, List, Match, Optional +from typing import TYPE_CHECKING, Iterable, List, Match, Optional, Pattern + +from netaddr import IPSet from synapse.api.constants import EventTypes from synapse.events import EventBase @@ -49,17 +51,17 @@ class ApplicationService: def __init__( self, - token, - hostname, - id, - sender, - url=None, - namespaces=None, - hs_token=None, - protocols=None, - rate_limited=True, - ip_range_whitelist=None, - supports_ephemeral=False, + token: str, + hostname: str, + id: str, + sender: str, + url: Optional[str] = None, + namespaces: Optional[JsonDict] = None, + hs_token: Optional[str] = None, + protocols: Iterable[str] = None, + rate_limited: bool = True, + ip_range_whitelist: Optional[IPSet] = None, + supports_ephemeral: bool = False, ): self.token = token self.url = ( @@ -284,7 +286,7 @@ def is_exclusive_alias(self, alias: str) -> bool: def is_exclusive_room(self, room_id: str) -> bool: return self._is_exclusive(ApplicationService.NS_ROOMS, room_id) - def get_exclusive_user_regexes(self): + def get_exclusive_user_regexes(self) -> List[Pattern]: """Get the list of regexes used to determine if a user is exclusively registered by the AS """ @@ -312,7 +314,7 @@ def get_groups_for_user(self, user_id: str) -> Iterable[str]: def is_rate_limited(self) -> bool: return self.rate_limited - def __str__(self): + def __str__(self) -> str: # copy dictionary and redact token fields so they don't get logged dict_copy = self.__dict__.copy() dict_copy["token"] = "" diff --git a/synapse/config/appservice.py b/synapse/config/appservice.py index 1ebea88db2a2..2d6e7585911f 100644 --- a/synapse/config/appservice.py +++ b/synapse/config/appservice.py @@ -142,8 +142,7 @@ def _load_appservice(hostname, as_info, config_filename): # protocols check protocols = as_info.get("protocols") if protocols: - # Because strings are lists in python - if isinstance(protocols, str) or not isinstance(protocols, list): + if not isinstance(protocols, list): raise KeyError("Optional 'protocols' must be a list if present.") for p in protocols: if not isinstance(p, str):