Functional Weave
Code in Python

todo.list@1.1.0

impl/python/update_todo.py

931 bytes · the Python implementation · view raw

Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.

from dataclasses import replace
from typing import List, Sequence

from .todo_item import Todo, TodoDraft  ← from todo.item ^1.0.0 · built alongside by fune
from .todo_list_add_todo import checked_draft  ← addTodo, another function of this group · built into the same file, even by a slim install


def index_of_todo(todos: Sequence[Todo], id: str) -> int:
    """The position of the todo with this id, or the unknown-id error every list operation shares."""
    for i, t in enumerate(todos):
        if t.id == id:
            return i
    raise ValueError(f'no todo with id "{id}"')


def update_todo(todos: Sequence[Todo], id: str, draft: TodoDraft) -> List[Todo]:
    """Save the edit form; id, done state, timestamps, order and place are kept."""
    at = index_of_todo(todos, id)
    clean = checked_draft(draft)
    out = list(todos)
    out[at] = replace(
        todos[at],
        title=clean.title,
        notes=clean.notes,
        priority=clean.priority,
        due=clean.due,
        tags=clean.tags,
        recurrence=clean.recurrence,
    )
    return out