Skip to content

Commit fa223e6

Browse files
committed
fix: use Path.stat mock compatible with Python 3.10
On Python 3.10, Path.stat() doesn't route through os.stat in a patchable way. Use patch.object(Path, "stat", new=func) instead, which preserves self-binding via descriptor protocol and works across all Python versions.
1 parent 1e79106 commit fa223e6

1 file changed

Lines changed: 12 additions & 18 deletions

File tree

tests/test_file_read_errors.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
gracefully, especially for cloud-synced filesystems (iCloud, Dropbox).
55
"""
66

7-
import signal
87
from pathlib import Path
98
from unittest.mock import patch
109

@@ -58,8 +57,6 @@ def test_timeout_on_read(self, tmp_path: Path) -> None:
5857
test_file.write_text("content", encoding="utf-8")
5958

6059
def mock_read_text(*args, **kwargs):
61-
# Simulate a file that hangs by raising SIGALRM
62-
signal.alarm(0) # Cancel any existing alarm
6360
raise FileReadTimeoutError("Simulated timeout")
6461

6562
with (
@@ -220,18 +217,17 @@ def test_index_file_timeout_on_stat(self, clean_db, tmp_path: Path) -> None:
220217
test_file = tmp_path / "slow.md"
221218
test_file.write_text("content", encoding="utf-8")
222219

223-
# Mock os.stat since it's what pathlib.Path.stat() uses internally
224-
import os
220+
# Patch Path.stat directly with new= to work across Python versions.
221+
# Using new= preserves self-binding (functions are descriptors).
222+
original_stat = Path.stat
225223

226-
original_os_stat = os.stat
227-
228-
def os_stat_side_effect(path, *args, **kwargs):
229-
if str(path) == str(test_file):
224+
def patched_stat(self, *args, **kwargs):
225+
if str(self) == str(test_file):
230226
raise TimeoutError("stat timeout")
231-
return original_os_stat(path, *args, **kwargs)
227+
return original_stat(self, *args, **kwargs)
232228

233229
with (
234-
patch("os.stat", side_effect=os_stat_side_effect),
230+
patch.object(Path, "stat", new=patched_stat),
235231
pytest.raises(FileReadTimeoutError),
236232
):
237233
service.index_file(test_file)
@@ -248,17 +244,15 @@ def test_index_file_stat_os_error(self, clean_db, tmp_path: Path) -> None:
248244
test_file = tmp_path / "broken.md"
249245
test_file.write_text("content", encoding="utf-8")
250246

251-
import os
252-
253-
original_os_stat = os.stat
247+
original_stat = Path.stat
254248

255-
def os_stat_side_effect(path, *args, **kwargs):
256-
if str(path) == str(test_file):
249+
def patched_stat(self, *args, **kwargs):
250+
if str(self) == str(test_file):
257251
raise OSError("Disk error")
258-
return original_os_stat(path, *args, **kwargs)
252+
return original_stat(self, *args, **kwargs)
259253

260254
with (
261-
patch("os.stat", side_effect=os_stat_side_effect),
255+
patch.object(Path, "stat", new=patched_stat),
262256
pytest.raises(FileReadError),
263257
):
264258
service.index_file(test_file)

0 commit comments

Comments
 (0)