From e2d8ae71573a3345faae81f3d82bf615842f67f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dato=20Sim=C3=B3?= Date: Sat, 31 Aug 2024 21:08:25 -0300 Subject: [PATCH 1/3] Catch/handle missing reply_parent_id in get_parent() --- bookwyrm/templatetags/status_display.py | 12 +++++++----- mypy.ini | 2 +- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/bookwyrm/templatetags/status_display.py b/bookwyrm/templatetags/status_display.py index cf526c099..96525ca22 100644 --- a/bookwyrm/templatetags/status_display.py +++ b/bookwyrm/templatetags/status_display.py @@ -34,11 +34,13 @@ def get_replies(status: models.Status) -> Any: @register.filter(name="parent") def get_parent(status: models.Status) -> Any: """get the reply parent for a status""" - return ( - models.Status.objects.filter(id=status.reply_parent_id) - .select_subclasses() - .first() - ) + if status.reply_parent_id: + return ( + models.Status.objects.filter(id=status.reply_parent_id) + .select_subclasses() + .first() + ) + return None @register.filter(name="boosted_status") diff --git a/mypy.ini b/mypy.ini index 567ca7d3f..dce5698c4 100644 --- a/mypy.ini +++ b/mypy.ini @@ -25,4 +25,4 @@ ignore_errors = False [mypy-bookwyrm.templatetags.*] ignore_errors = False allow_untyped_calls = True -disable_error_code = attr-defined, arg-type, misc +disable_error_code = attr-defined, arg-type From 4852f7ff1e8c6a43efb3e0634a602f5cbfc65f08 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dato=20Sim=C3=B3?= Date: Sun, 1 Sep 2024 02:21:10 -0300 Subject: [PATCH 2/3] Better typing for get_published_date() and get_isni_bio() --- bookwyrm/templatetags/status_display.py | 3 ++- bookwyrm/templatetags/utilities.py | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/bookwyrm/templatetags/status_display.py b/bookwyrm/templatetags/status_display.py index 96525ca22..430fba198 100644 --- a/bookwyrm/templatetags/status_display.py +++ b/bookwyrm/templatetags/status_display.py @@ -1,4 +1,5 @@ """ template filters """ +import datetime from typing import Any, Optional from dateutil.relativedelta import relativedelta from django import template @@ -55,7 +56,7 @@ def get_boosted(boost: models.Boost) -> Any: @register.filter(name="published_date") -def get_published_date(date: str) -> Any: +def get_published_date(date: datetime.datetime) -> str | None: """less verbose combo of humanize filters""" if not date: return "" diff --git a/bookwyrm/templatetags/utilities.py b/bookwyrm/templatetags/utilities.py index 0aabb94f7..f1f7c4bdd 100644 --- a/bookwyrm/templatetags/utilities.py +++ b/bookwyrm/templatetags/utilities.py @@ -93,7 +93,7 @@ def get_book_cover_thumbnail( @register.filter(name="get_isni_bio") -def get_isni_bio(existing: int, author: Author) -> str: +def get_isni_bio(existing: list[str], author: Author) -> str: """Returns the isni bio string if an existing author has an isni listed""" auth_isni = re.sub(r"\D", "", str(author.isni)) if len(existing) == 0: From 93396c0f2cefacee6d1c53ee56c6f14b2a7c0882 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dato=20Sim=C3=B3?= Date: Sun, 1 Sep 2024 02:21:10 -0300 Subject: [PATCH 3/3] Use typing.ParamSpec to type get_or_set() --- bookwyrm/utils/cache.py | 15 +++++++++------ mypy.ini | 2 +- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/bookwyrm/utils/cache.py b/bookwyrm/utils/cache.py index 5e896e621..df11f16de 100644 --- a/bookwyrm/utils/cache.py +++ b/bookwyrm/utils/cache.py @@ -1,17 +1,20 @@ """ Custom handler for caching """ -from typing import Any, Callable, Tuple, Union +from typing import Callable, Optional, ParamSpec, TypeVar, cast from django.core.cache import cache +Args = ParamSpec("Args") +Ret = TypeVar("Ret") + def get_or_set( cache_key: str, - function: Callable[..., Any], - *args: Tuple[Any, ...], - timeout: Union[float, None] = None -) -> Any: + function: Callable[Args, Ret], + *args: Args.args, + timeout: Optional[float] = None +) -> Ret: """Django's built-in get_or_set isn't cutting it""" - value = cache.get(cache_key) + value = cast(Optional[Ret], cache.get(cache_key)) if value is None: value = function(*args) cache.set(cache_key, value, timeout=timeout) diff --git a/mypy.ini b/mypy.ini index dce5698c4..ef5d2d78f 100644 --- a/mypy.ini +++ b/mypy.ini @@ -25,4 +25,4 @@ ignore_errors = False [mypy-bookwyrm.templatetags.*] ignore_errors = False allow_untyped_calls = True -disable_error_code = attr-defined, arg-type +disable_error_code = attr-defined