mirror of
https://github.com/rommapp/romm.git
synced 2026-11-28 17:48:31 +01:00
* fix(backend): make type ignores match what mypy reports Third in the staged series clearing the errors #4655 made visible. Whole-project mypy: 91 errors in 26 files -> 64 in 23. Twenty `# type: ignore[assignment]` comments in endpoints/responses/rom.py never suppressed anything. The request-scoped values `from_orm_with_request` attaches to a Rom (rom_user, has_notes, user_saves and the rest) are not declared on the model, so mypy reports `attr-defined`; it does so under 1.19 as well as 2.3, so the code was wrong when the comments were written rather than drifting since. They now name the error they were meant to silence. Seventy-nine ignores across 24 files suppressed nothing at all under `--warn-unused-ignores`, and are removed. One of them, `utils/zip_cache.py`, keeps its `attr-defined` code for the private `zipfile._get_compressor` and drops only `assignment`. The unused set is identical under mypy 2.3.0 and 2.3.1, and removing them leaves the error count unchanged under both, so none was quietly needed. The two `filter` helpers typed as `[QueryT: Select | Update | Delete]` bound the TypeVar to the union, so `query.filter(...)` returned the union and could not be assigned back to `QueryT`. Constraining it to the three types lets mypy check each one separately, and the callers are unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SPpv6W2ygP9tQ61fTQxdYp * fix(backend): replace mypy and ASYNC240 trunk-ignores with real fixes A `trunk-ignore` only suppresses when Trunk runs the linter. Plain mypy does not read them, so the 13 on mypy findings were all still errors to `mypy .` and would fail a whole-project check. Each now has a fix. endpoints/tasks.py: `common_data` was a plain dict literal unpacked into eight TypedDict constructors, so mypy saw `dict[str, Any]` and could not match its keys. It holds exactly the fields of BaseTaskStatusResponse and is now annotated as one, so the unpacking is checked. handler/metadata/gamelist_handler.py: both media maps were inferred as `dict[str, str]`, so indexing the TypedDict with their keys was unprovable. Their keys are now typed `GamelistMediaKey`, the Literal of the TypedDict's fields. A misspelt map key is now an error at the entry; before, only the index site could have noticed, and it was suppressed. handler/metadata/playmatch_handler.py: `attr` was a bare `str`. A TypeIs guard narrows it to the five lookup attributes, and the frozenset is derived from that Literal so the names live in one place. mypy now checks that each name is a real PlaymatchRomMatch key. It does not check that each takes an int, since mypy is lenient about value types on a union key; that still rests on which five names are listed. endpoints/search.py: the per-source tuples mixed ROM-list and handler types, which mypy joined to `object`. The dict is now annotated with what the loop uses: Mapping rows and the MetadataHandler base. tests/handler/metadata/test_launchbox_handler.py: `Path.stat()` inside an async test (ASYNC240) now goes through `anyio.Path`, as the rest of the codebase already does. Whole-project mypy: 64 errors in 23 files -> 51 in 20, identical under 2.3.0 and 2.3.1. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SPpv6W2ygP9tQ61fTQxdYp * fix(backend): keep gamelist media keys in one place Review pointed out that `GamelistMediaKey` repeated all fourteen fields of GamelistMetadataMedia. Python has no `keyof`, so any Literal of a TypedDict's keys is a second declaration of them, and a field added to the TypedDict would be left out of it without any error. The three index sites go back to suppressing `literal-required`, now as `# type: ignore[literal-required]` so plain mypy reads it as well as Trunk. The TypedDict stays the only declaration of the key set. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01SPpv6W2ygP9tQ61fTQxdYp --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
185 lines
5.5 KiB
Python
185 lines
5.5 KiB
Python
import functools
|
|
from collections.abc import Sequence
|
|
from datetime import datetime
|
|
|
|
from sqlalchemy import delete, func, or_, select, update
|
|
from sqlalchemy.orm import Query, Session, selectinload
|
|
|
|
from decorators.database import begin_session
|
|
from models.platform import Platform
|
|
from models.rom import Rom
|
|
|
|
from .base_handler import DBBaseHandler
|
|
|
|
|
|
def with_firmware(func):
|
|
@functools.wraps(func)
|
|
def wrapper(*args, **kwargs):
|
|
kwargs["query"] = select(Platform).options(
|
|
selectinload(Platform.firmware),
|
|
)
|
|
return func(*args, **kwargs)
|
|
|
|
return wrapper
|
|
|
|
|
|
class DBPlatformsHandler(DBBaseHandler):
|
|
@begin_session
|
|
@with_firmware
|
|
def add_platform(
|
|
self,
|
|
platform: Platform,
|
|
query: Query = None, # type: ignore
|
|
session: Session = None, # type: ignore
|
|
) -> Platform:
|
|
platform = session.merge(platform)
|
|
session.flush()
|
|
|
|
return session.scalar(query.filter_by(id=platform.id).limit(1))
|
|
|
|
@begin_session
|
|
def update_platform(
|
|
self,
|
|
id: int,
|
|
data: dict,
|
|
session: Session = None, # type: ignore
|
|
) -> Platform:
|
|
session.execute(
|
|
update(Platform)
|
|
.where(Platform.id == id)
|
|
.values(**data)
|
|
.execution_options(synchronize_session="evaluate")
|
|
)
|
|
return session.query(Platform).filter_by(id=id).one()
|
|
|
|
@begin_session
|
|
@with_firmware
|
|
def get_platform(
|
|
self,
|
|
id: int,
|
|
query: Query = None, # type: ignore
|
|
session: Session = None, # type: ignore
|
|
) -> Platform | None:
|
|
return session.scalar(query.filter_by(id=id).limit(1))
|
|
|
|
@begin_session
|
|
@with_firmware
|
|
def get_platforms(
|
|
self,
|
|
updated_after: datetime | None = None,
|
|
hidden_platform_ids: Sequence[int] | None = None,
|
|
query: Query = None, # type: ignore
|
|
session: Session = None, # type: ignore
|
|
) -> Sequence[Platform]:
|
|
if updated_after:
|
|
query = query.filter(Platform.updated_at > updated_after)
|
|
|
|
# Admin-driven visibility (opt-out): exclude platforms hidden from the
|
|
# caller. Empty/None (e.g. admins) skips filtering.
|
|
if hidden_platform_ids:
|
|
query = query.filter(Platform.id.not_in(hidden_platform_ids))
|
|
|
|
return session.scalars(query.order_by(Platform.name.asc())).unique().all()
|
|
|
|
@begin_session
|
|
def get_platform_ids(
|
|
self,
|
|
hidden_platform_ids: Sequence[int] | None = None,
|
|
session: Session = None, # type: ignore
|
|
) -> list[int]:
|
|
"""Ids only, deliberately off `with_firmware`: its eager load fires anyway."""
|
|
query = select(Platform).order_by(Platform.name.asc())
|
|
|
|
if hidden_platform_ids:
|
|
query = query.filter(Platform.id.not_in(hidden_platform_ids))
|
|
|
|
return list(session.scalars(query.with_only_columns(Platform.id)).all())
|
|
|
|
@begin_session
|
|
@with_firmware
|
|
def get_platform_by_fs_slug(
|
|
self,
|
|
fs_slug: str,
|
|
query: Query = None, # type: ignore
|
|
session: Session = None, # type: ignore
|
|
) -> Platform | None:
|
|
platform = session.scalar(query.filter_by(fs_slug=fs_slug).limit(1))
|
|
if platform:
|
|
return platform
|
|
|
|
# Folder names are matched case-insensitively everywhere else, so a
|
|
# folder renamed only in case is the same platform, not a new one.
|
|
return session.scalar(
|
|
query.filter(func.lower(Platform.fs_slug) == fs_slug.lower()).limit(1)
|
|
)
|
|
|
|
@begin_session
|
|
@with_firmware
|
|
def get_platform_by_slug(
|
|
self,
|
|
slug: str,
|
|
query: Query = None, # type: ignore
|
|
session: Session = None, # type: ignore
|
|
) -> Platform | None:
|
|
return session.scalar(query.filter_by(slug=slug).limit(1))
|
|
|
|
@begin_session
|
|
def delete_platform(
|
|
self,
|
|
id: int,
|
|
session: Session = None, # type: ignore
|
|
) -> None:
|
|
# Remove all roms from that platforms first
|
|
session.execute(
|
|
delete(Rom)
|
|
.where(Rom.platform_id == id)
|
|
.execution_options(synchronize_session="evaluate")
|
|
)
|
|
|
|
session.execute(
|
|
delete(Platform)
|
|
.where(Platform.id == id)
|
|
.execution_options(synchronize_session="evaluate")
|
|
)
|
|
|
|
@begin_session
|
|
def mark_missing_platforms(
|
|
self,
|
|
fs_platforms_to_keep: list[str],
|
|
query: Query = None, # type: ignore
|
|
session: Session = None, # type: ignore
|
|
) -> Sequence[Platform]:
|
|
missing_platforms = (
|
|
session.scalars(
|
|
select(Platform)
|
|
.order_by(Platform.name.asc())
|
|
.where(
|
|
or_(
|
|
Platform.fs_slug.not_in(fs_platforms_to_keep),
|
|
Platform.slug.is_(None),
|
|
)
|
|
)
|
|
)
|
|
.unique()
|
|
.all()
|
|
)
|
|
session.execute(
|
|
update(Platform)
|
|
.where(
|
|
or_(
|
|
Platform.fs_slug.not_in(fs_platforms_to_keep),
|
|
Platform.slug.is_(None),
|
|
)
|
|
)
|
|
.values(**{"missing_from_fs": True})
|
|
.execution_options(synchronize_session="fetch")
|
|
)
|
|
|
|
session.execute(
|
|
update(Rom)
|
|
.where(Rom.platform_id.in_([p.id for p in missing_platforms]))
|
|
.values(**{"missing_from_fs": True})
|
|
.execution_options(synchronize_session="fetch")
|
|
)
|
|
return missing_platforms
|