Skip to content

Commit 1c853e2

Browse files
committed
Enable ruff PTH rule and migrate to pathlib
- Replace os.path with pathlib.Path in tests and utils - Use Path / operator with split segments for consistency - Remove stale ASYNC230 noqa comment
1 parent 50e1ff5 commit 1c853e2

File tree

3 files changed

+24
-36
lines changed

3 files changed

+24
-36
lines changed

pyproject.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ select = [
118118
"ERA",
119119
# flake8-pyi
120120
"PYI",
121+
# flake8-pathlib
122+
"PTH",
121123
]
122124
ignore = [
123125
"E501", # Line too long

tests/test_client.py

Lines changed: 16 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
"""Unit tests for the high-level OverkizClient behaviour and responses."""
22

3-
# ruff: noqa: ASYNC230, S106
3+
# ruff: noqa: S106
44
# S106: Test credentials use dummy values.
5-
# ASYNC230: Blocking open() is acceptable for reading test fixtures
65

76
from __future__ import annotations
87

98
import json
10-
import os
9+
from pathlib import Path
1110
from unittest.mock import AsyncMock, patch
1211

1312
import aiohttp
@@ -25,7 +24,7 @@
2524
from pyoverkiz.response_handler import check_response
2625
from pyoverkiz.utils import create_local_server_config
2726

28-
CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
27+
CURRENT_DIR = Path(__file__).resolve().parent
2928

3029

3130
class TestOverkizClient:
@@ -60,9 +59,7 @@ async def test_get_api_type_local(self, local_client: OverkizClient):
6059
@pytest.mark.asyncio
6160
async def test_get_devices_basic(self, client: OverkizClient):
6261
"""Ensure the client can fetch and parse the basic devices fixture."""
63-
with open(
64-
os.path.join(CURRENT_DIR, "devices.json"), encoding="utf-8"
65-
) as raw_devices:
62+
with (CURRENT_DIR / "devices.json").open(encoding="utf-8") as raw_devices:
6663
resp = MockResponse(raw_devices.read())
6764

6865
with patch.object(aiohttp.ClientSession, "get", return_value=resp):
@@ -81,8 +78,7 @@ async def test_fetch_events_basic(
8178
self, client: OverkizClient, fixture_name: str, event_length: int
8279
):
8380
"""Parameterised test that fetches events fixture and checks the expected count."""
84-
with open(
85-
os.path.join(CURRENT_DIR, "fixtures/event/" + fixture_name),
81+
with (CURRENT_DIR / "fixtures" / "event" / fixture_name).open(
8682
encoding="utf-8",
8783
) as raw_events:
8884
resp = MockResponse(raw_events.read())
@@ -94,8 +90,8 @@ async def test_fetch_events_basic(
9490
@pytest.mark.asyncio
9591
async def test_fetch_events_simple_cast(self, client: OverkizClient):
9692
"""Check that event state values from the cloud (strings) are cast to appropriate types."""
97-
with open(
98-
os.path.join(CURRENT_DIR, "fixtures/event/events.json"), encoding="utf-8"
93+
with (CURRENT_DIR / "fixtures" / "event" / "events.json").open(
94+
encoding="utf-8",
9995
) as raw_events:
10096
resp = MockResponse(raw_events.read())
10197

@@ -195,8 +191,7 @@ async def test_backoff_retries_on_concurrent_requests(
195191
@pytest.mark.asyncio
196192
async def test_fetch_events_casting(self, client: OverkizClient, fixture_name: str):
197193
"""Validate that fetched event states are cast to the expected Python types for each data type."""
198-
with open(
199-
os.path.join(CURRENT_DIR, "fixtures/event/" + fixture_name),
194+
with (CURRENT_DIR / "fixtures" / "event" / fixture_name).open(
200195
encoding="utf-8",
201196
) as raw_events:
202197
resp = MockResponse(raw_events.read())
@@ -264,8 +259,7 @@ async def test_get_setup(
264259
gateway_count: int,
265260
):
266261
"""Ensure setup parsing yields expected device and gateway counts and device metadata."""
267-
with open(
268-
os.path.join(CURRENT_DIR, "fixtures/setup/" + fixture_name),
262+
with (CURRENT_DIR / "fixtures" / "setup" / fixture_name).open(
269263
encoding="utf-8",
270264
) as setup_mock:
271265
resp = MockResponse(setup_mock.read())
@@ -316,8 +310,7 @@ async def test_get_setup(
316310
@pytest.mark.asyncio
317311
async def test_get_diagnostic_data(self, client: OverkizClient, fixture_name: str):
318312
"""Verify that diagnostic data can be fetched and is not empty."""
319-
with open(
320-
os.path.join(CURRENT_DIR, "fixtures/setup/" + fixture_name),
313+
with (CURRENT_DIR / "fixtures" / "setup" / fixture_name).open(
321314
encoding="utf-8",
322315
) as setup_mock:
323316
resp = MockResponse(setup_mock.read())
@@ -329,8 +322,7 @@ async def test_get_diagnostic_data(self, client: OverkizClient, fixture_name: st
329322
@pytest.mark.asyncio
330323
async def test_get_diagnostic_data_redacted_by_default(self, client: OverkizClient):
331324
"""Ensure diagnostics are redacted when no argument is provided."""
332-
with open(
333-
os.path.join(CURRENT_DIR, "fixtures/setup/setup_tahoma_1.json"),
325+
with (CURRENT_DIR / "fixtures" / "setup" / "setup_tahoma_1.json").open(
334326
encoding="utf-8",
335327
) as setup_mock:
336328
resp = MockResponse(setup_mock.read())
@@ -349,8 +341,7 @@ async def test_get_diagnostic_data_redacted_by_default(self, client: OverkizClie
349341
@pytest.mark.asyncio
350342
async def test_get_diagnostic_data_without_masking(self, client: OverkizClient):
351343
"""Ensure diagnostics can be returned without masking when requested."""
352-
with open(
353-
os.path.join(CURRENT_DIR, "fixtures/setup/setup_tahoma_1.json"),
344+
with (CURRENT_DIR / "fixtures" / "setup" / "setup_tahoma_1.json").open(
354345
encoding="utf-8",
355346
) as setup_mock:
356347
raw_setup = setup_mock.read()
@@ -546,8 +537,7 @@ async def test_check_response_exception_handling(
546537
):
547538
"""Ensure client raises the correct error for various error fixtures/status codes."""
548539
if fixture_name:
549-
with open(
550-
os.path.join(CURRENT_DIR, "fixtures/exceptions/" + fixture_name),
540+
with (CURRENT_DIR / "fixtures" / "exceptions" / fixture_name).open(
551541
encoding="utf-8",
552542
) as raw_events:
553543
resp = MockResponse(raw_events.read(), status_code)
@@ -563,8 +553,7 @@ async def test_get_setup_options(
563553
client: OverkizClient,
564554
):
565555
"""Check that setup options are parsed and return the expected number of Option instances."""
566-
with open(
567-
os.path.join(CURRENT_DIR, "fixtures/endpoints/setup-options.json"),
556+
with (CURRENT_DIR / "fixtures" / "endpoints" / "setup-options.json").open(
568557
encoding="utf-8",
569558
) as raw_events:
570559
resp = MockResponse(raw_events.read())
@@ -665,8 +654,7 @@ async def test_get_setup_option(
665654
instance: Option | None,
666655
):
667656
"""Verify retrieval of a single setup option by name, including non-existent options."""
668-
with open(
669-
os.path.join(CURRENT_DIR, "fixtures/endpoints/" + fixture_name),
657+
with (CURRENT_DIR / "fixtures" / "endpoints" / fixture_name).open(
670658
encoding="utf-8",
671659
) as raw_events:
672660
resp = MockResponse(raw_events.read())
@@ -696,8 +684,7 @@ async def test_get_action_groups(
696684
scenario_count: int,
697685
):
698686
"""Ensure action groups (scenarios) are parsed correctly and contain actions and commands."""
699-
with open(
700-
os.path.join(CURRENT_DIR, "fixtures/action_groups/" + fixture_name),
687+
with (CURRENT_DIR / "fixtures" / "action_groups" / fixture_name).open(
701688
encoding="utf-8",
702689
) as action_group_mock:
703690
resp = MockResponse(action_group_mock.read())

utils/mask_fixtures.py

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,23 @@
55

66
from __future__ import annotations
77

8-
import glob
98
import json
10-
import os
9+
from pathlib import Path
1110

1211
from pyoverkiz.obfuscate import obfuscate_sensitive_data
1312

1413
# only process .JSON files in folder.
15-
for filename in glob.glob(os.path.join("tests/fixtures/setup", "*.json")):
16-
with open(filename, encoding="utf-8") as input_file:
17-
print(f"Masking {filename}")
14+
for filepath in Path("tests/fixtures/setup").glob("*.json"):
15+
with filepath.open(encoding="utf-8") as input_file:
16+
print(f"Masking {filepath}")
1817

1918
try:
2019
file = json.loads(input_file.read())
2120
output = obfuscate_sensitive_data(file)
2221
except Exception as exception: # pylint: disable=broad-except
23-
print(f"Error while masking: {filename}")
22+
print(f"Error while masking: {filepath}")
2423
print(exception)
2524
continue
2625

27-
with open(filename, encoding="utf-8", mode="w") as output_file:
26+
with filepath.open(encoding="utf-8", mode="w") as output_file:
2827
json.dump(output, output_file, ensure_ascii=False, indent=4)

0 commit comments

Comments
 (0)