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

Commit 30c8e7e

Browse files
author
David Robertson
authored
Make scripts-dev pass mypy --disallow-untyped-defs (#12356)
Not enforced in config yet. One day.
1 parent 6463244 commit 30c8e7e

9 files changed

Lines changed: 96 additions & 53 deletions

File tree

changelog.d/12356.misc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix scripts-dev to pass typechecking.

mypy.ini

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,6 @@ files =
2424
# https://docs.python.org/3/library/re.html#re.X
2525
exclude = (?x)
2626
^(
27-
|scripts-dev/build_debian_packages.py
28-
|scripts-dev/federation_client.py
29-
|scripts-dev/release.py
30-
3127
|synapse/storage/databases/__init__.py
3228
|synapse/storage/databases/main/cache.py
3329
|synapse/storage/databases/main/devices.py
@@ -308,6 +304,9 @@ ignore_missing_imports = True
308304
[mypy-pympler.*]
309305
ignore_missing_imports = True
310306

307+
[mypy-redbaron.*]
308+
ignore_missing_imports = True
309+
311310
[mypy-rust_python_jaeger_reporter.*]
312311
ignore_missing_imports = True
313312

@@ -323,6 +322,9 @@ ignore_missing_imports = True
323322
[mypy-signedjson.*]
324323
ignore_missing_imports = True
325324

325+
[mypy-srvlookup.*]
326+
ignore_missing_imports = True
327+
326328
[mypy-treq.*]
327329
ignore_missing_imports = True
328330

poetry.lock

Lines changed: 19 additions & 6 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,7 @@ flake8 = "*"
251251
mypy = "==0.931"
252252
mypy-zope = "==0.3.5"
253253
types-bleach = ">=4.1.0"
254+
types-commonmark = ">=0.9.2"
254255
types-jsonschema = ">=3.2.0"
255256
types-opentracing = ">=2.4.2"
256257
types-Pillow = ">=8.3.4"
@@ -270,7 +271,8 @@ idna = ">=2.5"
270271

271272
# The following are used by the release script
272273
click = "==8.1.0"
273-
GitPython = "==3.1.14"
274+
# GitPython was == 3.1.14; bumped to 3.1.20, the first release with type hints.
275+
GitPython = ">=3.1.20"
274276
commonmark = "==0.9.1"
275277
pygithub = "==1.55"
276278
# The following are executed as commands by the release script.

scripts-dev/build_debian_packages.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
import sys
1818
import threading
1919
from concurrent.futures import ThreadPoolExecutor
20-
from typing import Optional, Sequence
20+
from types import FrameType
21+
from typing import Collection, Optional, Sequence, Set
2122

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

4243
class Builder(object):
4344
def __init__(
44-
self, redirect_stdout=False, docker_build_args: Optional[Sequence[str]] = None
45+
self,
46+
redirect_stdout: bool = False,
47+
docker_build_args: Optional[Sequence[str]] = None,
4548
):
4649
self.redirect_stdout = redirect_stdout
4750
self._docker_build_args = tuple(docker_build_args or ())
48-
self.active_containers = set()
51+
self.active_containers: Set[str] = set()
4952
self._lock = threading.Lock()
5053
self._failed = False
5154

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

5558
if self._failed:
@@ -63,7 +66,7 @@ def run_build(self, dist, skip_tests=False):
6366
self._failed = True
6467
raise
6568

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

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

141-
def kill_containers(self):
144+
def kill_containers(self) -> None:
142145
with self._lock:
143146
active = list(self.active_containers)
144147

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

158161

159-
def run_builds(builder, dists, jobs=1, skip_tests=False):
160-
def sig(signum, _frame):
162+
def run_builds(
163+
builder: Builder, dists: Collection[str], jobs: int = 1, skip_tests: bool = False
164+
) -> None:
165+
def sig(signum: int, _frame: Optional[FrameType]) -> None:
161166
print("Caught SIGINT")
162167
builder.kill_containers()
163168

scripts-dev/federation_client.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
import base64
3939
import json
4040
import sys
41-
from typing import Any, Optional
41+
from typing import Any, Dict, Optional, Tuple
4242
from urllib import parse as urlparse
4343

4444
import requests
@@ -47,13 +47,14 @@
4747
import srvlookup
4848
import yaml
4949
from requests.adapters import HTTPAdapter
50+
from urllib3 import HTTPConnectionPool
5051

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

5556

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

5960
input_len = len(input_bytes)
@@ -63,7 +64,7 @@ def encode_base64(input_bytes):
6364
return output_string
6465

6566

66-
def encode_canonical_json(value):
67+
def encode_canonical_json(value: object) -> bytes:
6768
return json.dumps(
6869
value,
6970
# Encode code-points outside of ASCII as UTF-8 rather than \u escapes
@@ -130,7 +131,7 @@ def request(
130131
sig,
131132
destination,
132133
)
133-
authorization_headers.append(header.encode("ascii"))
134+
authorization_headers.append(header)
134135
print("Authorization: %s" % header, file=sys.stderr)
135136

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

142-
headers = {"Host": destination, "Authorization": authorization_headers[0]}
143+
headers: Dict[str, str] = {
144+
"Host": destination,
145+
"Authorization": authorization_headers[0],
146+
}
143147

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

156160

157-
def main():
161+
def main() -> None:
158162
parser = argparse.ArgumentParser(
159163
description="Signs and sends a federation request to a matrix homeserver"
160164
)
@@ -212,6 +216,7 @@ def main():
212216
if not args.server_name or not args.signing_key:
213217
read_args_from_config(args)
214218

219+
assert isinstance(args.signing_key, str)
215220
algorithm, version, key_base64 = args.signing_key.split()
216221
key = signedjson.key.decode_signing_key_base64(algorithm, version, key_base64)
217222

@@ -233,7 +238,7 @@ def main():
233238
print("")
234239

235240

236-
def read_args_from_config(args):
241+
def read_args_from_config(args: argparse.Namespace) -> None:
237242
with open(args.config, "r") as fh:
238243
config = yaml.safe_load(fh)
239244

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

251256
class MatrixConnectionAdapter(HTTPAdapter):
252257
@staticmethod
253-
def lookup(s, skip_well_known=False):
258+
def lookup(s: str, skip_well_known: bool = False) -> Tuple[str, int]:
254259
if s[-1] == "]":
255260
# ipv6 literal (with no port)
256261
return s, 8448
@@ -276,7 +281,7 @@ def lookup(s, skip_well_known=False):
276281
return s, 8448
277282

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

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

302-
def get_connection(self, url, proxies=None):
307+
def get_connection(
308+
self, url: str, proxies: Optional[Dict[str, str]] = None
309+
) -> HTTPConnectionPool:
303310
parsed = urlparse.urlparse(url)
304311

305312
(host, port) = self.lookup(parsed.netloc)

scripts-dev/mypy_synapse_plugin.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
can crop up, e.g the cache descriptors.
1717
"""
1818

19-
from typing import Callable, Optional
19+
from typing import Callable, Optional, Type
2020

2121
from mypy.nodes import ARG_NAMED_OPT
2222
from mypy.plugin import MethodSigContext, Plugin
@@ -94,7 +94,7 @@ def cached_function_method_signature(ctx: MethodSigContext) -> CallableType:
9494
return signature
9595

9696

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

0 commit comments

Comments
 (0)