Skip to content

Commit

Permalink
mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
zxdavb committed Apr 21, 2024
1 parent 52bcecc commit 8c27a98
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 45 deletions.
5 changes: 2 additions & 3 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -103,15 +103,14 @@

disable_error_code = [
"arg-type", # 7
"index", # 6
"no-any-return", # 5
"index", # 18
"no-untyped-def", # 16
"type-arg", # 15
]

#
[[tool.mypy.overrides]]
module = "tests_rf.mocked_server.aiohttp"
module = "tests_rf.mocked_server.*"

disable_error_code = [
"arg-type", # 2 (aiohttp,vendor)
Expand Down
6 changes: 4 additions & 2 deletions tests/tests/test_schedules.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import json
from pathlib import Path

import voluptuous as vol

from evohomeasync2.schema.schedule import (
SCH_GET_SCHEDULE_DHW,
SCH_GET_SCHEDULE_ZONE,
Expand All @@ -22,13 +24,13 @@
WORK_DIR = Path(f"{TEST_DIR}/schedules")


def _test_schedule_schema(file_name: str, schema) -> dict:
def _test_schedule_schema(file_name: str, schema: vol.Schema) -> dict:
def read_dict_from_file(file_name: str):
with open(WORK_DIR.joinpath(file_name)) as f:
data: dict = json.load(f)
return data

return schema(read_dict_from_file(file_name))
return schema(read_dict_from_file(file_name)) # type: ignore[no-any-return]


def _test_schema_schedule_dhw() -> None:
Expand Down
72 changes: 38 additions & 34 deletions tests/tests_rf/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ async def should_work_v1(
json: dict | None = None,
content_type: str | None = "application/json",
schema: vol.Schema | None = None,
) -> dict | list:
) -> dict | list | str:
"""Make a request that is expected to succeed."""

response: aiohttp.ClientResponse
Expand All @@ -85,9 +85,12 @@ async def should_work_v1(

assert response.content_type == content_type, content

if schema: # and response.content_type == "application/json"
return schema(content)
return content
if response.content_type != "application/json":
assert isinstance(content, str), content # mypy
return content

assert isinstance(content, dict | list), content # mypy
return schema(content) if schema else content


async def should_fail_v1(
Expand All @@ -97,7 +100,7 @@ async def should_fail_v1(
json: dict | None = None,
content_type: str | None = "application/json",
status: HTTPStatus | None = None,
) -> aiohttp.ClientResponse:
) -> dict | list | str | None:
"""Make a request that is expected to fail."""

response: aiohttp.ClientResponse
Expand All @@ -107,13 +110,13 @@ async def should_fail_v1(
response = await evo.broker._make_request(method, url, data=json)
response.raise_for_status()

except aiohttp.ClientResponseError as exc:
assert exc.status == status, exc.status
except aiohttp.ClientResponseError as err:
assert err.status == status, err.status
else:
assert False, response.status

if _DEBUG_DISABLE_STRICT_ASSERTS:
return response
return None

# TODO: perform this transform in the broker
if response.content_type == "application/json":
Expand All @@ -127,8 +130,12 @@ async def should_fail_v1(
assert "message" in content, content
elif isinstance(content, list):
assert "message" in content[0], content[0]
elif isinstance(content, str):
pass
else:
assert False, response.content_type

return content
return content # type: ignore[no-any-return]


# version 2 helpers ###################################################################
Expand Down Expand Up @@ -202,24 +209,24 @@ async def should_work(
json: dict | None = None,
content_type: str | None = "application/json",
schema: vol.Schema | None = None,
) -> dict | list:
) -> dict | list | str:
"""Make a request that is expected to succeed."""

response: aiohttp.ClientResponse

response, content = await evo.broker._client(
method, f"{URL_BASE_2}/{url}", json=json
)

response.raise_for_status()

assert response.content_type == content_type, content

if schema:
return schema(content)
if response.content_type != "application/json":
assert isinstance(content, str), content # mypy
return content

assert isinstance(content, dict | list), content
return content
assert isinstance(content, dict | list), content # mypy
return schema(content) if schema else content


async def should_fail(
Expand All @@ -229,54 +236,50 @@ async def should_fail(
json: dict | None = None,
content_type: str | None = "application/json",
status: HTTPStatus | None = None,
) -> aiohttp.ClientResponse:
) -> dict | list | str | None:
"""Make a request that is expected to fail."""

response: aiohttp.ClientResponse

# if method == HTTPMethod.GET:
# content = await evo.broker.get(
# else:
# content = await evo.broker.put(

response, content = await evo.broker._client(
method, f"{URL_BASE_2}/{url}", json=json
)

try:
response.raise_for_status()
except aiohttp.ClientResponseError as exc:
assert exc.status == status, exc.status

except aiohttp.ClientResponseError as err:
assert err.status == status, err.status
else:
assert False, response.status

if _DEBUG_DISABLE_STRICT_ASSERTS:
return response
return None

assert response.content_type == content_type, response.content_type

if isinstance(content, list):
if isinstance(content, dict):
assert status in (
HTTPStatus.NOT_FOUND,
HTTPStatus.METHOD_NOT_ALLOWED,
), content
assert "message" in content, content # sometimes "code" too

elif isinstance(content, list):
assert status in (
HTTPStatus.BAD_REQUEST,
HTTPStatus.NOT_FOUND, # CommTaskNotFound
HTTPStatus.UNAUTHORIZED,
), content
assert "message" in content[0], content[0] # sometimes "code" too

elif isinstance(content, dict):
assert status in (
HTTPStatus.NOT_FOUND,
HTTPStatus.METHOD_NOT_ALLOWED,
), content
assert "message" in content, content # sometimes "code" too

elif isinstance(content, str): # 404
assert status in (HTTPStatus.NOT_FOUND,), status

else:
assert False, response.status
assert False, response.content_type

return response
return content


async def wait_for_comm_task(
Expand All @@ -293,6 +296,7 @@ async def wait_for_comm_task(
start_time = dt.now()
while True:
response = await should_work(evo, HTTPMethod.GET, url)
assert isinstance(response, dict | list), response
if response["state"] == "Succeeded": # type: ignore[call-overload]
return True
if (dt.now() - start_time).total_seconds() > timeout:
Expand Down
10 changes: 6 additions & 4 deletions tests/tests_rf/test_v2_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ async def _test_task_id(evo: evo2.EvohomeClient) -> None:

#
# PART 0: Get initial state...
old_status = await should_work(evo, HTTPMethod.GET, GET_URL) # HTTP 200
old_status = await should_work(evo, HTTPMethod.GET, GET_URL)
assert isinstance(old_status, dict) # mypy
# {
# 'dhwId': '3933910',
# 'temperatureStatus': {'isAvailable': False},
Expand All @@ -80,9 +81,9 @@ async def _test_task_id(evo: evo2.EvohomeClient) -> None:
# } # HTTP 200

old_mode = {
SZ_MODE: old_status[SZ_STATE_STATUS][SZ_MODE], # type: ignore[call-overload]
SZ_STATE: old_status[SZ_STATE_STATUS][SZ_STATE], # type: ignore[call-overload]
SZ_UNTIL_TIME: old_status[SZ_STATE_STATUS].get(SZ_UNTIL), # type: ignore[call-overload]
SZ_MODE: old_status[SZ_STATE_STATUS][SZ_MODE],
SZ_STATE: old_status[SZ_STATE_STATUS][SZ_STATE],
SZ_UNTIL_TIME: old_status[SZ_STATE_STATUS].get(SZ_UNTIL),
} # NOTE: untilTime/until

#
Expand All @@ -95,6 +96,7 @@ async def _test_task_id(evo: evo2.EvohomeClient) -> None:
}

result = await should_work(evo, HTTPMethod.PUT, PUT_URL, json=new_mode)
assert isinstance(result, dict | list) # mypy
# {'id': '840367013'} # HTTP 201/Created

task_id = result[0]["id"] if isinstance(result, list) else result["id"]
Expand Down
4 changes: 2 additions & 2 deletions tests/tests_rf/test_v2_urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ async def _test_schedule(evo: evo2.EvohomeClient) -> None:

temp = schedule[SZ_DAILY_SCHEDULES][0][SZ_SWITCHPOINTS][0][SZ_HEAT_SETPOINT] # type: ignore[call-overload]

schedule[SZ_DAILY_SCHEDULES][0][SZ_SWITCHPOINTS][0][SZ_HEAT_SETPOINT] = temp + 1 # type: ignore[call-overload]
schedule[SZ_DAILY_SCHEDULES][0][SZ_SWITCHPOINTS][0][SZ_HEAT_SETPOINT] = temp + 1 # type: ignore[call-overload,operator]
_ = await should_work(
evo,
HTTPMethod.PUT,
Expand All @@ -285,7 +285,7 @@ async def _test_schedule(evo: evo2.EvohomeClient) -> None:
schedule = await should_work(evo, HTTPMethod.GET, url, schema=SCH_GET_SCHEDULE)
assert (
schedule[SZ_DAILY_SCHEDULES][0][SZ_SWITCHPOINTS][0][SZ_HEAT_SETPOINT] # type: ignore[call-overload]
== temp + 1
== temp + 1 # type: ignore[operator]
)

schedule[SZ_DAILY_SCHEDULES][0][SZ_SWITCHPOINTS][0][SZ_HEAT_SETPOINT] = temp # type: ignore[call-overload]
Expand Down

0 comments on commit 8c27a98

Please sign in to comment.