From 256a8098c69a54420d1934299633be2a328b0a05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?dni=20=E2=9A=A1?= Date: Wed, 9 Jul 2025 11:08:48 +0200 Subject: [PATCH] chore: get rid of mypy notes and typecheck more functions (#3257) --- lnbits/app.py | 4 ++-- lnbits/core/services/notifications.py | 6 +++--- lnbits/core/tasks.py | 20 ++++++++++---------- lnbits/wallets/blink.py | 4 ++-- lnbits/wallets/boltz.py | 2 +- lnbits/wallets/nwc.py | 2 +- lnbits/wallets/strike.py | 6 +++--- tests/unit/test_crud_payments.py | 2 +- 8 files changed, 23 insertions(+), 23 deletions(-) diff --git a/lnbits/app.py b/lnbits/app.py index d8f153c0..6c1fa678 100644 --- a/lnbits/app.py +++ b/lnbits/app.py @@ -448,7 +448,7 @@ def register_ext_routes(app: FastAPI, ext: Extension) -> None: app.include_router(router=ext_route, prefix=prefix) -async def check_and_register_extensions(app: FastAPI): +async def check_and_register_extensions(app: FastAPI) -> None: await check_installed_extensions(app) for ext in await get_valid_extensions(False): try: @@ -458,7 +458,7 @@ async def check_and_register_extensions(app: FastAPI): logger.error(f"Could not load extension `{ext.code}`: {exc!s}") -def register_async_tasks(): +def register_async_tasks() -> None: create_permanent_task(wait_for_audit_data) create_permanent_task(wait_notification_messages) diff --git a/lnbits/core/services/notifications.py b/lnbits/core/services/notifications.py index c006fe9d..cf4800b8 100644 --- a/lnbits/core/services/notifications.py +++ b/lnbits/core/services/notifications.py @@ -28,7 +28,7 @@ from lnbits.helpers import check_callback_url, is_valid_email_address from lnbits.settings import settings from lnbits.utils.nostr import normalize_private_key -notifications_queue: asyncio.Queue = asyncio.Queue() +notifications_queue: asyncio.Queue[NotificationMessage] = asyncio.Queue() def enqueue_notification(message_type: NotificationType, values: dict) -> None: @@ -42,8 +42,8 @@ def enqueue_notification(message_type: NotificationType, values: dict) -> None: logger.error(f"Error enqueuing notification: {e}") -async def process_next_notification(): - notification_message: NotificationMessage = await notifications_queue.get() +async def process_next_notification() -> None: + notification_message = await notifications_queue.get() message_type, text = _notification_message_to_text(notification_message) await send_notification(text, message_type) diff --git a/lnbits/core/tasks.py b/lnbits/core/tasks.py index 49ac6b77..994df33e 100644 --- a/lnbits/core/tasks.py +++ b/lnbits/core/tasks.py @@ -13,7 +13,7 @@ from lnbits.core.crud.audit import delete_expired_audit_entries from lnbits.core.crud.payments import get_payments_status_count from lnbits.core.crud.users import get_accounts from lnbits.core.crud.wallets import get_wallets_count -from lnbits.core.models import AuditEntry +from lnbits.core.models.audit import AuditEntry from lnbits.core.models.extensions import InstallableExtension from lnbits.core.models.notifications import NotificationType from lnbits.core.services.funding_source import ( @@ -31,10 +31,10 @@ from lnbits.settings import settings from lnbits.tasks import create_unique_task from lnbits.utils.exchange_rates import btc_rates -audit_queue: asyncio.Queue = asyncio.Queue() +audit_queue: asyncio.Queue[AuditEntry] = asyncio.Queue() -async def run_by_the_minute_tasks(): +async def run_by_the_minute_tasks() -> None: minute_counter = 0 while settings.lnbits_running: status_minutes = settings.lnbits_notification_server_status_hours * 60 @@ -69,7 +69,7 @@ async def run_by_the_minute_tasks(): await asyncio.sleep(60) -async def _notify_server_status(): +async def _notify_server_status() -> None: accounts = await get_accounts(filters=Filters(limit=0)) wallets_count = await get_wallets_count() payments = await get_payments_status_count() @@ -90,7 +90,7 @@ async def _notify_server_status(): enqueue_notification(NotificationType.server_status, values) -async def wait_for_paid_invoices(invoice_paid_queue: asyncio.Queue): +async def wait_for_paid_invoices(invoice_paid_queue: asyncio.Queue) -> None: """ This worker dispatches events to all extensions and dispatches webhooks. """ @@ -103,13 +103,13 @@ async def wait_for_paid_invoices(invoice_paid_queue: asyncio.Queue): await send_payment_notification(wallet, payment) -async def wait_for_audit_data(): +async def wait_for_audit_data() -> None: """ Waits for audit entries to be pushed to the queue. Then it inserts the entries into the DB. """ while settings.lnbits_running: - data: AuditEntry = await audit_queue.get() + data = await audit_queue.get() try: await create_audit_entry(data) except Exception as ex: @@ -117,7 +117,7 @@ async def wait_for_audit_data(): await asyncio.sleep(3) -async def wait_notification_messages(): +async def wait_notification_messages() -> None: while settings.lnbits_running: try: @@ -127,7 +127,7 @@ async def wait_notification_messages(): await asyncio.sleep(3) -async def purge_audit_data(): +async def purge_audit_data() -> None: """ Remove audit entries which have passed their retention period. """ @@ -141,7 +141,7 @@ async def purge_audit_data(): await asyncio.sleep(60 * 60) -async def collect_exchange_rates_data(): +async def collect_exchange_rates_data() -> None: """ Collect exchange rates data. Used for monitoring only. """ diff --git a/lnbits/wallets/blink.py b/lnbits/wallets/blink.py index c6f733eb..2d0fb476 100644 --- a/lnbits/wallets/blink.py +++ b/lnbits/wallets/blink.py @@ -7,7 +7,7 @@ from typing import Optional import httpx from loguru import logger from pydantic import BaseModel -from websockets.legacy.client import WebSocketClientProtocol, connect +from websockets.legacy.client import connect from websockets.typing import Subprotocol from lnbits import bolt11 @@ -48,7 +48,7 @@ class BlinkWallet(Wallet): "payload": {"X-API-KEY": settings.blink_token}, } self.client = httpx.AsyncClient(base_url=self.endpoint, headers=self.auth) - self.ws: Optional[WebSocketClientProtocol] = None + self.ws = None self._wallet_id = None @property diff --git a/lnbits/wallets/boltz.py b/lnbits/wallets/boltz.py index 578cf08c..74f53dec 100644 --- a/lnbits/wallets/boltz.py +++ b/lnbits/wallets/boltz.py @@ -63,7 +63,7 @@ class BoltzWallet(Wallet): channel = grpc.aio.insecure_channel(settings.boltz_client_endpoint) self.rpc = boltzrpc_pb2_grpc.BoltzStub(channel) - self.wallet_id: int = 0 + self.wallet_id = 0 # Auto-create wallet if running in Docker mode async def _init_boltz_wallet(): diff --git a/lnbits/wallets/nwc.py b/lnbits/wallets/nwc.py index eff1a4ef..27a30a25 100644 --- a/lnbits/wallets/nwc.py +++ b/lnbits/wallets/nwc.py @@ -62,7 +62,7 @@ class NWCWallet(Wallet): # interval in seconds between checks for pending payments self.pending_payments_lookup_interval = 10 # track paid invoices for paid_invoices_stream - self.paid_invoices_queue: asyncio.Queue = asyncio.Queue(0) + self.paid_invoices_queue = asyncio.Queue(0) # This task periodically checks if pending payments have been settled self.pending_payments_lookup_task = asyncio.create_task( self._handle_pending_payments() diff --git a/lnbits/wallets/strike.py b/lnbits/wallets/strike.py index 7f9ff8b1..e3f42851 100644 --- a/lnbits/wallets/strike.py +++ b/lnbits/wallets/strike.py @@ -27,7 +27,7 @@ class TokenBucket: Token bucket rate limiter for Strike API endpoints. """ - def __init__(self, rate: int, period_seconds: int): + def __init__(self, rate: int, period_seconds: int) -> None: """ Initialize a token bucket. @@ -77,7 +77,7 @@ class StrikeWallet(Wallet): # construction / teardown # # --------------------------------------------------------------------- # - def __init__(self): + def __init__(self) -> None: if not settings.strike_api_endpoint: raise ValueError("Missing strike_api_endpoint") if not settings.strike_api_key: @@ -120,7 +120,7 @@ class StrikeWallet(Wallet): self._cached_balance_ts: float = 0.0 self._cache_ttl = 30 # seconds - async def cleanup(self): + async def cleanup(self) -> None: try: await self.client.aclose() except Exception: diff --git a/tests/unit/test_crud_payments.py b/tests/unit/test_crud_payments.py index c5be2410..89d84cc1 100644 --- a/tests/unit/test_crud_payments.py +++ b/tests/unit/test_crud_payments.py @@ -81,7 +81,7 @@ async def test_crud_search_payments(): user = await create_user_account() wallet = await create_wallet(user_id=user.id) - filters: Filters = Filters( + filters = Filters( search="", model=PaymentFilters, )