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

Make scripts-dev pass mypy --disallow-untyped-defs #12356

Merged
merged 14 commits into from
Apr 27, 2022
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/12356.misc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix scripts-dev to pass typechecking.
10 changes: 6 additions & 4 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ files =
# https://docs.python.org/3/library/re.html#re.X
exclude = (?x)
^(
|scripts-dev/build_debian_packages.py
|scripts-dev/federation_client.py
|scripts-dev/release.py

|synapse/storage/databases/__init__.py
|synapse/storage/databases/main/cache.py
|synapse/storage/databases/main/devices.py
Expand Down Expand Up @@ -302,6 +298,9 @@ ignore_missing_imports = True
[mypy-pympler.*]
ignore_missing_imports = True

[mypy-redbaron.*]
ignore_missing_imports = True

[mypy-rust_python_jaeger_reporter.*]
ignore_missing_imports = True

Expand All @@ -317,6 +316,9 @@ ignore_missing_imports = True
[mypy-signedjson.*]
ignore_missing_imports = True

[mypy-srvlookup.*]
ignore_missing_imports = True

[mypy-treq.*]
ignore_missing_imports = True

Expand Down
25 changes: 19 additions & 6 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ flake8 = "*"
mypy = "==0.931"
mypy-zope = "==0.3.5"
types-bleach = ">=4.1.0"
types-commonmark = ">=0.9.2"
types-jsonschema = ">=3.2.0"
types-opentracing = ">=2.4.2"
types-Pillow = ">=8.3.4"
Expand All @@ -270,7 +271,8 @@ idna = ">=2.5"

# The following are used by the release script
click = "==8.1.0"
GitPython = "==3.1.14"
# GitPython was == 3.1.14; bumped to 3.1.20, the first release with type hints.
GitPython = ">=3.1.20"
commonmark = "==0.9.1"
pygithub = "==1.55"
# The following are executed as commands by the release script.
Expand Down
21 changes: 13 additions & 8 deletions scripts-dev/build_debian_packages.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
import sys
import threading
from concurrent.futures import ThreadPoolExecutor
from typing import Optional, Sequence
from types import FrameType
from typing import Collection, Optional, Sequence, Set

DISTS = (
"debian:buster", # oldstable: EOL 2022-08
Expand All @@ -41,15 +42,17 @@

class Builder(object):
def __init__(
self, redirect_stdout=False, docker_build_args: Optional[Sequence[str]] = None
self,
redirect_stdout: bool = False,
docker_build_args: Optional[Sequence[str]] = None,
):
self.redirect_stdout = redirect_stdout
self._docker_build_args = tuple(docker_build_args or ())
self.active_containers = set()
self.active_containers: Set[str] = set()
self._lock = threading.Lock()
self._failed = False

def run_build(self, dist, skip_tests=False):
def run_build(self, dist: str, skip_tests: bool = False) -> None:
"""Build deb for a single distribution"""

if self._failed:
Expand All @@ -63,7 +66,7 @@ def run_build(self, dist, skip_tests=False):
self._failed = True
raise

def _inner_build(self, dist, skip_tests=False):
def _inner_build(self, dist: str, skip_tests: bool = False) -> None:
tag = dist.split(":", 1)[1]

# Make the dir where the debs will live.
Expand Down Expand Up @@ -138,7 +141,7 @@ def _inner_build(self, dist, skip_tests=False):
stdout.close()
print("Completed build of %s" % (dist,))

def kill_containers(self):
def kill_containers(self) -> None:
with self._lock:
active = list(self.active_containers)

Expand All @@ -156,8 +159,10 @@ def kill_containers(self):
self.active_containers.remove(c)


def run_builds(builder, dists, jobs=1, skip_tests=False):
def sig(signum, _frame):
def run_builds(
builder: Builder, dists: Collection[str], jobs: int = 1, skip_tests: bool = False
) -> None:
def sig(signum: int, _frame: Optional[FrameType]) -> None:
print("Caught SIGINT")
builder.kill_containers()

Expand Down
27 changes: 17 additions & 10 deletions scripts-dev/federation_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import base64
import json
import sys
from typing import Any, Optional
from typing import Any, Dict, Optional, Tuple
from urllib import parse as urlparse

import requests
Expand All @@ -47,13 +47,14 @@
import srvlookup
import yaml
from requests.adapters import HTTPAdapter
from urllib3 import HTTPConnectionPool

# uncomment the following to enable debug logging of http requests
# from httplib import HTTPConnection
# HTTPConnection.debuglevel = 1


def encode_base64(input_bytes):
def encode_base64(input_bytes: bytes) -> str:
"""Encode bytes as a base64 string without any padding."""

input_len = len(input_bytes)
Expand All @@ -63,7 +64,7 @@ def encode_base64(input_bytes):
return output_string


def encode_canonical_json(value):
def encode_canonical_json(value: object) -> bytes:
return json.dumps(
value,
# Encode code-points outside of ASCII as UTF-8 rather than \u escapes
Expand Down Expand Up @@ -130,7 +131,7 @@ def request(
sig,
destination,
)
authorization_headers.append(header.encode("ascii"))
authorization_headers.append(header)
print("Authorization: %s" % header, file=sys.stderr)

dest = "matrix://%s%s" % (destination, path)
Expand All @@ -139,7 +140,10 @@ def request(
s = requests.Session()
s.mount("matrix://", MatrixConnectionAdapter())

headers = {"Host": destination, "Authorization": authorization_headers[0]}
headers: Dict[str, str] = {
"Host": destination,
"Authorization": authorization_headers[0],
}

if method == "POST":
headers["Content-Type"] = "application/json"
Expand All @@ -154,7 +158,7 @@ def request(
)


def main():
def main() -> None:
parser = argparse.ArgumentParser(
description="Signs and sends a federation request to a matrix homeserver"
)
Expand Down Expand Up @@ -212,6 +216,7 @@ def main():
if not args.server_name or not args.signing_key:
read_args_from_config(args)

assert isinstance(args.signing_key, str)
algorithm, version, key_base64 = args.signing_key.split()
key = signedjson.key.decode_signing_key_base64(algorithm, version, key_base64)

Expand All @@ -233,7 +238,7 @@ def main():
print("")


def read_args_from_config(args):
def read_args_from_config(args: argparse.Namespace) -> None:
with open(args.config, "r") as fh:
config = yaml.safe_load(fh)

Expand All @@ -250,7 +255,7 @@ def read_args_from_config(args):

class MatrixConnectionAdapter(HTTPAdapter):
@staticmethod
def lookup(s, skip_well_known=False):
def lookup(s: str, skip_well_known: bool = False) -> Tuple[str, int]:
if s[-1] == "]":
# ipv6 literal (with no port)
return s, 8448
Expand All @@ -276,7 +281,7 @@ def lookup(s, skip_well_known=False):
return s, 8448

@staticmethod
def get_well_known(server_name):
def get_well_known(server_name: str) -> Optional[str]:
uri = "https://%s/.well-known/matrix/server" % (server_name,)
print("fetching %s" % (uri,), file=sys.stderr)

Expand All @@ -299,7 +304,9 @@ def get_well_known(server_name):
print("Invalid response from %s: %s" % (uri, e), file=sys.stderr)
return None

def get_connection(self, url, proxies=None):
def get_connection(
self, url: str, proxies: Optional[Dict[str, str]] = None
) -> HTTPConnectionPool:
parsed = urlparse.urlparse(url)

(host, port) = self.lookup(parsed.netloc)
Expand Down
4 changes: 2 additions & 2 deletions scripts-dev/mypy_synapse_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
can crop up, e.g the cache descriptors.
"""

from typing import Callable, Optional
from typing import Callable, Optional, Type

from mypy.nodes import ARG_NAMED_OPT
from mypy.plugin import MethodSigContext, Plugin
Expand Down Expand Up @@ -94,7 +94,7 @@ def cached_function_method_signature(ctx: MethodSigContext) -> CallableType:
return signature


def plugin(version: str):
def plugin(version: str) -> Type[SynapsePlugin]:
# This is the entry point of the plugin, and let's us deal with the fact
# that the mypy plugin interface is *not* stable by looking at the version
# string.
Expand Down
Loading