From 2f061412550dae0e1309dd851e43a550f889534f Mon Sep 17 00:00:00 2001 From: Dave Pearson Date: Wed, 24 Jul 2024 16:46:34 +0100 Subject: [PATCH 1/5] :card_index: Bump pre-commit to Python 3.12 --- .pre-commit-config.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index e7b41ed..dc05f8c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -4,10 +4,10 @@ repos: hooks: - id: isort name: isort (python) - language_version: '3.10' + language_version: '3.12' args: ["--profile", "black", "--filter-files"] - repo: https://github.com/psf/black rev: 23.3.0 hooks: - id: black - language_version: python3.10 + language_version: python3.12 From 21b6360049da41bd633b0841bc21c6dca0ee276d Mon Sep 17 00:00:00 2001 From: Dave Pearson Date: Wed, 24 Jul 2024 16:47:01 +0100 Subject: [PATCH 2/5] :bug: Handle an item whose data comes back null for some reason https://news.ycombinator.com/item?id=41050801 is a job that I can see on HackerNews. Via https://hacker-news.firebaseio.com/v0/item/41050801.json (in other via their API) though it comes back as pure null. This change stops this causing a problem. --- oshit/hn/client.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/oshit/hn/client.py b/oshit/hn/client.py index 688c01e..8c3dc54 100644 --- a/oshit/hn/client.py +++ b/oshit/hn/client.py @@ -135,7 +135,12 @@ async def item(self, item_type: type[ItemType], item_id: int) -> ItemType: Returns: The item. """ - if isinstance(item := Loader.load(await self._raw_item(item_id)), item_type): + # If we can get the item but it comes back with no data at all... + if not (data := await self._raw_item(item_id)): + # ...as https://hacker-news.firebaseio.com/v0/item/41050801.json + # does for some reason, just make an empty version of the item. + return item_type() + if isinstance(item := Loader.load(data), item_type): return item raise ValueError( f"The item of ID '{item_id}' is of type '{item.item_type}', not {item_type.__name__}" From 2af3c5180a8f4b0272ee1d2938721ea6a26bf0fc Mon Sep 17 00:00:00 2001 From: Dave Pearson Date: Wed, 24 Jul 2024 16:49:06 +0100 Subject: [PATCH 3/5] :sparkles: Add a property that tests if an item looks valid --- oshit/hn/item/base.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/oshit/hn/item/base.py b/oshit/hn/item/base.py index 3627e28..431f693 100644 --- a/oshit/hn/item/base.py +++ b/oshit/hn/item/base.py @@ -71,6 +71,11 @@ def has_text(self) -> bool: """Does the item have any text?""" return bool(self.text.strip()) + @property + def looks_valid(self) -> bool: + """Does the item look valid?""" + return bool(self.item_id) and bool(self.item_type) + def __contains__(self, search_for: str) -> bool: return ( search_for.casefold() in self.by.casefold() From 8e448d1fa383a080bd997ddd2735e5637951a6d2 Mon Sep 17 00:00:00 2001 From: Dave Pearson Date: Wed, 24 Jul 2024 16:49:31 +0100 Subject: [PATCH 4/5] :sparkles: Filter out items that come back lacking data --- oshit/app/widgets/items.py | 1 + 1 file changed, 1 insertion(+) diff --git a/oshit/app/widgets/items.py b/oshit/app/widgets/items.py index b220135..ffd946c 100644 --- a/oshit/app/widgets/items.py +++ b/oshit/app/widgets/items.py @@ -229,6 +229,7 @@ def _redisplay(self) -> None: [ HackerNewsArticle(item, self.compact, number if self.numbered else None) for number, item in enumerate(self._items) + if item.looks_valid ] ) display.highlighted = remember From 27d7a8b952a21bb4d79aa6fa1982c80b8f1f26f0 Mon Sep 17 00:00:00 2001 From: Dave Pearson Date: Wed, 24 Jul 2024 16:52:50 +0100 Subject: [PATCH 5/5] :bookmark: v0.12.3 --- ChangeLog.md | 7 +++++++ oshit/__init__.py | 2 +- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/ChangeLog.md b/ChangeLog.md index 090d6f9..f8d461e 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -1,5 +1,12 @@ # OSHit ChangeLog +## v0.12.3 + +**Released: 2024-07-24** + +- Fixed a crash when the HackerNews API returns `null` for an otherwise + valid and available story. + ## v0.12.2 **Released: 2024-06-23** diff --git a/oshit/__init__.py b/oshit/__init__.py index ef6272f..5471e9f 100644 --- a/oshit/__init__.py +++ b/oshit/__init__.py @@ -7,7 +7,7 @@ __credits__ = ["Dave Pearson"] __maintainer__ = "Dave Pearson" __email__ = "davep@davep.org" -__version__ = "0.12.2" +__version__ = "0.12.3" __licence__ = "GPLv3+" ##############################################################################