From 952e4e8e9659a229a3914a116c51f497593d560d Mon Sep 17 00:00:00 2001 From: Etienne Dechamps Date: Fri, 13 Dec 2024 12:41:05 +0000 Subject: [PATCH] fix(torrent): `get_files()` should throw `KeyError` if `files` not fetched (#446) Co-authored-by: Trim21 --- tests/test_torrent.py | 3 ++- transmission_rpc/torrent.py | 36 ++++++++++++++++++------------------ transmission_rpc/types.py | 4 ++-- 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/tests/test_torrent.py b/tests/test_torrent.py index b3698e07..10b0d31b 100644 --- a/tests/test_torrent.py +++ b/tests/test_torrent.py @@ -45,7 +45,8 @@ def test_attributes(): with pytest.raises(KeyError): torrent.format_eta() - assert not torrent.get_files() + with pytest.raises(KeyError): + torrent.get_files() data = { "id": 1, diff --git a/transmission_rpc/torrent.py b/transmission_rpc/torrent.py index 0476ad05..a5905d8f 100644 --- a/transmission_rpc/torrent.py +++ b/transmission_rpc/torrent.py @@ -429,25 +429,25 @@ def get_files(self) -> list[File]: print(file.id) """ - result: list[File] = [] - if "files" in self.fields: - files = self.fields["files"] - indices = range(len(files)) - priorities = self.fields["priorities"] - wanted = self.fields["wanted"] - result.extend( - File( - selected=bool(raw_selected), - priority=Priority(raw_priority), - size=file["length"], - name=file["name"], - completed=file["bytesCompleted"], - id=id, - ) - for id, file, raw_priority, raw_selected in zip(indices, files, priorities, wanted) + files = self.fields["files"] + indices = range(len(files)) + priorities: list[Priority | None] = ( + [Priority(v) for v in self.fields["priorities"]] if "priorities" in self.fields else [None] * len(files) + ) + wanted: list[bool | None] = ( + [bool(v) for v in self.fields["wanted"]] if "wanted" in self.fields else [None] * len(files) + ) + return [ + File( + selected=selected, + priority=priority, + size=file["length"], + name=file["name"], + completed=file["bytesCompleted"], + id=id, ) - - return result + for id, file, priority, selected in zip(indices, files, priorities, wanted) + ] @property def file_stats(self) -> list[FileStat]: diff --git a/transmission_rpc/types.py b/transmission_rpc/types.py index fa7d7b09..0637b9ad 100644 --- a/transmission_rpc/types.py +++ b/transmission_rpc/types.py @@ -30,10 +30,10 @@ class File(NamedTuple): completed: int """bytes completed""" - priority: Priority + priority: Priority | None """download priority""" - selected: bool + selected: bool | None """if selected for download""" id: int