From 808bd36e23765ca502b37c720349bdfe1da039bc Mon Sep 17 00:00:00 2001 From: dni Date: Tue, 19 Jul 2022 18:51:35 +0200 Subject: [PATCH] some more mypy fixes --- lnbits/core/crud.py | 2 +- lnbits/core/models.py | 2 ++ lnbits/core/views/api.py | 15 +++++++++------ lnbits/core/views/generic.py | 6 +++++- lnbits/helpers.py | 2 +- lnbits/tasks.py | 2 +- lnbits/utils/exchange_rates.py | 8 ++++---- lnbits/wallets/lnpay.py | 2 +- lnbits/wallets/opennode.py | 2 +- 9 files changed, 25 insertions(+), 16 deletions(-) diff --git a/lnbits/core/crud.py b/lnbits/core/crud.py index 770e2906a..db802d7bb 100644 --- a/lnbits/core/crud.py +++ b/lnbits/core/crud.py @@ -113,7 +113,7 @@ async def create_wallet( async def update_wallet( wallet_id: str, new_name: str, conn: Optional[Connection] = None ) -> Optional[Wallet]: - await (conn or db).execute( + return await (conn or db).execute( """ UPDATE wallets SET name = ? diff --git a/lnbits/core/models.py b/lnbits/core/models.py index ab73b7020..0f7eba737 100644 --- a/lnbits/core/models.py +++ b/lnbits/core/models.py @@ -106,6 +106,8 @@ class Payment(BaseModel): @property def tag(self) -> Optional[str]: + if self.extra is None: + return "" return self.extra.get("tag") @property diff --git a/lnbits/core/views/api.py b/lnbits/core/views/api.py index 9fee6063d..bd15ee8d6 100644 --- a/lnbits/core/views/api.py +++ b/lnbits/core/views/api.py @@ -3,7 +3,7 @@ import hashlib import json from binascii import unhexlify from http import HTTPStatus -from typing import Dict, List, Optional, Union +from typing import Dict, List, Optional, Union, Tuple from urllib.parse import ParseResult, parse_qs, urlencode, urlparse, urlunparse import httpx @@ -185,7 +185,7 @@ async def api_payments_create_invoice(data: CreateInvoiceData, wallet: Wallet): assert ( data.lnurl_balance_check is not None ), "lnurl_balance_check is required" - save_balance_check(wallet.id, data.lnurl_balance_check) + await save_balance_check(wallet.id, data.lnurl_balance_check) async with httpx.AsyncClient() as client: try: @@ -291,7 +291,7 @@ async def api_payments_pay_lnurl( timeout=40, ) if r.is_error: - raise httpx.ConnectError + raise httpx.ConnectError("LNURL Callback Connection Error") except (httpx.ConnectError, httpx.RequestError): raise HTTPException( status_code=HTTPStatus.BAD_REQUEST, @@ -348,7 +348,7 @@ async def subscribe(request: Request, wallet: Wallet): logger.debug("adding sse listener", payment_queue) api_invoice_listeners.append(payment_queue) - send_queue: asyncio.Queue[tuple[str, Payment]] = asyncio.Queue(0) + send_queue: asyncio.Queue[Tuple[str, Payment]] = asyncio.Queue(0) async def payment_received() -> None: while True: @@ -389,10 +389,13 @@ async def api_payment(payment_hash, X_Api_Key: Optional[str] = Header(None)): # If a valid key is given, we also return the field "details", otherwise not wallet = None try: + assert X_Api_Key is not None + # TODO: type above is Optional[str] how can that have .extra? if X_Api_Key.extra: logger.warning("No key") except: - wallet = await get_wallet_for_key(X_Api_Key) + if X_Api_Key is not None: + wallet = await get_wallet_for_key(X_Api_Key) payment = await get_standalone_payment( payment_hash, wallet_id=wallet.id if wallet else None ) # we have to specify the wallet id here, because postgres and sqlite return internal payments in different order @@ -606,7 +609,7 @@ class ConversionData(BaseModel): async def api_fiat_as_sats(data: ConversionData): output = {} if data.from_ == "sat": - output["sats"] = int(data.amount) + output["sats"] = data.amount output["BTC"] = data.amount / 100000000 for currency in data.to.split(","): output[currency.strip().upper()] = await satoshis_amount_as_fiat( diff --git a/lnbits/core/views/generic.py b/lnbits/core/views/generic.py index 44666ce16..7ef306dc8 100644 --- a/lnbits/core/views/generic.py +++ b/lnbits/core/views/generic.py @@ -121,9 +121,11 @@ async def wallet( if not user_id: user = await get_user((await create_account()).id) + assert user is not None logger.info(f"Create user {user.id}") else: user = await get_user(user_id) + assert user is not None if not user: return template_renderer().TemplateResponse( "error.html", {"request": request, "err": "User does not exist."} @@ -218,6 +220,7 @@ async def lnurl_full_withdraw_callback(request: Request): @core_html_routes.get("/deletewallet", response_class=RedirectResponse) async def deletewallet(request: Request, wal: str = Query(...), usr: str = Query(...)): user = await get_user(usr) + assert user is not None user_wallet_ids = [u.id for u in user.wallets] if wal not in user_wallet_ids: @@ -242,7 +245,7 @@ async def deletewallet(request: Request, wal: str = Query(...), usr: str = Query async def lnurl_balance_notify(request: Request, service: str): bc = await get_balance_check(request.query_params.get("wal"), service) if bc: - redeem_lnurl_withdraw(bc.wallet, bc.url) + await redeem_lnurl_withdraw(bc.wallet, bc.url) @core_html_routes.get( @@ -252,6 +255,7 @@ async def lnurlwallet(request: Request): async with db.connect() as conn: account = await create_account(conn=conn) user = await get_user(account.id, conn=conn) + assert user is not None wallet = await create_wallet(user_id=user.id, conn=conn) asyncio.create_task( diff --git a/lnbits/helpers.py b/lnbits/helpers.py index 71b3dd691..e97fc7bbb 100644 --- a/lnbits/helpers.py +++ b/lnbits/helpers.py @@ -34,7 +34,7 @@ class ExtensionManager: @property def extensions(self) -> List[Extension]: - output = [] + output: List[Extension] = [] if "all" in self._disabled: return output diff --git a/lnbits/tasks.py b/lnbits/tasks.py index 86863f98f..f4d0a928d 100644 --- a/lnbits/tasks.py +++ b/lnbits/tasks.py @@ -66,7 +66,7 @@ async def webhook_handler(): raise HTTPException(status_code=HTTPStatus.NO_CONTENT) -internal_invoice_queue = asyncio.Queue(0) +internal_invoice_queue: asyncio.Queue = asyncio.Queue(0) async def internal_invoice_listener(): diff --git a/lnbits/utils/exchange_rates.py b/lnbits/utils/exchange_rates.py index 0432b364a..fbb4add22 100644 --- a/lnbits/utils/exchange_rates.py +++ b/lnbits/utils/exchange_rates.py @@ -1,5 +1,5 @@ import asyncio -from typing import Callable, NamedTuple +from typing import Callable, NamedTuple, List import httpx from loguru import logger @@ -227,10 +227,10 @@ async def btc_price(currency: str) -> float: "TO": currency.upper(), "to": currency.lower(), } - rates = [] - tasks = [] + rates: List[float] = [] + tasks: List[asyncio.Task] = [] - send_channel = asyncio.Queue() + send_channel: asyncio.Queue = asyncio.Queue() async def controller(): failures = 0 diff --git a/lnbits/wallets/lnpay.py b/lnbits/wallets/lnpay.py index 807d72538..2ff1afa93 100644 --- a/lnbits/wallets/lnpay.py +++ b/lnbits/wallets/lnpay.py @@ -119,7 +119,7 @@ class LNPayWallet(Wallet): return PaymentStatus(statuses[r.json()["settled"]]) async def paid_invoices_stream(self) -> AsyncGenerator[str, None]: - self.queue = asyncio.Queue(0) + self.queue: asyncio.Queue = asyncio.Queue(0) while True: value = await self.queue.get() yield value diff --git a/lnbits/wallets/opennode.py b/lnbits/wallets/opennode.py index 6d3fb02c9..9cd05ebdd 100644 --- a/lnbits/wallets/opennode.py +++ b/lnbits/wallets/opennode.py @@ -127,7 +127,7 @@ class OpenNodeWallet(Wallet): return PaymentStatus(statuses[r.json()["data"]["status"]]) async def paid_invoices_stream(self) -> AsyncGenerator[str, None]: - self.queue = asyncio.Queue(0) + self.queue: asyncio.Queue = asyncio.Queue(0) while True: value = await self.queue.get() yield value