Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions docs/development/code.md
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,12 @@ async def tag_dataset(
11. Log statements give observability to what's happening in the REST API (see [Logging](#logging)).
12. Return values are coerced into JSON form by FastAPI.

## Database sessions
The `expdb_session` and `userdb_session` dependencies commit the transaction when the endpoint finishes.
Always declare them (and `fetch_user`, which uses a session) with `scope="function"`, for example `Depends(expdb_session, scope="function")`.
Without it, FastAPI commits *after* the response is sent, so a client can get a success response for data that is not saved yet.
A test checks that every router follows this rule.

## Logging
We use [`loguru`](https://loguru.readthedocs.io/en/stable/) for logging.
A [middleware](https://fastapi.tiangolo.com/reference/middleware/) layer automatically provides the following context to each log entry (see `core/logging.py::add_request_context_to_log`):
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ classifiers = [
"Operating System :: OS Independent",
]
dependencies = [
"fastapi",
"fastapi>=0.121",
"loguru",
"cryptography",
"pydantic",
Expand Down
22 changes: 11 additions & 11 deletions src/routers/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ async def tag_dataset(
data_id: Annotated[Identifier, Body()],
tag: Annotated[TagString, Body()],
user: Annotated[User, Depends(fetch_user_or_raise)],
expdb_db: Annotated[AsyncSession, Depends(expdb_session)],
expdb_db: Annotated[AsyncSession, Depends(expdb_session, scope="function")],
) -> dict[str, dict[str, Any]]:
"""Add a tag to the dataset, this tag is publicly visible to all users."""
try:
Expand All @@ -120,7 +120,7 @@ async def untag_dataset_like_php(
data_id: Annotated[Identifier, Body()],
tag: Annotated[TagString, Body()],
user: Annotated[User, Depends(fetch_user_or_raise)],
expdb_db: Annotated[AsyncSession, Depends(expdb_session)],
expdb_db: Annotated[AsyncSession, Depends(expdb_session, scope="function")],
) -> dict[Literal["data_untag"], TagInfo]:
"""Remove a tag from the dataset with a response similar to the old PHP server."""
await untag_dataset(data_id, tag, user, expdb_db)
Expand All @@ -138,7 +138,7 @@ async def untag_dataset(
identifier: Identifier,
tag: Annotated[TagString, Query()],
user: Annotated[User, Depends(fetch_user_or_raise)],
expdb_db: Annotated[AsyncSession, Depends(expdb_session)],
expdb_db: Annotated[AsyncSession, Depends(expdb_session, scope="function")],
) -> None:
"""Remove a tag that you added to a dataset, or from a dataset you uploaded."""
dataset_tag = await database.datasets.get_tag(identifier, tag, expdb_db)
Expand Down Expand Up @@ -186,7 +186,7 @@ def _quality_clause(quality: str, range_: str | None) -> str:
@router.get(path="/list")
async def list_datasets( # noqa: PLR0913, C901
*,
expdb_db: Annotated[AsyncSession, Depends(expdb_session)],
expdb_db: Annotated[AsyncSession, Depends(expdb_session, scope="function")],
pagination: Annotated[Pagination, Body(default_factory=Pagination)],
data_name: Annotated[CasualString128 | None, Body()] = None,
tag: Annotated[TagString | None, Body()] = None,
Expand All @@ -210,7 +210,7 @@ async def list_datasets( # noqa: PLR0913, C901
number_classes: Annotated[IntegerRange | None, Body()] = None,
number_missing_values: Annotated[IntegerRange | None, Body()] = None,
status: Annotated[DatasetStatusFilter, Body()] = DatasetStatusFilter.ACTIVE,
user: Annotated[User | None, Depends(fetch_user)] = None,
user: Annotated[User | None, Depends(fetch_user, scope="function")] = None,
) -> list[dict[str, Any]]:
"""List all datasets that match the filters."""
status_subquery = text(
Expand Down Expand Up @@ -388,8 +388,8 @@ async def _get_dataset_raise_otherwise(
@router.get("/features/{dataset_id}", response_model_exclude_none=True)
async def get_dataset_features(
dataset_id: Identifier,
expdb: Annotated[AsyncSession, Depends(expdb_session)],
user: Annotated[User | None, Depends(fetch_user)] = None,
expdb: Annotated[AsyncSession, Depends(expdb_session, scope="function")],
user: Annotated[User | None, Depends(fetch_user, scope="function")] = None,
) -> list[Feature]:
"""Return metadata for each feature (column) in the dataset."""
assert expdb is not None # noqa: S101
Expand Down Expand Up @@ -432,7 +432,7 @@ async def update_dataset_status(
dataset_id: Annotated[Identifier, Body()],
status: Annotated[Literal[DatasetStatus.ACTIVE, DatasetStatus.DEACTIVATED], Body()],
user: Annotated[User, Depends(fetch_user_or_raise)],
expdb: Annotated[AsyncSession, Depends(expdb_session)],
expdb: Annotated[AsyncSession, Depends(expdb_session, scope="function")],
) -> dict[str, str | int]:
"""Update the status of the dataset. Can be used to deactivate a dataset."""
dataset = await _get_dataset_raise_otherwise(dataset_id, user, expdb)
Expand Down Expand Up @@ -485,9 +485,9 @@ async def update_dataset_status(
)
async def get_dataset(
dataset_id: Identifier,
userdb_session: Annotated[AsyncSession, Depends(userdb_session)],
expdb_session: Annotated[AsyncSession, Depends(expdb_session)],
user: Annotated[User | None, Depends(fetch_user)] = None,
userdb_session: Annotated[AsyncSession, Depends(userdb_session, scope="function")],
expdb_session: Annotated[AsyncSession, Depends(expdb_session, scope="function")],
user: Annotated[User | None, Depends(fetch_user, scope="function")] = None,
) -> DatasetMetadata:
"""Get the user-provided metadata for a dataset."""
dataset = await _get_dataset_raise_otherwise(dataset_id, user, expdb_session)
Expand Down
4 changes: 2 additions & 2 deletions src/routers/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ async def userdb_session() -> AsyncIterator[AsyncSession]:

async def fetch_user(
api_key: APIKey | None = None,
user_data: Annotated[AsyncSession | None, Depends(userdb_session)] = None,
user_data: Annotated[AsyncSession | None, Depends(userdb_session, scope="function")] = None,
) -> AsyncGenerator[User | None]:
if not (api_key and user_data):
yield None
Expand All @@ -53,7 +53,7 @@ async def fetch_user(


def fetch_user_or_raise(
user: Annotated[User | None, Depends(fetch_user)] = None,
user: Annotated[User | None, Depends(fetch_user, scope="function")] = None,
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
) -> User:
if user is None:
logger.info("Unauthenticated user tried to access endpoint that requires authentication.")
Expand Down
2 changes: 1 addition & 1 deletion src/routers/estimation_procedure.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

@router.get("/list", response_model_exclude_none=True)
async def get_estimation_procedures(
expdb: Annotated[AsyncSession, Depends(expdb_session)],
expdb: Annotated[AsyncSession, Depends(expdb_session, scope="function")],
) -> list[EstimationProcedure]:
"""Return a list with descriptions of estimation procedures.

Expand Down
2 changes: 1 addition & 1 deletion src/routers/evaluations.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

@router.get("/list")
async def get_evaluation_measures(
expdb: Annotated[AsyncSession, Depends(expdb_session)],
expdb: Annotated[AsyncSession, Depends(expdb_session, scope="function")],
) -> list[str]:
"""Return a list of evaluation measure names.

Expand Down
4 changes: 2 additions & 2 deletions src/routers/flows.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
async def flow_exists(
name: str,
external_version: str,
expdb: Annotated[AsyncSession, Depends(expdb_session)],
expdb: Annotated[AsyncSession, Depends(expdb_session, scope="function")],
) -> dict[Literal["flow_id"], int]:
"""Check if a Flow with the name and version exists, if so, return the flow id."""
flow = await database.flows.get_by_name(
Expand All @@ -38,7 +38,7 @@ async def flow_exists(
@router.get("/{flow_id}")
async def get_flow(
flow_id: Identifier,
expdb: Annotated[AsyncSession, Depends(expdb_session)],
expdb: Annotated[AsyncSession, Depends(expdb_session, scope="function")],
) -> Flow:
"""Get a Flow by its identifier."""
flow = await database.flows.get(flow_id, expdb)
Expand Down
6 changes: 3 additions & 3 deletions src/routers/qualities.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@

@router.get("/qualities/list")
async def list_qualities(
expdb: Annotated[AsyncSession, Depends(expdb_session)],
expdb: Annotated[AsyncSession, Depends(expdb_session, scope="function")],
) -> dict[Literal["data_qualities_list"], dict[Literal["quality"], list[str]]]:
"""List names of all computed qualities (dataset metafeatures)."""
qualities = await database.qualities.list_all_qualities(session=expdb)
Expand All @@ -44,8 +44,8 @@ async def list_qualities(
@router.get("/qualities/{dataset_id}")
async def get_qualities(
dataset_id: Identifier,
user: Annotated[User | None, Depends(fetch_user)],
expdb: Annotated[AsyncSession, Depends(expdb_session)],
user: Annotated[User | None, Depends(fetch_user, scope="function")],
expdb: Annotated[AsyncSession, Depends(expdb_session, scope="function")],
) -> list[Quality]:
"""Get computed qualities (metafeatures) for a dataset."""
dataset = await database.datasets.get(dataset_id, expdb)
Expand Down
6 changes: 3 additions & 3 deletions src/routers/runs.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
@router.get("/trace/{run_id}")
async def get_run_trace(
run_id: Identifier,
expdb: Annotated[AsyncSession, Depends(expdb_session)],
expdb: Annotated[AsyncSession, Depends(expdb_session, scope="function")],
) -> RunTrace:
"""Get trace data for a run by run ID."""
if not await database.runs.exist(run_id, expdb):
Expand Down Expand Up @@ -135,8 +135,8 @@ def _normalise_value(v: object) -> object:
@router.get("/{run_id}", response_model_exclude_none=True)
async def get_run(
run_id: Identifier,
expdb: Annotated[AsyncSession, Depends(expdb_session)],
userdb: Annotated[AsyncSession, Depends(userdb_session)],
expdb: Annotated[AsyncSession, Depends(expdb_session, scope="function")],
userdb: Annotated[AsyncSession, Depends(userdb_session, scope="function")],
) -> Run:
"""Get full metadata for a run by ID."""
# Authentication is not required because runs are always public.
Expand Down
6 changes: 3 additions & 3 deletions src/routers/setups.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
@router.get(path="/{setup_id}", response_model_exclude_none=True)
async def get_setup(
setup_id: Annotated[Identifier, Path()],
expdb_session: Annotated[AsyncSession, Depends(expdb_session)],
expdb_session: Annotated[AsyncSession, Depends(expdb_session, scope="function")],
) -> SetupResponse:
"""Get setup by id."""
setup = await database.setups.get(setup_id, expdb_session)
Expand All @@ -51,7 +51,7 @@ async def tag_setup(
setup_id: Annotated[Identifier, Body()],
tag: Annotated[TagString, Body()],
user: Annotated[User, Depends(fetch_user_or_raise)],
expdb_session: Annotated[AsyncSession, Depends(expdb_session)],
expdb_session: Annotated[AsyncSession, Depends(expdb_session, scope="function")],
) -> dict[str, dict[str, str | list[str]]]:
"""Add a tag to the setup, this tag is publicly visible to all users."""
try:
Expand All @@ -75,7 +75,7 @@ async def untag_setup(
setup_id: Annotated[Identifier, Body()],
tag: Annotated[TagString, Body()],
user: Annotated[User, Depends(fetch_user_or_raise)],
expdb_session: Annotated[AsyncSession, Depends(expdb_session)],
expdb_session: Annotated[AsyncSession, Depends(expdb_session, scope="function")],
) -> dict[str, dict[str, str | list[str]]]:
"""Remove tag `tag` from setup with id `setup_id`."""
# Setups don't really have an owner, they are associated with runs.
Expand Down
8 changes: 4 additions & 4 deletions src/routers/study.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ async def attach_to_study(
study_id: Annotated[Identifier, Body()],
entity_ids: Annotated[list[Identifier], Body()],
user: Annotated[User, Depends(fetch_user_or_raise)],
expdb: Annotated[AsyncSession, Depends(expdb_session)],
expdb: Annotated[AsyncSession, Depends(expdb_session, scope="function")],
) -> AttachDetachResponse:
"""Add runs or tasks to a study which is in preparation."""
if user is None:
Expand Down Expand Up @@ -125,7 +125,7 @@ async def attach_to_study(
async def create_study(
study: CreateStudy,
user: Annotated[User, Depends(fetch_user_or_raise)],
expdb: Annotated[AsyncSession, Depends(expdb_session)],
expdb: Annotated[AsyncSession, Depends(expdb_session, scope="function")],
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Resolved
) -> dict[Literal["study_id"], int]:
"""Create a new study."""
if study.main_entity_type == StudyType.RUN and study.tasks:
Expand Down Expand Up @@ -161,8 +161,8 @@ async def create_study(
@router.get("/{alias_or_id}")
async def get_study(
alias_or_id: Identifier | str,
expdb: Annotated[AsyncSession, Depends(expdb_session)],
user: Annotated[User | None, Depends(fetch_user)] = None,
expdb: Annotated[AsyncSession, Depends(expdb_session, scope="function")],
user: Annotated[User | None, Depends(fetch_user, scope="function")] = None,
) -> Study:
"""Get a study by id or alias."""
study = await _get_study_raise_otherwise(alias_or_id, user, expdb)
Expand Down
6 changes: 3 additions & 3 deletions src/routers/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ async def tag_task(
task_id: Annotated[Identifier, Body()],
tag: Annotated[TagString, Body()],
user: Annotated[User, Depends(fetch_user_or_raise)],
expdb_session: Annotated[AsyncSession, Depends(expdb_session)],
expdb_session: Annotated[AsyncSession, Depends(expdb_session, scope="function")],
) -> dict[str, dict[str, Any]]:
"""Add a tag to the task, this tag is publicly visible to all users."""
try:
Expand Down Expand Up @@ -259,7 +259,7 @@ def _quality_clause(quality: str, range_: str | None) -> str:
@router.get(path="/list")
async def list_tasks( # noqa: PLR0913, PLR0912, C901, PLR0915
*,
expdb: Annotated[AsyncSession, Depends(expdb_session)],
expdb: Annotated[AsyncSession, Depends(expdb_session, scope="function")],
pagination: Annotated[Pagination, Body(default_factory=Pagination)],
task_type_id: Annotated[Identifier | None, Body(description="Filter by task type id.")] = None,
tag: Annotated[TagString | None, Body()] = None,
Expand Down Expand Up @@ -450,7 +450,7 @@ async def list_tasks( # noqa: PLR0913, PLR0912, C901, PLR0915
@router.get("/{task_id}")
async def get_task(
task_id: Identifier,
expdb: Annotated[AsyncSession, Depends(expdb_session)],
expdb: Annotated[AsyncSession, Depends(expdb_session, scope="function")],
) -> Task:
"""Get a task by identifier."""
if not (task := await database.tasks.get(task_id, expdb)):
Expand Down
4 changes: 2 additions & 2 deletions src/routers/tasktype.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def _normalize_task_type(task_type: Row[Any]) -> dict[str, str | list[Any] | Non

@router.get(path="/list")
async def list_task_types(
expdb: Annotated[AsyncSession, Depends(expdb_session)],
expdb: Annotated[AsyncSession, Depends(expdb_session, scope="function")],
) -> dict[
Literal["task_types"],
dict[Literal["task_type"], list[dict[str, str | list[Any] | None]]],
Expand All @@ -52,7 +52,7 @@ async def list_task_types(
@router.get(path="/{task_type_id}")
async def get_task_type(
task_type_id: Identifier,
expdb: Annotated[AsyncSession, Depends(expdb_session)],
expdb: Annotated[AsyncSession, Depends(expdb_session, scope="function")],
) -> dict[Literal["task_type"], dict[str, str | list[str] | list[dict[str, str]] | None]]:
"""Return a detailed description for the given task type, including expected inputs."""
task_type_record = await db_get_task_type(task_type_id, expdb)
Expand Down
4 changes: 2 additions & 2 deletions src/routers/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
async def delete_user_account(
user_id: Annotated[int, Path(description="Numeric user id to delete.", gt=0)],
current_user: Annotated[User, Depends(fetch_user_or_raise)],
expdb: Annotated[AsyncSession, Depends(expdb_session)],
userdb: Annotated[AsyncSession, Depends(userdb_session)],
expdb: Annotated[AsyncSession, Depends(expdb_session, scope="function")],
userdb: Annotated[AsyncSession, Depends(userdb_session, scope="function")],
) -> Response:
"""Delete the user account if they have no associated resources.

Expand Down
Loading