Use basedpyright refactor

This commit is contained in:
marc
2025-03-22 23:06:34 +01:00
parent ac54453b7f
commit 2f7c7c2429
41 changed files with 480 additions and 381 deletions

View File

@@ -1,14 +1,13 @@
from typing import Optional
from folkugat_web.dal.sql import Connection, get_connection
from folkugat_web.model import search as search_model
from folkugat_web.model import temes as model
from ._conversion import cell_to_ngrams, row_to_tema
from . import conversion
TEMA_ID_TO_NGRAMS_CACHE = None
_tema_id_to_ngrams_cache: dict[int, search_model.NGrams] | None = None
def get_tema_by_id(tema_id: int, con: Optional[Connection] = None) -> Optional[model.Tema]:
def get_tema_by_id(tema_id: int, con: Connection | None = None) -> model.Tema | None:
query = """
SELECT
id, title, properties, links, lyrics, alternatives, ngrams,
@@ -19,30 +18,30 @@ def get_tema_by_id(tema_id: int, con: Optional[Connection] = None) -> Optional[m
data = dict(id=tema_id)
with get_connection(con) as con:
cur = con.cursor()
cur.execute(query, data)
row = cur.fetchone()
return row_to_tema(row) if row else None
_ = cur.execute(query, data)
row: conversion.TemaRowTuple = cur.fetchone()
return conversion.row_to_tema(row) if row else None
def evict_tema_id_to_ngrams_cache():
global TEMA_ID_TO_NGRAMS_CACHE
TEMA_ID_TO_NGRAMS_CACHE = None
global _tema_id_to_ngrams_cache
_tema_id_to_ngrams_cache = None
def get_tema_id_to_ngrams(con: Optional[Connection] = None) -> dict[int, model.NGrams]:
global TEMA_ID_TO_NGRAMS_CACHE
if TEMA_ID_TO_NGRAMS_CACHE is None:
TEMA_ID_TO_NGRAMS_CACHE = _get_tema_id_to_ngrams(con)
return TEMA_ID_TO_NGRAMS_CACHE
def get_tema_id_to_ngrams(con: Connection | None = None) -> dict[int, search_model.NGrams]:
global _tema_id_to_ngrams_cache
if _tema_id_to_ngrams_cache is None:
_tema_id_to_ngrams_cache = _get_tema_id_to_ngrams(con)
return _tema_id_to_ngrams_cache
def _get_tema_id_to_ngrams(con: Optional[Connection] = None) -> dict[int, model.NGrams]:
def _get_tema_id_to_ngrams(con: Connection | None = None) -> dict[int, search_model.NGrams]:
query = """
SELECT id, ngrams
FROM temes
"""
with get_connection(con) as con:
cur = con.cursor()
cur.execute(query)
rows = cur.fetchall()
return {id_: cell_to_ngrams(ng) for id_, ng in rows}
_ = cur.execute(query)
rows: list[tuple[int, str]] = cur.fetchall()
return {id_: conversion.cell_to_ngrams(ng) for id_, ng in rows}