Skip to content

Commit 91a9f59

Browse files
committed
fix: replace os.path with pathlib for file handling and improve readability
1 parent dbbaae4 commit 91a9f59

4 files changed

Lines changed: 18 additions & 13 deletions

File tree

examples/getting-started/01_simple_tts.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
- Audio file contains the spoken text
1919
"""
2020

21-
import os
21+
from pathlib import Path
2222

2323
from fishaudio import FishAudio
2424
from fishaudio.utils import save
@@ -44,7 +44,7 @@ def main():
4444
save(audio, output_file)
4545

4646
print(f"✓ Audio saved to {output_file}")
47-
print(f" File size: {os.path.getsize(output_file) / 1024:.2f} KB")
47+
print(f" File size: {Path(output_file).stat().st_size / 1024:.2f} KB")
4848

4949

5050
if __name__ == "__main__":

src/fishaudio/utils/save.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Audio saving utility."""
22

33
from collections.abc import Iterable
4+
from pathlib import Path
45
from typing import Union
56

67

@@ -32,5 +33,5 @@ def save(audio: Union[bytes, Iterable[bytes]], filename: str) -> None:
3233
audio = b"".join(audio)
3334

3435
# Write to file
35-
with open(filename, "wb") as f:
36+
with Path(filename).open("wb") as f:
3637
f.write(audio)

tests/unit/test_core.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,11 @@ def test_init_with_api_key(self, mock_api_key, mock_base_url):
101101
assert wrapper.base_url == mock_base_url
102102

103103
def test_init_without_api_key_raises(self):
104-
with patch.dict("os.environ", {}, clear=True):
105-
with pytest.raises(ValueError, match="API key must be provided"):
106-
ClientWrapper()
104+
with (
105+
patch.dict("os.environ", {}, clear=True),
106+
pytest.raises(ValueError, match="API key must be provided"),
107+
):
108+
ClientWrapper()
107109

108110
def test_init_with_env_var(self, mock_api_key):
109111
with patch.dict("os.environ", {"FISH_API_KEY": mock_api_key}):
@@ -134,9 +136,11 @@ def test_init_with_api_key(self, mock_api_key, mock_base_url):
134136
assert wrapper.base_url == mock_base_url
135137

136138
def test_init_without_api_key_raises(self):
137-
with patch.dict("os.environ", {}, clear=True):
138-
with pytest.raises(ValueError, match="API key must be provided"):
139-
AsyncClientWrapper()
139+
with (
140+
patch.dict("os.environ", {}, clear=True),
141+
pytest.raises(ValueError, match="API key must be provided"),
142+
):
143+
AsyncClientWrapper()
140144

141145
def test_get_headers(self, mock_api_key):
142146
wrapper = AsyncClientWrapper(api_key=mock_api_key)

tests/unit/test_utils.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,20 +16,20 @@ def test_save_bytes(self):
1616
"""Test saving bytes to file."""
1717
audio = b"fake audio data"
1818

19-
with patch("builtins.open", mock_open()) as m:
19+
with patch("pathlib.Path.open", mock_open()) as m:
2020
save(audio, "output.mp3")
2121

22-
m.assert_called_once_with("output.mp3", "wb")
22+
m.assert_called_once_with("wb")
2323
m().write.assert_called_once_with(audio)
2424

2525
def test_save_iterator(self):
2626
"""Test saving iterator to file."""
2727
audio = iter([b"chunk1", b"chunk2", b"chunk3"])
2828

29-
with patch("builtins.open", mock_open()) as m:
29+
with patch("pathlib.Path.open", mock_open()) as m:
3030
save(audio, "output.mp3")
3131

32-
m.assert_called_once_with("output.mp3", "wb")
32+
m.assert_called_once_with("wb")
3333
# Should consolidate chunks
3434
m().write.assert_called_once_with(b"chunk1chunk2chunk3")
3535

0 commit comments

Comments
 (0)