`anki.decks.DeckManager::add_deck` does not update the deck's `id`

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

I think that’s intentional. The Rust backend also normalizes the name.
Newer APIs (the ones returning OpChanges*) generally require the caller to reload the object from the database before further operations; see anki.models.ModelManager.add_dict() for example. I agree it’s confusing though – a PR to add a docstring is welcome!

I personally use anki.decks.DeckManager.id() instead most of the time; it’s easier to use and is available in older Anki versions.

Thanks! Docs PR sent in: docs: add warning to `add_deck` that you must refetch after adding by jfly · Pull Request #4760 · ankitects/anki · GitHub