I’ve been getting familiar with Anki’s python library (anki). IIUC, the pattern is generally: new_* followed by add_*. For example, new_note followed by add_note. I’ve found that the add_* operations mutate the given object to set its id. For example, add_note sets note.id [0].
I was surprised to find that new_deck and add_deck violate this pattern: add_deck does not update the given deck’s id [1]. This would be pretty simple to do:
diff --git a/pylib/anki/decks.py b/pylib/anki/decks.py
index c53275ae3..ad39b9e33 100644
--- a/pylib/anki/decks.py
+++ b/pylib/anki/decks.py
@@ -168,7 +168,9 @@ def new_deck(self) -> Deck:
return self.col._backend.new_deck()
def add_deck(self, deck: Deck) -> OpChangesWithId:
- return self.col._backend.add_deck(message=deck)
+ add_op = self.col._backend.add_deck(message=deck)
+ deck.id = add_op.id
+ return add_op
def new_deck_legacy(self, filtered: bool) -> DeckDict:
deck = from_json_bytes(self.col._backend.new_deck_legacy(filtered))
Would this be an acceptable change? Pretty minor, but it took me an embarrassingly long time to figure out why the notes I was creating weren’t getting added to the deck I created.
[0]: https://github.com/ankitects/anki/blob/25.09.2/pylib/anki/collection.py#L534
[1]: https://github.com/ankitects/anki/blob/25.09.2/pylib/anki/decks.py#L171