Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .githooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ echo "Running ruff format..."
echo "Running ruff check..."
.venv/bin/ruff check docking/ tests/
echo "Running ty..."
.venv/bin/ty check docking/
PYTHONPATH="$(pwd):$PYTHONPATH" .venv/bin/ty check docking/
echo "Checking i18n template sync..."
bash tools/i18n.sh --check-pot-sync
echo "Checking i18n completeness..."
Expand Down
2 changes: 1 addition & 1 deletion docking/applets/certwatch/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def build_tooltip(
loading: bool = False,
error: str | None = None,
now: datetime | None = None,
updated_at: object | None = None,
updated_at: datetime | str | None = None,
cadence_seconds: int | None = None,
) -> str:
"""Full tooltip text: header + one line per domain."""
Expand Down
9 changes: 5 additions & 4 deletions docking/applets/deskpresence/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from dataclasses import dataclass, field
from datetime import date, datetime, timedelta, timezone
from enum import Enum
from typing import Any
from typing import Any, cast

from docking.i18n import _

Expand Down Expand Up @@ -230,7 +230,8 @@ def _parse_history(raw: object, *, today: date) -> list[DayEntry]:
for rd in raw:
if not isinstance(rd, dict):
continue
iso = str(rd.get("date", ""))
entry = cast(dict[str, Any], rd)
iso = str(entry.get("date", ""))
if not iso:
continue
try:
Expand All @@ -242,8 +243,8 @@ def _parse_history(raw: object, *, today: date) -> list[DayEntry]:
# anything older than the rolling window.
continue
try:
at_desk = float(rd.get("at_desk_seconds", 0.0))
away = float(rd.get("away_seconds", 0.0))
at_desk = float(entry.get("at_desk_seconds", 0.0))
away = float(entry.get("away_seconds", 0.0))
except (TypeError, ValueError):
continue
parsed.append(
Expand Down
50 changes: 29 additions & 21 deletions docking/applets/hackernews/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import urllib.request
from collections.abc import Callable, Mapping, Sequence
from dataclasses import dataclass
from typing import Any
from typing import Any, cast

from docking.applets.live_state import (
live_freshness_lines,
Expand Down Expand Up @@ -135,7 +135,7 @@ def build_tooltip(
loading: bool = False,
page_loading: bool = False,
error: str = "",
fetched_at: object | None = None,
fetched_at: dt.datetime | str | None = None,
cadence_seconds: int | None = None,
) -> str:
"""Build tooltip text for the current applet state."""
Expand Down Expand Up @@ -202,31 +202,32 @@ def parse_story_payload(data: object) -> HackerNewsStory | None:
"""
if not isinstance(data, Mapping):
return None
if data.get("deleted") or data.get("dead"):
raw = cast(Mapping[str, Any], data)
if raw.get("deleted") or raw.get("dead"):
return None
if data.get("type") != "story":
if raw.get("type") != "story":
return None
try:
story_id = int(data.get("id", 0))
story_id = int(raw.get("id", 0))
except (TypeError, ValueError):
return None
title = normalize_text(data.get("title"))
title = normalize_text(raw.get("title"))
if story_id <= 0 or not title:
return None
try:
score = int(data.get("score", 0) or 0)
score = int(raw.get("score", 0) or 0)
except (TypeError, ValueError):
score = 0
try:
comments = int(data.get("descendants", 0) or 0)
comments = int(raw.get("descendants", 0) or 0)
except (TypeError, ValueError):
comments = 0
try:
timestamp = int(data.get("time", 0) or 0)
timestamp = int(raw.get("time", 0) or 0)
except (TypeError, ValueError):
timestamp = 0

item_url = normalize_text(data.get("url"))
item_url = normalize_text(raw.get("url"))
hn_url = comments_url(story_id)
return HackerNewsStory(
id=story_id,
Expand All @@ -235,7 +236,7 @@ def parse_story_payload(data: object) -> HackerNewsStory | None:
hn_url=hn_url,
score=max(0, score),
comments=max(0, comments),
by=normalize_text(data.get("by")),
by=normalize_text(raw.get("by")),
time=max(0, timestamp),
)

Expand All @@ -259,6 +260,8 @@ def parse_top_story_ids(
skipped = 0
offset = max(0, offset)
for value in data:
if not isinstance(value, (int, float, str)):
continue
try:
story_id = int(value)
except (TypeError, ValueError):
Expand Down Expand Up @@ -288,6 +291,8 @@ def parse_top_story_id_page(

valid_ids: list[int] = []
for value in data:
if not isinstance(value, (int, float, str)):
continue
try:
story_id = int(value)
except (TypeError, ValueError):
Expand Down Expand Up @@ -336,27 +341,28 @@ def story_from_pref(data: object) -> HackerNewsStory | None:
"""Parse one persisted story."""
if not isinstance(data, Mapping):
return None
raw = cast(Mapping[str, Any], data)
try:
story_id = int(data.get("id", 0))
score = int(data.get("score", 0) or 0)
comments = int(data.get("comments", 0) or 0)
timestamp = int(data.get("time", 0) or 0)
story_id = int(raw.get("id", 0))
score = int(raw.get("score", 0) or 0)
comments = int(raw.get("comments", 0) or 0)
timestamp = int(raw.get("time", 0) or 0)
except (TypeError, ValueError):
return None
title = normalize_text(data.get("title"))
title = normalize_text(raw.get("title"))
if story_id <= 0 or not title:
return None
hn_url = normalize_text(data.get("hn_url")) or comments_url(story_id)
hn_url = normalize_text(raw.get("hn_url")) or comments_url(story_id)
return HackerNewsStory(
id=story_id,
title=title,
url=normalize_text(data.get("url")) or hn_url,
url=normalize_text(raw.get("url")) or hn_url,
hn_url=hn_url,
score=max(0, score),
comments=max(0, comments),
by=normalize_text(data.get("by")),
by=normalize_text(raw.get("by")),
time=max(0, timestamp),
source=normalize_text(data.get("source")) or HN_SOURCE,
source=normalize_text(raw.get("source")) or HN_SOURCE,
)


Expand Down Expand Up @@ -393,7 +399,9 @@ def prefs_from_mapping(prefs: Mapping[str, Any] | None) -> HackerNewsPrefs:

def _int_from_pref(value: object, fallback: int) -> int:
try:
return int(value)
if isinstance(value, (int, float, str)):
return int(value)
return fallback
except (TypeError, ValueError):
return fallback

Expand Down
3 changes: 2 additions & 1 deletion docking/applets/thermals/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import datetime as dt
import re
import shutil
import subprocess
Expand Down Expand Up @@ -389,7 +390,7 @@ def build_tooltip(
loading: bool = False,
error: str | None = None,
temperature_unit: TemperatureUnit = TemperatureUnit.CELSIUS,
updated_at: object | None = None,
updated_at: dt.datetime | str | None = None,
cadence_seconds: int | None = None,
) -> str:
"""Build tooltip text for Thermals."""
Expand Down
3 changes: 2 additions & 1 deletion docking/applets/weather/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import annotations

import datetime as dt
from collections.abc import Mapping
from dataclasses import dataclass
from functools import lru_cache
Expand Down Expand Up @@ -153,7 +154,7 @@ def build_tooltip(
fetch_failed: bool = False,
error: str | None = None,
temperature_unit: TemperatureUnit = TemperatureUnit.CELSIUS,
updated_at: object | None = None,
updated_at: dt.datetime | str | None = None,
cadence_seconds: int | None = None,
) -> str:
"""Build multi-line tooltip with current + daily forecast."""
Expand Down
Loading