Annotate list link attrs with Sequence type

This is beneficial as lists are invariant and we're using a union type
here with User | Link[User], which means we can't assign a list[User] to
the attr, which is very annoying.
This commit is contained in:
Peter Vacho 2024-12-25 20:46:38 +01:00
parent 8737897ce9
commit 8e82d7b661
Signed by: school
GPG key ID: 8CFC3837052871B4
2 changed files with 6 additions and 3 deletions

View file

@ -160,7 +160,9 @@ async def delete_category(category_id: PydanticObjectId, user: CurrentUserDep) -
# and it's far more sensible solution than deleting all events with this category)
events = await Event.find(category in Event.categories).to_list()
for event in events:
event.categories.remove(category)
new_categories = list(event.categories)
new_categories.remove(category)
event.categories = new_categories
await Event.replace_many(events)
# We can now safely delete the category

View file

@ -1,3 +1,4 @@
from collections.abc import Sequence
from datetime import UTC, date, datetime, time
from typing import Annotated, ClassVar, final
@ -15,13 +16,13 @@ class Event(Document):
user: Annotated[User, Annotated[Link[User], Indexed()]]
title: str
description: str
categories: Annotated[list[Category | Link[Category]], Annotated[list[Link[Category]], Indexed()]]
categories: Annotated[Sequence[Category | Link[Category]], Annotated[list[Link[Category]], Indexed()]]
start_date: Annotated[date, Indexed()]
start_time: time
end_date: Annotated[date, Indexed()]
end_time: time
color: str # TODO: Consider using a complex rgb type
attendees: Annotated[list[User | Link[User]], Annotated[list[Link[User]], Indexed()]]
attendees: Annotated[Sequence[User | Link[User]], Annotated[list[Link[User]], Indexed()]]
created_at: datetime = Field(default_factory=lambda: datetime.now(UTC))
@final