Skip to content

Commit d7ed02f

Browse files
committed
Fix some typing issues
1 parent a67c1a3 commit d7ed02f

5 files changed

Lines changed: 24 additions & 14 deletions

File tree

homeassistant_api/baseclient.py

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,9 +62,6 @@ def prepare_headers(
6262
"""Prepares and verifies dictionary headers."""
6363
if headers is None:
6464
return dict(self._headers)
65-
if not isinstance(headers, dict):
66-
msg = f"headers must be dict or dict subclass, not type {type(headers)!r}"
67-
raise TypeError(msg)
6865
return {**self._headers, **headers}
6966

7067
@staticmethod

homeassistant_api/client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ class Client(BaseClient):
4747
:param global_request_kwargs: Kwargs to pass to :func:`requests.request`. Optional.
4848
""" # pylint: disable=line-too-long
4949

50-
_session: Session | None
50+
_session: Session
5151

5252
def __init__(
5353
self,

homeassistant_api/models/domains.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def _build_from_json(cls, json: dict[str, Any], **model_kwargs: Any) -> Self:
5151
msg = "Missing services or domain attribute in json argument."
5252
raise ValueError(msg)
5353
domain = cls(domain_id=cast("str", json.get("domain")), **model_kwargs)
54-
services = cast("dict[str, dict[str, Any]]", json.get("services"))
54+
services = json.get("services")
5555
if not isinstance(services, dict):
5656
msg = f"Expected dict for services, got {type(services)}"
5757
raise TypeError(msg)

homeassistant_api/processing.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,18 @@ def _check_status(info: ResponseInfo, content: str) -> None:
5858

5959
def _extract_info(response: ResponseType) -> ResponseInfo:
6060
"""Extract status code, URL, and method from a response."""
61+
if response.status_code is None:
62+
msg = "Response is missing status code."
63+
raise ValueError(msg)
64+
if response.request is None:
65+
msg = "Response is missing request information."
66+
raise ValueError(msg)
67+
if response.url is None:
68+
msg = "Response is missing URL information."
69+
raise ValueError(msg)
6170
return ResponseInfo(
6271
status_code=response.status_code,
63-
url=str(response.url),
72+
url=response.url,
6473
method=response.request.method,
6574
)
6675

@@ -70,7 +79,7 @@ def _check_sync_status(response: ResponseType) -> None:
7079
info = _extract_info(response)
7180
if info.status_code in (HTTPStatus.OK, HTTPStatus.CREATED):
7281
return
73-
_check_status(info, content=response.text)
82+
_check_status(info, content=_parse_text(response))
7483

7584

7685
# --- Individual parse functions ---
@@ -87,6 +96,9 @@ def _parse_json(response: ResponseType) -> Any:
8796

8897
def _parse_text(response: ResponseType) -> str:
8998
"""Return the plaintext content of a sync response."""
99+
if response.text is None:
100+
msg = "Response is missing text content."
101+
raise MalformedDataError(msg)
90102
return response.text
91103

92104

@@ -104,9 +116,12 @@ def _parse_text(response: ResponseType) -> str:
104116

105117
def _parse_content(response: ResponseType) -> Any:
106118
"""Look up and call the appropriate parser by content-type."""
107-
mimetype = response.headers.get("content-type", "text/plain").split(";")[0]
108-
parser = _PARSERS.get(mimetype)
109-
if parser is None:
119+
content_type = response.headers.get("content-type", "text/plain")
120+
if isinstance(content_type, bytes):
121+
content_type = content_type.decode("utf-8")
122+
mimetype = str(content_type).split(";")[0].strip().lower()
123+
124+
if (parser := _PARSERS.get(mimetype)) is None:
110125
msg = f"No response processor found for mimetype {mimetype!r}."
111126
raise ProcessorNotFoundError(msg)
112127
return parser(response)

tests/test_errors.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
import niquests
99
import pytest
10-
from multidict import CIMultiDict
11-
from multidict import CIMultiDictProxy
1210

1311
from homeassistant_api import AsyncClient
1412
from homeassistant_api import AsyncWebsocketClient
@@ -165,7 +163,7 @@ def make_response(
165163
text=content,
166164
url="http://localhost/api/test",
167165
request=unittest.mock.Mock(method="GET"),
168-
headers=CIMultiDictProxy(CIMultiDict(headers)),
166+
headers=headers,
169167
json=unittest.mock.Mock(
170168
side_effect=json.JSONDecodeError("This is a fake message", "", 1),
171169
),
@@ -184,7 +182,7 @@ def make_async_response(
184182
text=content,
185183
url="http://localhost/api/test",
186184
request=unittest.mock.Mock(method="GET"),
187-
headers=CIMultiDictProxy(CIMultiDict(headers)),
185+
headers=headers,
188186
json=unittest.mock.Mock(
189187
side_effect=json.JSONDecodeError("This is a fake message", "", 1),
190188
),

0 commit comments

Comments
 (0)