From aa6583cdd13caee18f85414c1efd942cd9d0033e Mon Sep 17 00:00:00 2001 From: David Culley <6276049+davidculley@users.noreply.github.com> Date: Sun, 21 Jul 2024 10:25:48 +0200 Subject: [PATCH] Refactor `ephemeral_card` of notes (#3307) * refactor: fix type annotation * fix: properly check if argument is None Don't use Boolean expressions to implement a default value. * fix: ensure that 'model' is not None Don't use exceptions to control the flow. * refactor: simplify if-else construct --- pylib/anki/notes.py | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/pylib/anki/notes.py b/pylib/anki/notes.py index f3a51db19..5de95bfb6 100644 --- a/pylib/anki/notes.py +++ b/pylib/anki/notes.py @@ -95,21 +95,28 @@ class Note(DeprecatedNamesMixin): self, ord: int = 0, *, - custom_note_type: NotetypeDict = None, - custom_template: TemplateDict = None, + custom_note_type: NotetypeDict | None = None, + custom_template: TemplateDict | None = None, fill_empty: bool = False, ) -> anki.cards.Card: card = anki.cards.Card(self.col) card.ord = ord card.did = anki.decks.DEFAULT_DECK_ID - model = custom_note_type or self.note_type() - template = copy.copy( - custom_template - or ( - model["tmpls"][ord] if model["type"] == MODEL_STD else model["tmpls"][0] - ) - ) + if custom_note_type is None: + model = self.note_type() + else: + model = custom_note_type + if model is None: + raise NotImplementedError + + if custom_template is not None: + template = custom_template + elif model["type"] == MODEL_STD: + template = model["tmpls"][ord] + else: + template = model["tmpls"][0] + template = copy.copy(template) # may differ in cloze case template["ord"] = card.ord