Skip to content

Commit cf01854

Browse files
Tsunami Teamcopybara-github
authored andcommitted
Improve type hints and None safety checks.
PiperOrigin-RevId: 856633232 Change-Id: I581ddf712bebc1fc48bf1bcd7820d6c58626021c
1 parent c8751e1 commit cf01854

File tree

2 files changed

+13
-12
lines changed

2 files changed

+13
-12
lines changed

plugin_server/py/common/net/http/http_response.py

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
"""HTTP response utility."""
22

33
import abc
4+
from collections.abc import Mapping
45
import json
5-
from typing import Any, Optional
6+
from typing import Any
67
from common.net.http.http_headers import HttpHeaders
78
from common.net.http.http_status import HttpStatus
89

@@ -20,19 +21,18 @@ class HttpResponse(metaclass=abc.ABCMeta):
2021
"""
2122

2223
def __init__(self):
23-
self.status: HttpStatus = None
24-
self.url: Optional[str] = None
25-
self.headers: HttpHeaders = None
26-
self.body: Optional[bytes] = None
24+
self.status: HttpStatus | None = None
25+
self.url: str | None = None
26+
self.headers: HttpHeaders | None = None
27+
self.body: bytes | None = None
2728

2829
def body_string(self) -> str:
2930
"""Get the body data as a UTF-8 encoded string."""
30-
try:
31-
return self.body.decode('utf-8')
32-
except AttributeError:
31+
if self.body is None:
3332
return ''
33+
return self.body.decode('utf-8')
3434

35-
def body_json(self) -> Optional[dict[str, Any]]:
35+
def body_json(self) -> Mapping[str, Any] | None:
3636
"""Parse the response body as JSON. Returns null if parsing failed."""
3737
try:
3838
return json.loads(self.body_string())
@@ -71,7 +71,7 @@ def set_headers(self, headers: HttpHeaders):
7171
self.http_response.headers = headers
7272
return self
7373

74-
def set_response_body(self, response_body: Optional[bytes]):
74+
def set_response_body(self, response_body: bytes | None):
7575
"""Set the body for the response."""
7676
self.http_response.body = response_body
7777
return self

plugin_server/py/plugin/tcs_client.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,13 @@ def _send_polling_request(
104104
request = self._build_polling_request(secret_string)
105105
try:
106106
response = self.http_client.send(request)
107-
if response.status.is_success():
107+
if response.status and response.status.is_success():
108108
return json_format.Parse(
109109
response.body_string(), polling_pb2.PollingResult()
110110
)
111111
else:
112-
logging.info('OOB server returned %s.', response.status.code)
112+
code = response.status.code if response.status else 'UNKNOWN'
113+
logging.info('OOB server returned %s.', code)
113114
except (json_format.ParseError, ValueError):
114115
logging.exception('Polling request failed.')
115116
return None

0 commit comments

Comments
 (0)