From 79ef55b67d28e05c710a6270b5e42c7ad5ca0b29 Mon Sep 17 00:00:00 2001 From: Raventric <78981416+Ravencentric@users.noreply.github.com> Date: Thu, 20 Jun 2024 13:20:44 +0530 Subject: [PATCH] fix: return None for empty descriptions and information fields Previously it just returned Nyaa's placeholder value --- src/pynyaa/_models.py | 28 +++++++++++++++++++++++++--- tests/test_async.py | 4 ++-- tests/test_sync.py | 4 ++-- 3 files changed, 29 insertions(+), 7 deletions(-) diff --git a/src/pynyaa/_models.py b/src/pynyaa/_models.py index e6786ad..96e1591 100644 --- a/src/pynyaa/_models.py +++ b/src/pynyaa/_models.py @@ -2,7 +2,7 @@ from datetime import datetime -from pydantic import AnyUrl, BaseModel, ConfigDict, HttpUrl +from pydantic import AnyUrl, BaseModel, ConfigDict, HttpUrl, field_validator from torf import Torrent from ._enums import NyaaCategory @@ -51,7 +51,7 @@ class NyaaTorrentPage(ParentModel): submitter: Submitter """User who submitted the torrent.""" - information: str + information: str | None """Information about the torrent.""" seeders: int @@ -78,7 +78,7 @@ class NyaaTorrentPage(ParentModel): This is a current limitation that I don't know how to work around. """ - description: str + description: str | None """Torrent description.""" torrent_file: HttpUrl @@ -102,6 +102,28 @@ class NyaaTorrentPage(ParentModel): representing the data stored in the `.torrent` file. """ + @field_validator("information") + @classmethod + def replace_empty_info_with_none(cls, information: str) -> str | None: + """ + If the information field is empty, Nyaa replaces it with a placeholder value. + This replaces said placeholder value with `None`. + """ + if information == "No information.": + return None + return information + + @field_validator("description") + @classmethod + def replace_empty_desc_with_none(cls, description: str) -> str | None: + """ + If the description field is empty, Nyaa replaces it with a placeholder value. + This replaces said placeholder value with `None`. + """ + if description == "#### No description.": + return None + return description + __all__ = [ "Submitter", diff --git a/tests/test_async.py b/tests/test_async.py index d6552b8..cfc738d 100644 --- a/tests/test_async.py +++ b/tests/test_async.py @@ -70,5 +70,5 @@ async def test_nyaa_banned_and_trusted() -> None: async def test_nyaa_empty_desc_info() -> None: nyaa = await client.get("https://nyaa.si/view/1586776") - assert nyaa.information == "No information." - assert nyaa.description == "#### No description." + assert nyaa.information is None + assert nyaa.description is None diff --git a/tests/test_sync.py b/tests/test_sync.py index 2e8659a..a4e3f20 100644 --- a/tests/test_sync.py +++ b/tests/test_sync.py @@ -70,5 +70,5 @@ def test_nyaa_banned_and_trusted() -> None: def test_nyaa_empty_desc_info() -> None: nyaa = client.get("https://nyaa.si/view/1586776") - assert nyaa.information == "No information." - assert nyaa.description == "#### No description." + assert nyaa.information is None + assert nyaa.description is None