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

Commit 4c96ce3

Browse files
author
David Robertson
authored
Misc typing fixes for tests, part 1 of N (#11323)
* Annotate HomeserverTestCase.servlets * Correct annotation of federation_auth_origin * Use AnyStr custom_headers instead of a Union This allows (str, str) and (bytes, bytes). This disallows (str, bytes) and (bytes, str) * DomainSpecificString.SIGIL is a ClassVar
1 parent 95547e5 commit 4c96ce3

File tree

7 files changed

+53
-29
lines changed

7 files changed

+53
-29
lines changed

changelog.d/11323.misc

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Improve type annotations in Synapse's test suite.

synapse/rest/__init__.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313
# See the License for the specific language governing permissions and
1414
# limitations under the License.
15-
from typing import TYPE_CHECKING
15+
from typing import TYPE_CHECKING, Callable
1616

1717
from synapse.http.server import HttpServer, JsonResource
1818
from synapse.rest import admin
@@ -62,6 +62,8 @@
6262
if TYPE_CHECKING:
6363
from synapse.server import HomeServer
6464

65+
RegisterServletsFunc = Callable[["HomeServer", HttpServer], None]
66+
6567

6668
class ClientRestResource(JsonResource):
6769
"""Matrix Client API REST resource.

synapse/types.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
from typing import (
2020
TYPE_CHECKING,
2121
Any,
22+
ClassVar,
2223
Dict,
2324
Mapping,
2425
MutableMapping,
@@ -219,7 +220,7 @@ class DomainSpecificString(metaclass=abc.ABCMeta):
219220
'domain' : The domain part of the name
220221
"""
221222

222-
SIGIL: str = abc.abstractproperty() # type: ignore
223+
SIGIL: ClassVar[str] = abc.abstractproperty() # type: ignore
223224

224225
localpart = attr.ib(type=str)
225226
domain = attr.ib(type=str)

tests/replication/_base.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,12 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414
import logging
15-
from typing import Any, Callable, Dict, List, Optional, Tuple
15+
from typing import Any, Dict, List, Optional, Tuple
1616

1717
from twisted.internet.protocol import Protocol
1818
from twisted.web.resource import Resource
1919

2020
from synapse.app.generic_worker import GenericWorkerServer
21-
from synapse.http.server import JsonResource
2221
from synapse.http.site import SynapseRequest, SynapseSite
2322
from synapse.replication.http import ReplicationRestResource
2423
from synapse.replication.tcp.client import ReplicationDataHandler
@@ -220,8 +219,6 @@ class BaseMultiWorkerStreamTestCase(unittest.HomeserverTestCase):
220219
unlike `BaseStreamTestCase`.
221220
"""
222221

223-
servlets: List[Callable[[HomeServer, JsonResource], None]] = []
224-
225222
def setUp(self):
226223
super().setUp()
227224

tests/rest/client/utils.py

+14-8
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,17 @@
1919
import re
2020
import time
2121
import urllib.parse
22-
from typing import Any, Dict, Iterable, Mapping, MutableMapping, Optional, Tuple, Union
22+
from typing import (
23+
Any,
24+
AnyStr,
25+
Dict,
26+
Iterable,
27+
Mapping,
28+
MutableMapping,
29+
Optional,
30+
Tuple,
31+
Union,
32+
)
2333
from unittest.mock import patch
2434

2535
import attr
@@ -53,9 +63,7 @@ def create_room_as(
5363
tok: Optional[str] = None,
5464
expect_code: int = 200,
5565
extra_content: Optional[Dict] = None,
56-
custom_headers: Optional[
57-
Iterable[Tuple[Union[bytes, str], Union[bytes, str]]]
58-
] = None,
66+
custom_headers: Optional[Iterable[Tuple[AnyStr, AnyStr]]] = None,
5967
) -> str:
6068
"""
6169
Create a room.
@@ -227,9 +235,7 @@ def send(
227235
txn_id=None,
228236
tok=None,
229237
expect_code=200,
230-
custom_headers: Optional[
231-
Iterable[Tuple[Union[bytes, str], Union[bytes, str]]]
232-
] = None,
238+
custom_headers: Optional[Iterable[Tuple[AnyStr, AnyStr]]] = None,
233239
):
234240
if body is None:
235241
body = "body_text_here"
@@ -418,7 +424,7 @@ def upload_media(
418424
path,
419425
content=image_data,
420426
access_token=tok,
421-
custom_headers=[(b"Content-Length", str(image_length))],
427+
custom_headers=[("Content-Length", str(image_length))],
422428
)
423429

424430
assert channel.code == expect_code, "Expected: %d, got: %d, resp: %r" % (

tests/server.py

+11-4
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,16 @@
1616
import logging
1717
from collections import deque
1818
from io import SEEK_END, BytesIO
19-
from typing import Callable, Dict, Iterable, MutableMapping, Optional, Tuple, Union
19+
from typing import (
20+
AnyStr,
21+
Callable,
22+
Dict,
23+
Iterable,
24+
MutableMapping,
25+
Optional,
26+
Tuple,
27+
Union,
28+
)
2029

2130
import attr
2231
from typing_extensions import Deque
@@ -222,9 +231,7 @@ def make_request(
222231
federation_auth_origin: Optional[bytes] = None,
223232
content_is_form: bool = False,
224233
await_result: bool = True,
225-
custom_headers: Optional[
226-
Iterable[Tuple[Union[bytes, str], Union[bytes, str]]]
227-
] = None,
234+
custom_headers: Optional[Iterable[Tuple[AnyStr, AnyStr]]] = None,
228235
client_ip: str = "127.0.0.1",
229236
) -> FakeChannel:
230237
"""

tests/unittest.py

+21-11
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,20 @@
2020
import logging
2121
import secrets
2222
import time
23-
from typing import Any, Callable, Dict, Iterable, Optional, Tuple, Type, TypeVar, Union
23+
from typing import (
24+
Any,
25+
AnyStr,
26+
Callable,
27+
ClassVar,
28+
Dict,
29+
Iterable,
30+
List,
31+
Optional,
32+
Tuple,
33+
Type,
34+
TypeVar,
35+
Union,
36+
)
2437
from unittest.mock import Mock, patch
2538

2639
from canonicaljson import json
@@ -45,6 +58,7 @@
4558
current_context,
4659
set_current_context,
4760
)
61+
from synapse.rest import RegisterServletsFunc
4862
from synapse.server import HomeServer
4963
from synapse.types import JsonDict, UserID, create_requester
5064
from synapse.util import Clock
@@ -204,15 +218,15 @@ class HomeserverTestCase(TestCase):
204218
config dict.
205219
206220
Attributes:
207-
servlets (list[function]): List of servlet registration function.
221+
servlets: List of servlet registration function.
208222
user_id (str): The user ID to assume if auth is hijacked.
209223
hijack_auth (bool): Whether to hijack auth to return the user specified
210224
in user_id.
211225
"""
212226

213-
servlets = []
214227
hijack_auth = True
215228
needs_threadpool = False
229+
servlets: ClassVar[List[RegisterServletsFunc]] = []
216230

217231
def __init__(self, methodName, *args, **kwargs):
218232
super().__init__(methodName, *args, **kwargs)
@@ -405,12 +419,10 @@ def make_request(
405419
access_token: Optional[str] = None,
406420
request: Type[T] = SynapseRequest,
407421
shorthand: bool = True,
408-
federation_auth_origin: str = None,
422+
federation_auth_origin: Optional[bytes] = None,
409423
content_is_form: bool = False,
410424
await_result: bool = True,
411-
custom_headers: Optional[
412-
Iterable[Tuple[Union[bytes, str], Union[bytes, str]]]
413-
] = None,
425+
custom_headers: Optional[Iterable[Tuple[AnyStr, AnyStr]]] = None,
414426
client_ip: str = "127.0.0.1",
415427
) -> FakeChannel:
416428
"""
@@ -425,7 +437,7 @@ def make_request(
425437
a dict.
426438
shorthand: Whether to try and be helpful and prefix the given URL
427439
with the usual REST API path, if it doesn't contain it.
428-
federation_auth_origin (bytes|None): if set to not-None, we will add a fake
440+
federation_auth_origin: if set to not-None, we will add a fake
429441
Authorization header pretenting to be the given server name.
430442
content_is_form: Whether the content is URL encoded form data. Adds the
431443
'Content-Type': 'application/x-www-form-urlencoded' header.
@@ -639,9 +651,7 @@ def login(
639651
username,
640652
password,
641653
device_id=None,
642-
custom_headers: Optional[
643-
Iterable[Tuple[Union[bytes, str], Union[bytes, str]]]
644-
] = None,
654+
custom_headers: Optional[Iterable[Tuple[AnyStr, AnyStr]]] = None,
645655
):
646656
"""
647657
Log in a user, and get an access token. Requires the Login API be

0 commit comments

Comments
 (0)