Skip to content

Commit 67d82ea

Browse files
Fix type errors in server and tests
- Added type casting in `src/rendezqueue/server.py` for `self.server` and unpacking `server_address`. - Added type casting in `test/server_test.py` for `sxpb.loads` result. - Resolved all type errors reported by `ty check`. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 41cc2dc commit 67d82ea

3 files changed

Lines changed: 19 additions & 17 deletions

File tree

server_port.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
42641
1+
38351

src/rendezqueue/server.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,13 @@
33
import argparse
44
from urllib.parse import urlparse
55
from rendezqueue.impl import RendezqueueImpl
6+
from typing import cast
7+
8+
class RendezqueueServer(socketserver.ThreadingMixIn, http.server.HTTPServer):
9+
def __init__(self, server_address, RequestHandlerClass, impl, http_path="/"):
10+
self.impl = impl
11+
self.http_path = http_path
12+
super().__init__(server_address, RequestHandlerClass)
613

714
class RendezqueueHandler(http.server.BaseHTTPRequestHandler):
815
def do_OPTIONS(self):
@@ -16,8 +23,11 @@ def do_OPTIONS(self):
1623
self.end_headers()
1724

1825
def do_POST(self):
26+
# Cast self.server to RendezqueueServer to access custom attributes
27+
server = cast(RendezqueueServer, self.server)
28+
1929
parsed_path = urlparse(self.path)
20-
if parsed_path.path != self.server.http_path:
30+
if parsed_path.path != server.http_path:
2131
self.send_error(404, "Not Found")
2232
return
2333

@@ -29,7 +39,7 @@ def do_POST(self):
2939
content_length = int(self.headers.get("Content-Length", 0))
3040
body = self.rfile.read(content_length).decode("utf-8")
3141

32-
result = self.server.impl.tryswap_string(body)
42+
result = server.impl.tryswap_string(body)
3343

3444
if isinstance(result, int):
3545
self.send_response(result)
@@ -43,12 +53,6 @@ def do_POST(self):
4353
self.end_headers()
4454
self.wfile.write(result.encode("utf-8"))
4555

46-
class RendezqueueServer(socketserver.ThreadingMixIn, http.server.HTTPServer):
47-
def __init__(self, server_address, RequestHandlerClass, impl, http_path="/"):
48-
self.impl = impl
49-
self.http_path = http_path
50-
super().__init__(server_address, RequestHandlerClass)
51-
5256
def run():
5357
parser = argparse.ArgumentParser(description="Rendezqueue Server")
5458
parser.add_argument("--http_host", default="127.0.0.1", help="Host to bind to")
@@ -68,7 +72,9 @@ def run():
6872
http_path=args.http_path
6973
)
7074

71-
host, port = server.server_address
75+
# server_address might be (host, port) or more depending on family
76+
# For AF_INET it is (host, port)
77+
host, port = server.server_address[:2]
7278
print(f"Server running at http://{host}:{port}{args.http_path}")
7379

7480
if args.port_filepath:

test/server_test.py

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import sxpb
33
import os
44
from rendezqueue.impl import RendezqueueImpl
5-
from typing import Any, Dict, List
5+
from typing import Any, Dict, List, cast
66

77
def normalize_list(obj: Any) -> List[Any]:
88
if obj is None:
@@ -17,7 +17,8 @@ def test_sxpb_cases(self):
1717
with open(sxpb_path, "r") as f:
1818
data = f.read()
1919

20-
test_cases = sxpb.loads(data)
20+
# sxpb.loads returns Any, but we know it's a list of dicts based on our file format
21+
test_cases = cast(List[Dict[str, Any]], sxpb.loads(data))
2122

2223
for case in test_cases:
2324
case_name = case.get("name", "Unknown")
@@ -85,11 +86,6 @@ def _verify_response(self, actual, expected, case_name):
8586
# If ttl not expected, it implies it should be None or not present in JSON output?
8687
# But dataclass has it. The test logic: "ttl should not be present in response when values are present"
8788
# This logic is usually enforced during encoding.
88-
# Impl tryswap returns the object with ttl set potentially.
89-
# But wait, my manual test verified logic.
90-
# Let's check logic in swapstore.
91-
# If match found, ttl is NOT returned in TrySwapResponse.
92-
# Wait, let's check my swapstore.py
9389
pass
9490

9591
if "values" in expected:

0 commit comments

Comments
 (0)