Skip to content

Commit 9367682

Browse files
committed
ruff: type annotations fixes
1 parent ee928ec commit 9367682

4 files changed

Lines changed: 57 additions & 63 deletions

File tree

pytest_httpserver/blocking_httpserver.py

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,8 @@
44
from queue import Queue
55
from typing import TYPE_CHECKING
66
from typing import Any
7-
from typing import Dict
87
from typing import Mapping
9-
from typing import Optional
108
from typing import Pattern
11-
from typing import Union
129

1310
from pytest_httpserver.httpserver import METHOD_ALL
1411
from pytest_httpserver.httpserver import UNDEFINED
@@ -64,23 +61,23 @@ def __init__(
6461
self,
6562
host=DEFAULT_LISTEN_HOST,
6663
port=DEFAULT_LISTEN_PORT,
67-
ssl_context: Optional[SSLContext] = None,
64+
ssl_context: SSLContext | None = None,
6865
timeout: int = 30,
6966
):
7067
super().__init__(host, port, ssl_context)
7168
self.timeout = timeout
7269
self.request_queue: Queue[Request] = Queue()
73-
self.request_handlers: Dict[Request, Queue[BlockingRequestHandler]] = {}
70+
self.request_handlers: dict[Request, Queue[BlockingRequestHandler]] = {}
7471

7572
def assert_request(
7673
self,
77-
uri: Union[str, URIPattern, Pattern[str]],
74+
uri: str | URIPattern | Pattern[str],
7875
method: str = METHOD_ALL,
79-
data: Union[str, bytes, None] = None,
76+
data: str | bytes | None = None,
8077
data_encoding: str = "utf-8",
81-
headers: Optional[Mapping[str, str]] = None,
82-
query_string: Union[None, QueryMatcher, str, bytes, Mapping] = None,
83-
header_value_matcher: Optional[HeaderValueMatcher] = None,
78+
headers: Mapping[str, str] | None = None,
79+
query_string: None | QueryMatcher | str | bytes | Mapping = None,
80+
header_value_matcher: HeaderValueMatcher | None = None,
8481
json: Any = UNDEFINED,
8582
timeout: int = 30,
8683
) -> BlockingRequestHandler:

pytest_httpserver/httpserver.py

Lines changed: 48 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616
from typing import TYPE_CHECKING
1717
from typing import Any
1818
from typing import Callable
19+
from typing import ClassVar
1920
from typing import Iterable
20-
from typing import List
2121
from typing import Mapping
2222
from typing import MutableMapping
2323
from typing import Optional
@@ -137,23 +137,23 @@ class HeaderValueMatcher:
137137
and return whether they are equal as bool.
138138
"""
139139

140-
DEFAULT_MATCHERS: MutableMapping[str, Callable[[Optional[str], str], bool]] = {}
140+
DEFAULT_MATCHERS: ClassVar[MutableMapping[str, Callable[[str | None, str], bool]]] = {}
141141

142-
def __init__(self, matchers: Optional[Mapping[str, Callable[[Optional[str], str], bool]]] = None):
142+
def __init__(self, matchers: Mapping[str, Callable[[str | None, str], bool]] | None = None):
143143
self.matchers = self.DEFAULT_MATCHERS if matchers is None else matchers
144144

145145
@staticmethod
146-
def authorization_header_value_matcher(actual: Optional[str], expected: str) -> bool:
146+
def authorization_header_value_matcher(actual: str | None, expected: str) -> bool:
147147
callable = getattr(Authorization, "from_header", None)
148148
if callable is None: # Werkzeug < 2.3.0
149149
callable = werkzeug.http.parse_authorization_header # type: ignore[attr-defined]
150150
return callable(actual) == callable(expected)
151151

152152
@staticmethod
153-
def default_header_value_matcher(actual: Optional[str], expected: str) -> bool:
153+
def default_header_value_matcher(actual: str | None, expected: str) -> bool:
154154
return actual == expected
155155

156-
def __call__(self, header_name: str, actual: Optional[str], expected: str) -> bool:
156+
def __call__(self, header_name: str, actual: str | None, expected: str) -> bool:
157157
try:
158158
matcher = self.matchers[header_name]
159159
except KeyError:
@@ -191,7 +191,7 @@ class StringQueryMatcher(QueryMatcher):
191191
Matches a query for a string or bytes specified
192192
"""
193193

194-
def __init__(self, query_string: Union[bytes, str]):
194+
def __init__(self, query_string: bytes | str):
195195
"""
196196
:param query_string: the query string will be compared to this string or bytes.
197197
If string is specified, it will be encoded by the encode() method.
@@ -219,7 +219,7 @@ class MappingQueryMatcher(QueryMatcher):
219219
Matches a query string to a dictionary or MultiDict specified
220220
"""
221221

222-
def __init__(self, query_dict: Union[Mapping, MultiDict]):
222+
def __init__(self, query_dict: Mapping | MultiDict):
223223
"""
224224
:param query_dict: if dictionary (Mapping) is specified, it will be used as a
225225
key-value mapping where both key and value should be string. If there are multiple
@@ -256,7 +256,7 @@ def get_comparing_values(self, request_query_string):
256256
return (True, False)
257257

258258

259-
def _create_query_matcher(query_string: Union[None, QueryMatcher, str, bytes, Mapping]) -> QueryMatcher:
259+
def _create_query_matcher(query_string: None | QueryMatcher | str | bytes | Mapping) -> QueryMatcher:
260260
if isinstance(query_string, QueryMatcher):
261261
return query_string
262262

@@ -316,13 +316,13 @@ class RequestMatcher:
316316

317317
def __init__(
318318
self,
319-
uri: Union[str, URIPattern, Pattern[str]],
319+
uri: str | URIPattern | Pattern[str],
320320
method: str = METHOD_ALL,
321-
data: Union[str, bytes, None] = None,
321+
data: str | bytes | None = None,
322322
data_encoding: str = "utf-8",
323-
headers: Optional[Mapping[str, str]] = None,
324-
query_string: Union[None, QueryMatcher, str, bytes, Mapping] = None,
325-
header_value_matcher: Optional[HVMATCHER_T] = None,
323+
headers: Mapping[str, str] | None = None,
324+
query_string: None | QueryMatcher | str | bytes | Mapping = None,
325+
header_value_matcher: HVMATCHER_T | None = None,
326326
json: Any = UNDEFINED,
327327
):
328328
if json is not UNDEFINED and data is not None:
@@ -419,7 +419,7 @@ def match_json(self, request: Request) -> bool:
419419

420420
return json_received == self.json
421421

422-
def difference(self, request: Request) -> List[Tuple]:
422+
def difference(self, request: Request) -> list[tuple]:
423423
"""
424424
Calculates the difference between the matcher and the request.
425425
@@ -431,7 +431,7 @@ def difference(self, request: Request) -> List[Tuple]:
431431
matches the fields set in the matcher object.
432432
"""
433433

434-
retval: List[Tuple] = []
434+
retval: list[tuple] = []
435435

436436
if not self.match_uri(request):
437437
retval.append(("uri", request.path, self.uri))
@@ -478,7 +478,7 @@ def respond_with_json(
478478
self,
479479
response_json,
480480
status: int = 200,
481-
headers: Optional[Mapping[str, str]] = None,
481+
headers: Mapping[str, str] | None = None,
482482
content_type: str = "application/json",
483483
):
484484
"""
@@ -495,11 +495,11 @@ def respond_with_json(
495495

496496
def respond_with_data(
497497
self,
498-
response_data: Union[str, bytes] = "",
498+
response_data: str | bytes = "",
499499
status: int = 200,
500-
headers: Optional[HEADERS_T] = None,
501-
mimetype: Optional[str] = None,
502-
content_type: Optional[str] = None,
500+
headers: HEADERS_T | None = None,
501+
mimetype: str | None = None,
502+
content_type: str | None = None,
503503
):
504504
"""
505505
Prepares a response with raw data.
@@ -539,7 +539,7 @@ class RequestHandler(RequestHandlerBase):
539539

540540
def __init__(self, matcher: RequestMatcher):
541541
self.matcher = matcher
542-
self.request_handler: Optional[Callable[[Request], Response]] = None
542+
self.request_handler: Callable[[Request], Response] | None = None
543543

544544
def respond(self, request: Request) -> Response:
545545
"""
@@ -586,7 +586,7 @@ class RequestHandlerList(list):
586586
587587
"""
588588

589-
def match(self, request: Request) -> Optional[RequestHandler]:
589+
def match(self, request: Request) -> RequestHandler | None:
590590
"""
591591
Returns the first request handler which matches the specified request. Otherwise, it returns `None`.
592592
"""
@@ -629,7 +629,7 @@ def __init__(
629629
self,
630630
host: str,
631631
port: int,
632-
ssl_context: Optional[SSLContext] = None,
632+
ssl_context: SSLContext | None = None,
633633
):
634634
"""
635635
Initializes the instance.
@@ -639,9 +639,9 @@ def __init__(
639639
self.port = port
640640
self.server = None
641641
self.server_thread = None
642-
self.assertions: List[str] = []
643-
self.handler_errors: List[Exception] = []
644-
self.log: List[Tuple[Request, Response]] = []
642+
self.assertions: list[str] = []
643+
self.handler_errors: list[Exception] = []
644+
self.log: list[tuple[Request, Response]] = []
645645
self.ssl_context = ssl_context
646646
self.no_handler_status_code = 500
647647

@@ -926,15 +926,15 @@ def __init__(
926926
self,
927927
host=DEFAULT_LISTEN_HOST,
928928
port=DEFAULT_LISTEN_PORT,
929-
ssl_context: Optional[SSLContext] = None,
930-
default_waiting_settings: Optional[WaitingSettings] = None,
929+
ssl_context: SSLContext | None = None,
930+
default_waiting_settings: WaitingSettings | None = None,
931931
):
932932
"""
933933
Initializes the instance.
934934
"""
935935
super().__init__(host, port, ssl_context)
936936

937-
self.ordered_handlers: List[RequestHandler] = []
937+
self.ordered_handlers: list[RequestHandler] = []
938938
self.oneshot_handlers = RequestHandlerList()
939939
self.handlers = RequestHandlerList()
940940
self.permanently_failed = False
@@ -967,13 +967,13 @@ def clear_all_handlers(self):
967967

968968
def expect_request(
969969
self,
970-
uri: Union[str, URIPattern, Pattern[str]],
970+
uri: str | URIPattern | Pattern[str],
971971
method: str = METHOD_ALL,
972-
data: Union[str, bytes, None] = None,
972+
data: str | bytes | None = None,
973973
data_encoding: str = "utf-8",
974-
headers: Optional[Mapping[str, str]] = None,
975-
query_string: Union[None, QueryMatcher, str, bytes, Mapping] = None,
976-
header_value_matcher: Optional[HVMATCHER_T] = None,
974+
headers: Mapping[str, str] | None = None,
975+
query_string: None | QueryMatcher | str | bytes | Mapping = None,
976+
header_value_matcher: HVMATCHER_T | None = None,
977977
handler_type: HandlerType = HandlerType.PERMANENT,
978978
json: Any = UNDEFINED,
979979
) -> RequestHandler:
@@ -1050,13 +1050,13 @@ def expect_request(
10501050

10511051
def expect_oneshot_request(
10521052
self,
1053-
uri: Union[str, URIPattern, Pattern[str]],
1053+
uri: str | URIPattern | Pattern[str],
10541054
method: str = METHOD_ALL,
1055-
data: Union[str, bytes, None] = None,
1055+
data: str | bytes | None = None,
10561056
data_encoding: str = "utf-8",
1057-
headers: Optional[Mapping[str, str]] = None,
1058-
query_string: Union[None, QueryMatcher, str, bytes, Mapping] = None,
1059-
header_value_matcher: Optional[HVMATCHER_T] = None,
1057+
headers: Mapping[str, str] | None = None,
1058+
query_string: None | QueryMatcher | str | bytes | Mapping = None,
1059+
header_value_matcher: HVMATCHER_T | None = None,
10601060
json: Any = UNDEFINED,
10611061
) -> RequestHandler:
10621062
"""
@@ -1105,13 +1105,13 @@ def expect_oneshot_request(
11051105

11061106
def expect_ordered_request(
11071107
self,
1108-
uri: Union[str, URIPattern, Pattern[str]],
1108+
uri: str | URIPattern | Pattern[str],
11091109
method: str = METHOD_ALL,
1110-
data: Union[str, bytes, None] = None,
1110+
data: str | bytes | None = None,
11111111
data_encoding: str = "utf-8",
1112-
headers: Optional[Mapping[str, str]] = None,
1113-
query_string: Union[None, QueryMatcher, str, bytes, Mapping] = None,
1114-
header_value_matcher: Optional[HVMATCHER_T] = None,
1112+
headers: Mapping[str, str] | None = None,
1113+
query_string: None | QueryMatcher | str | bytes | Mapping = None,
1114+
header_value_matcher: HVMATCHER_T | None = None,
11151115
json: Any = UNDEFINED,
11161116
) -> RequestHandler:
11171117
"""
@@ -1289,9 +1289,9 @@ def _update_waiting_result(self) -> None:
12891289
@contextmanager
12901290
def wait(
12911291
self,
1292-
raise_assertions: Optional[bool] = None,
1293-
stop_on_nohandler: Optional[bool] = None,
1294-
timeout: Optional[float] = None,
1292+
raise_assertions: bool | None = None,
1293+
stop_on_nohandler: bool | None = None,
1294+
timeout: float | None = None,
12951295
):
12961296
"""Context manager to wait until the first of following event occurs: all ordered and oneshot handlers were
12971297
executed, unexpected request was received (if `stop_on_nohandler` is set to `True`), or time was out

tests/test_parse_qs.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
from __future__ import annotations
22

33
import urllib.parse
4-
from typing import List
5-
from typing import Tuple
64

75
import pytest
86

@@ -20,5 +18,5 @@
2018

2119

2220
@pytest.mark.parametrize("qs,expected", parse_qsl_semicolon_cases)
23-
def test_qsl(qs: str, expected: List[Tuple[bytes, bytes]]):
21+
def test_qsl(qs: str, expected: list[tuple[bytes, bytes]]):
2422
assert urllib.parse.parse_qsl(qs, keep_blank_values=True) == expected

tests/test_release.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
from dataclasses import dataclass
1010
from pathlib import Path
1111
from typing import Iterable
12-
from typing import Tuple
1312

1413
import pytest
1514
import toml
@@ -106,7 +105,7 @@ def version(pyproject) -> str:
106105
return pyproject["tool"]["poetry"]["version"]
107106

108107

109-
def version_to_tuple(version: str) -> Tuple:
108+
def version_to_tuple(version: str) -> tuple:
110109
return tuple([int(x) for x in version.split(".")])
111110

112111

0 commit comments

Comments
 (0)