mirror of
https://github.com/rommapp/romm.git
synced 2026-11-28 17:48:31 +01:00
* chore(mypy): switch to strict mode with listed exceptions Replace the hand-picked flags with `strict = True`, turning off only the four flags that aren't clean yet (disallow_any_generics, warn_return_any, disallow_untyped_calls, disallow_untyped_decorators). Any other strict flag, including those future mypy releases add to the preset, is on by default. That turns on no_implicit_reexport, strict_equality and extra_checks: - Import each name from the module that defines it. About 240 test sites patched a dependency through the module that imported it (`scan_module.fs_rom_handler`); they now import the same object from its source, so every patch targets what it did before. Tests that replace a module attribute are unchanged. - handler.streaming.memory_cards re-exports store_memory_card_version explicitly, since endpoints call it there and tests patch it there. - mutagen gets implicit_reexport, as it re-exports without __all__. - Test comparisons build typed values (CoverResult, a typed expected dict, enum .value) and the CORS lookup casts for its identity check. - One ss_handler ** spread keeps a coded ignore: extra_checks wants its NotRequired dump_* keys spelled out, and the media dict never has them. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PLzXae5up2pYXRoBjfjn2G * chore(mypy): give every generic its type arguments Drop disallow_any_generics from the strict-mode exceptions and fix the 347 bare generics it reports, 230 of them in app code. - Bare `dict` becomes `dict[str, Any]`: update payloads, raw provider JSON and endpoint bodies map string keys to mixed values, so the Any is real and now written down. The OpenAPI document is unchanged. - Rom query filters take and return RomSelect; _music_facet_joins and _exclude_hidden are generic over the Select they're given, and _build_hash_selects returns id-only selects. - Redis clients are typed by what they return: the decoding caches are Redis[str], the RQ connection and binary cache Redis[bytes]. - The rest get explicit parameters: Row, Pattern, ColumnElement, QueryableAttribute, SpooledTemporaryFile, Task, Future and friends. Precise types reached callers that had been getting Any, which surfaced: - The LaunchBox cache writer is fed binary-cache pipelines. - The flashpoint and IGDB request helpers return raw JSON of varying shape, not always a dict. - The rom search endpoint returned dicts under a list[SearchRomSchema] annotation; it now validates them into that schema. - The audit filter's id subquery selects strings. - In tests the binary cache is the non-decoding fake shared with async_cache, so it is cast to Redis[bytes] rather than changed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PLzXae5up2pYXRoBjfjn2G * test(upload): assert cleanup against the patched upload directory The 409 test checked config's ROM_UPLOAD_TMP_BASE while the fixture patches the endpoint's copy to tmp_path/uploads, so it passed even if cleanup failed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PLzXae5up2pYXRoBjfjn2G * chore(mypy): keep the row type through the ROM filter helpers filter_roms and its helpers also receive select(Rom.id) membership queries, so they now take and return the caller's Select type instead of narrowing to RomSelect. merged_ra_metadata mixes scalar and list values. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PLzXae5up2pYXRoBjfjn2G --------- Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
193 lines
5.9 KiB
Python
193 lines
5.9 KiB
Python
from collections.abc import Collection, Sequence
|
|
from typing import Any
|
|
|
|
from sqlalchemy import Select, and_, delete, desc, or_, select, update
|
|
from sqlalchemy.orm import Session
|
|
|
|
from decorators.database import begin_session
|
|
from models.assets import State
|
|
from models.base import with_file_name_parts
|
|
from models.rom import Rom
|
|
|
|
from .base_handler import DBBaseHandler
|
|
|
|
|
|
class DBStatesHandler(DBBaseHandler):
|
|
@begin_session
|
|
def add_state(
|
|
self,
|
|
state: State,
|
|
session: Session = None, # type: ignore[assignment]
|
|
) -> State:
|
|
return session.merge(state)
|
|
|
|
@begin_session
|
|
def get_state(
|
|
self,
|
|
user_id: int,
|
|
id: int,
|
|
session: Session = None, # type: ignore[assignment]
|
|
) -> State | None:
|
|
return session.scalar(select(State).filter_by(user_id=user_id, id=id).limit(1))
|
|
|
|
@begin_session
|
|
def get_state_by_filename(
|
|
self,
|
|
user_id: int,
|
|
rom_id: int,
|
|
file_name: str,
|
|
session: Session = None, # type: ignore[assignment]
|
|
) -> State | None:
|
|
return session.scalar(
|
|
select(State)
|
|
.filter_by(rom_id=rom_id, user_id=user_id, file_name=file_name)
|
|
.limit(1)
|
|
)
|
|
|
|
def _states_query(
|
|
self,
|
|
user_id: int,
|
|
rom_ids: Collection[int] | None = None,
|
|
platform_id: int | None = None,
|
|
) -> Select[tuple[State]]:
|
|
query = select(State).filter_by(user_id=user_id)
|
|
|
|
# An empty collection is an explicit empty scope, not an absent filter.
|
|
if rom_ids is not None:
|
|
query = query.filter(State.rom_id.in_(rom_ids))
|
|
|
|
if platform_id:
|
|
query = query.join(Rom, State.rom_id == Rom.id).filter(
|
|
Rom.platform_id == platform_id
|
|
)
|
|
|
|
return query
|
|
|
|
@begin_session
|
|
def get_states(
|
|
self,
|
|
user_id: int,
|
|
rom_ids: Collection[int] | None = None,
|
|
platform_id: int | None = None,
|
|
session: Session = None, # type: ignore[assignment]
|
|
) -> Sequence[State]:
|
|
query = self._states_query(
|
|
user_id=user_id, rom_ids=rom_ids, platform_id=platform_id
|
|
)
|
|
return session.scalars(query).all()
|
|
|
|
@begin_session
|
|
def get_state_ids(
|
|
self,
|
|
user_id: int,
|
|
rom_ids: Collection[int] | None = None,
|
|
platform_id: int | None = None,
|
|
session: Session = None, # type: ignore[assignment]
|
|
) -> list[int]:
|
|
"""Ids only, so no `State` is built and no eager rom or user join fires."""
|
|
query = self._states_query(
|
|
user_id=user_id, rom_ids=rom_ids, platform_id=platform_id
|
|
)
|
|
return list(session.scalars(query.with_only_columns(State.id)).all())
|
|
|
|
@begin_session
|
|
def get_state_by_id(
|
|
self,
|
|
id: int,
|
|
session: Session = None, # type: ignore[assignment]
|
|
) -> State | None:
|
|
"""Fetch a state by id without scoping to an owner. Used for the
|
|
visibility toggle and community downloads, where the caller may not own
|
|
the state. Mirrors db_screenshot_handler.get_screenshot_by_id."""
|
|
return session.get(State, id)
|
|
|
|
@begin_session
|
|
def get_rom_shared_states(
|
|
self,
|
|
rom_id: int,
|
|
user_id: int,
|
|
public_only: bool = False,
|
|
session: Session = None, # type: ignore[assignment]
|
|
) -> Sequence[State]:
|
|
"""States for a ROM visible to the requesting user: own (public +
|
|
private) plus other users' public ones. Mirrors
|
|
db_screenshot_handler.get_rom_gallery_screenshots."""
|
|
query = select(State).filter(State.rom_id == rom_id)
|
|
|
|
if public_only:
|
|
query = query.filter(State.is_public)
|
|
else:
|
|
query = query.filter(or_(State.user_id == user_id, State.is_public))
|
|
|
|
query = query.order_by(desc(State.updated_at))
|
|
return session.scalars(query).all()
|
|
|
|
@begin_session
|
|
def update_state(
|
|
self,
|
|
id: int,
|
|
data: dict[str, Any],
|
|
touch: bool = True,
|
|
session: Session = None, # type: ignore[assignment]
|
|
) -> State:
|
|
"""Write `data` onto a state.
|
|
|
|
Args:
|
|
touch: False keeps `updated_at`, since annotating is not a write
|
|
to the bytes and device sync reads it to detect staleness.
|
|
"""
|
|
data = with_file_name_parts(data)
|
|
values = data if touch else {**data, "updated_at": State.updated_at}
|
|
session.execute(
|
|
update(State)
|
|
.where(State.id == id)
|
|
.values(**values)
|
|
.execution_options(synchronize_session="evaluate")
|
|
)
|
|
return session.scalars(select(State).filter_by(id=id)).one()
|
|
|
|
@begin_session
|
|
def delete_state(
|
|
self,
|
|
id: int,
|
|
session: Session = None, # type: ignore[assignment]
|
|
) -> None:
|
|
session.execute(
|
|
delete(State)
|
|
.where(State.id == id)
|
|
.execution_options(synchronize_session="evaluate")
|
|
)
|
|
|
|
@begin_session
|
|
def mark_missing_states(
|
|
self,
|
|
rom_id: int,
|
|
user_id: int,
|
|
states_to_keep: list[str],
|
|
session: Session = None, # type: ignore[assignment]
|
|
) -> Sequence[State]:
|
|
missing_states = session.scalars(
|
|
select(State).filter(
|
|
and_(
|
|
State.rom_id == rom_id,
|
|
State.user_id == user_id,
|
|
State.file_name.not_in(states_to_keep),
|
|
)
|
|
)
|
|
).all()
|
|
|
|
session.execute(
|
|
update(State)
|
|
.where(
|
|
and_(
|
|
State.rom_id == rom_id,
|
|
State.user_id == user_id,
|
|
State.file_name.not_in(states_to_keep),
|
|
)
|
|
)
|
|
.values(**{"missing_from_fs": True})
|
|
.execution_options(synchronize_session="evaluate")
|
|
)
|
|
|
|
return missing_states
|