Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Use inline type hints in various other places (in synapse/) #10380

Merged
merged 4 commits into from
Jul 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog.d/10380.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Convert internal type variable syntax to reflect wider ecosystem use.
4 changes: 2 additions & 2 deletions synapse/api/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,9 @@ def __init__(self, hs: "HomeServer"):
self.store = hs.get_datastore()
self.state = hs.get_state_handler()

self.token_cache = LruCache(
self.token_cache: LruCache[str, Tuple[str, bool]] = LruCache(
10000, "token_cache"
) # type: LruCache[str, Tuple[str, bool]]
)

self._auth_blocking = AuthBlocking(self.hs)

Expand Down
4 changes: 2 additions & 2 deletions synapse/api/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ def __init__(self, location: bytes, http_code: int = http.FOUND):
super().__init__(code=http_code, msg=msg)
self.location = location

self.cookies = [] # type: List[bytes]
self.cookies: List[bytes] = []


class SynapseError(CodeMessageException):
Expand Down Expand Up @@ -160,7 +160,7 @@ def __init__(
):
super().__init__(code, msg, errcode)
if additional_fields is None:
self._additional_fields = {} # type: Dict
self._additional_fields: Dict = {}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pointless typing is pointless :)

else:
self._additional_fields = dict(additional_fields)

Expand Down
2 changes: 1 addition & 1 deletion synapse/api/filtering.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def check(self, event):
room_id = None
ev_type = "m.presence"
contains_url = False
labels = [] # type: List[str]
labels: List[str] = []
else:
sender = event.get("sender", None)
if not sender:
Expand Down
4 changes: 1 addition & 3 deletions synapse/api/ratelimiting.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,7 @@ def __init__(
# * How many times an action has occurred since a point in time
# * The point in time
# * The rate_hz of this particular entry. This can vary per request
self.actions = (
OrderedDict()
) # type: OrderedDict[Hashable, Tuple[float, int, float]]
self.actions: OrderedDict[Hashable, Tuple[float, int, float]] = OrderedDict()

async def can_do_action(
self,
Expand Down
4 changes: 2 additions & 2 deletions synapse/api/room_versions.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ class RoomVersions:
)


KNOWN_ROOM_VERSIONS = {
KNOWN_ROOM_VERSIONS: Dict[str, RoomVersion] = {
v.identifier: v
for v in (
RoomVersions.V1,
Expand All @@ -209,4 +209,4 @@ class RoomVersions:
RoomVersions.V7,
)
# Note that we do not include MSC2043 here unless it is enabled in the config.
} # type: Dict[str, RoomVersion]
}
2 changes: 1 addition & 1 deletion synapse/app/generic_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ def _listen_http(self, listener_config: ListenerConfig):
site_tag = port

# We always include a health resource.
resources = {"/health": HealthResource()} # type: Dict[str, IResource]
resources: Dict[str, IResource] = {"/health": HealthResource()}

for res in listener_config.http_options.resources:
for name in res.names:
Expand Down
4 changes: 2 additions & 2 deletions synapse/appservice/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,9 @@ def __init__(self, hs):
super().__init__(hs)
self.clock = hs.get_clock()

self.protocol_meta_cache = ResponseCache(
self.protocol_meta_cache: ResponseCache[Tuple[str, str]] = ResponseCache(
hs.get_clock(), "as_protocol_meta", timeout_ms=HOUR_IN_MS
) # type: ResponseCache[Tuple[str, str]]
)

async def query_user(self, service, user_id):
if service.url is None:
Expand Down
4 changes: 2 additions & 2 deletions synapse/config/appservice.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ def load_appservices(hostname, config_files):
return []

# Dicts of value -> filename
seen_as_tokens = {} # type: Dict[str, str]
seen_ids = {} # type: Dict[str, str]
seen_as_tokens: Dict[str, str] = {}
seen_ids: Dict[str, str] = {}

appservices = []

Expand Down
4 changes: 2 additions & 2 deletions synapse/config/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
_CACHE_PREFIX = "SYNAPSE_CACHE_FACTOR"

# Map from canonicalised cache name to cache.
_CACHES = {} # type: Dict[str, Callable[[float], None]]
_CACHES: Dict[str, Callable[[float], None]] = {}

# a lock on the contents of _CACHES
_CACHES_LOCK = threading.Lock()
Expand Down Expand Up @@ -157,7 +157,7 @@ def read_config(self, config, **kwargs):
self.event_cache_size = self.parse_size(
config.get("event_cache_size", _DEFAULT_EVENT_CACHE_SIZE)
)
self.cache_factors = {} # type: Dict[str, float]
self.cache_factors: Dict[str, float] = {}

cache_config = config.get("caches") or {}
self.global_factor = cache_config.get(
Expand Down
4 changes: 2 additions & 2 deletions synapse/config/emailconfig.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,9 +134,9 @@ def read_config(self, config, **kwargs):

# trusted_third_party_id_servers does not contain a scheme whereas
# account_threepid_delegate_email is expected to. Presume https
self.account_threepid_delegate_email = (
self.account_threepid_delegate_email: Optional[str] = (
"https://" + first_trusted_identity_server
) # type: Optional[str]
)
self.using_identity_server_from_trusted_list = True
else:
raise ConfigError(
Expand Down
6 changes: 3 additions & 3 deletions synapse/config/experimental.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ def read_config(self, config: JsonDict, **kwargs):
experimental = config.get("experimental_features") or {}

# MSC2858 (multiple SSO identity providers)
self.msc2858_enabled = experimental.get("msc2858_enabled", False) # type: bool
self.msc2858_enabled: bool = experimental.get("msc2858_enabled", False)

# MSC3026 (busy presence state)
self.msc3026_enabled = experimental.get("msc3026_enabled", False) # type: bool
self.msc3026_enabled: bool = experimental.get("msc3026_enabled", False)

# MSC2716 (backfill existing history)
self.msc2716_enabled = experimental.get("msc2716_enabled", False) # type: bool
self.msc2716_enabled: bool = experimental.get("msc2716_enabled", False)
2 changes: 1 addition & 1 deletion synapse/config/federation.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class FederationConfig(Config):

def read_config(self, config, **kwargs):
# FIXME: federation_domain_whitelist needs sytests
self.federation_domain_whitelist = None # type: Optional[dict]
self.federation_domain_whitelist: Optional[dict] = None
federation_domain_whitelist = config.get("federation_domain_whitelist", None)

if federation_domain_whitelist is not None:
Expand Down
2 changes: 1 addition & 1 deletion synapse/config/oidc.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,7 +460,7 @@ def _parse_oidc_config_dict(
) from e

client_secret_jwt_key_config = oidc_config.get("client_secret_jwt_key")
client_secret_jwt_key = None # type: Optional[OidcProviderClientSecretJwtKey]
client_secret_jwt_key: Optional[OidcProviderClientSecretJwtKey] = None
if client_secret_jwt_key_config is not None:
keyfile = client_secret_jwt_key_config.get("key_file")
if keyfile:
Expand Down
2 changes: 1 addition & 1 deletion synapse/config/password_auth_providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class PasswordAuthProviderConfig(Config):
section = "authproviders"

def read_config(self, config, **kwargs):
self.password_providers = [] # type: List[Any]
self.password_providers: List[Any] = []
providers = []

# We want to be backwards compatible with the old `ldap_config`
Expand Down
4 changes: 2 additions & 2 deletions synapse/config/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def parse_thumbnail_requirements(thumbnail_sizes):
Dictionary mapping from media type string to list of
ThumbnailRequirement tuples.
"""
requirements = {} # type: Dict[str, List]
requirements: Dict[str, List] = {}
for size in thumbnail_sizes:
width = size["width"]
height = size["height"]
Expand Down Expand Up @@ -141,7 +141,7 @@ def read_config(self, config, **kwargs):
#
# We don't create the storage providers here as not all workers need
# them to be started.
self.media_storage_providers = [] # type: List[tuple]
self.media_storage_providers: List[tuple] = []

for i, provider_config in enumerate(storage_providers):
# We special case the module "file_system" so as not to need to
Expand Down
16 changes: 7 additions & 9 deletions synapse/config/server.py
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ def read_config(self, config, **kwargs):
" greater than 'allowed_lifetime_max'"
)

self.retention_purge_jobs = [] # type: List[Dict[str, Optional[int]]]
self.retention_purge_jobs: List[Dict[str, Optional[int]]] = []
for purge_job_config in retention_config.get("purge_jobs", []):
interval_config = purge_job_config.get("interval")

Expand Down Expand Up @@ -688,23 +688,21 @@ class LimitRemoteRoomsConfig:
# not included in the sample configuration file on purpose as it's a temporary
# hack, so that some users can trial the new defaults without impacting every
# user on the homeserver.
users_new_default_push_rules = (
users_new_default_push_rules: list = (
config.get("users_new_default_push_rules") or []
) # type: list
)
if not isinstance(users_new_default_push_rules, list):
raise ConfigError("'users_new_default_push_rules' must be a list")

# Turn the list into a set to improve lookup speed.
self.users_new_default_push_rules = set(
users_new_default_push_rules
) # type: set
self.users_new_default_push_rules: set = set(users_new_default_push_rules)

# Whitelist of domain names that given next_link parameters must have
next_link_domain_whitelist = config.get(
next_link_domain_whitelist: Optional[List[str]] = config.get(
"next_link_domain_whitelist"
) # type: Optional[List[str]]
)

self.next_link_domain_whitelist = None # type: Optional[Set[str]]
self.next_link_domain_whitelist: Optional[Set[str]] = None
if next_link_domain_whitelist is not None:
if not isinstance(next_link_domain_whitelist, list):
raise ConfigError("'next_link_domain_whitelist' must be a list")
Expand Down
2 changes: 1 addition & 1 deletion synapse/config/spam_checker.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class SpamCheckerConfig(Config):
section = "spamchecker"

def read_config(self, config, **kwargs):
self.spam_checkers = [] # type: List[Tuple[Any, Dict]]
self.spam_checkers: List[Tuple[Any, Dict]] = []

spam_checkers = config.get("spam_checker") or []
if isinstance(spam_checkers, dict):
Expand Down
2 changes: 1 addition & 1 deletion synapse/config/sso.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ class SSOConfig(Config):
section = "sso"

def read_config(self, config, **kwargs):
sso_config = config.get("sso") or {} # type: Dict[str, Any]
sso_config: Dict[str, Any] = config.get("sso") or {}

# The sso-specific template_dir
self.sso_template_dir = sso_config.get("template_dir")
Expand Down
6 changes: 3 additions & 3 deletions synapse/config/tls.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def read_config(self, config: dict, config_dir_path: str, **kwargs):
fed_whitelist_entries = []

# Support globs (*) in whitelist values
self.federation_certificate_verification_whitelist = [] # type: List[Pattern]
self.federation_certificate_verification_whitelist: List[Pattern] = []
for entry in fed_whitelist_entries:
try:
entry_regex = glob_to_regex(entry.encode("ascii").decode("ascii"))
Expand Down Expand Up @@ -132,8 +132,8 @@ def read_config(self, config: dict, config_dir_path: str, **kwargs):
"use_insecure_ssl_client_just_for_testing_do_not_use"
)

self.tls_certificate = None # type: Optional[crypto.X509]
self.tls_private_key = None # type: Optional[crypto.PKey]
self.tls_certificate: Optional[crypto.X509] = None
self.tls_private_key: Optional[crypto.PKey] = None

def is_disk_cert_valid(self, allow_self_signed=True):
"""
Expand Down
20 changes: 11 additions & 9 deletions synapse/crypto/keyring.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,11 +170,13 @@ def __init__(
)
self._key_fetchers = key_fetchers

self._server_queue = BatchingQueue(
self._server_queue: BatchingQueue[
_FetchKeyRequest, Dict[str, Dict[str, FetchKeyResult]]
] = BatchingQueue(
"keyring_server",
clock=hs.get_clock(),
process_batch_callback=self._inner_fetch_key_requests,
) # type: BatchingQueue[_FetchKeyRequest, Dict[str, Dict[str, FetchKeyResult]]]
)

async def verify_json_for_server(
self,
Expand Down Expand Up @@ -330,7 +332,7 @@ async def _inner_fetch_key_requests(
# First we need to deduplicate requests for the same key. We do this by
# taking the *maximum* requested `minimum_valid_until_ts` for each pair
# of server name/key ID.
server_to_key_to_ts = {} # type: Dict[str, Dict[str, int]]
server_to_key_to_ts: Dict[str, Dict[str, int]] = {}
for request in requests:
by_server = server_to_key_to_ts.setdefault(request.server_name, {})
for key_id in request.key_ids:
Expand All @@ -355,7 +357,7 @@ async def _inner_fetch_key_requests(

# We now convert the returned list of results into a map from server
# name to key ID to FetchKeyResult, to return.
to_return = {} # type: Dict[str, Dict[str, FetchKeyResult]]
to_return: Dict[str, Dict[str, FetchKeyResult]] = {}
for (request, results) in zip(deduped_requests, results_per_request):
to_return_by_server = to_return.setdefault(request.server_name, {})
for key_id, key_result in results.items():
Expand Down Expand Up @@ -455,7 +457,7 @@ async def _fetch_keys(self, keys_to_fetch: List[_FetchKeyRequest]):
)

res = await self.store.get_server_verify_keys(key_ids_to_fetch)
keys = {} # type: Dict[str, Dict[str, FetchKeyResult]]
keys: Dict[str, Dict[str, FetchKeyResult]] = {}
for (server_name, key_id), key in res.items():
keys.setdefault(server_name, {})[key_id] = key
return keys
Expand Down Expand Up @@ -603,7 +605,7 @@ async def get_key(key_server: TrustedKeyServer) -> Dict:
).addErrback(unwrapFirstError)
)

union_of_keys = {} # type: Dict[str, Dict[str, FetchKeyResult]]
union_of_keys: Dict[str, Dict[str, FetchKeyResult]] = {}
for result in results:
for server_name, keys in result.items():
union_of_keys.setdefault(server_name, {}).update(keys)
Expand Down Expand Up @@ -656,8 +658,8 @@ async def get_server_verify_key_v2_indirect(
except HttpResponseException as e:
raise KeyLookupError("Remote server returned an error: %s" % (e,))

keys = {} # type: Dict[str, Dict[str, FetchKeyResult]]
added_keys = [] # type: List[Tuple[str, str, FetchKeyResult]]
keys: Dict[str, Dict[str, FetchKeyResult]] = {}
added_keys: List[Tuple[str, str, FetchKeyResult]] = []

time_now_ms = self.clock.time_msec()

Expand Down Expand Up @@ -805,7 +807,7 @@ async def get_server_verify_key_v2_direct(
Raises:
KeyLookupError if there was a problem making the lookup
"""
keys = {} # type: Dict[str, FetchKeyResult]
keys: Dict[str, FetchKeyResult] = {}

for requested_key_id in key_ids:
# we may have found this key as a side-effect of asking for another.
Expand Down
8 changes: 4 additions & 4 deletions synapse/event_auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -528,15 +528,15 @@ def _check_power_levels(
user_level = get_user_power_level(event.user_id, auth_events)

# Check other levels:
levels_to_check = [
levels_to_check: List[Tuple[str, Optional[str]]] = [
("users_default", None),
("events_default", None),
("state_default", None),
("ban", None),
("redact", None),
("kick", None),
("invite", None),
] # type: List[Tuple[str, Optional[str]]]
]

old_list = current_state.content.get("users", {})
for user in set(list(old_list) + list(user_list)):
Expand Down Expand Up @@ -566,12 +566,12 @@ def _check_power_levels(
new_loc = new_loc.get(dir, {})

if level_to_check in old_loc:
old_level = int(old_loc[level_to_check]) # type: Optional[int]
old_level: Optional[int] = int(old_loc[level_to_check])
else:
old_level = None

if level_to_check in new_loc:
new_level = int(new_loc[level_to_check]) # type: Optional[int]
new_level: Optional[int] = int(new_loc[level_to_check])
else:
new_level = None

Expand Down
Loading