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
This commit is contained in:
David Culley 2024-07-21 10:25:48 +02:00 committed by GitHub
parent e213c0a6d1
commit aa6583cdd1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -95,21 +95,28 @@ class Note(DeprecatedNamesMixin):
self, self,
ord: int = 0, ord: int = 0,
*, *,
custom_note_type: NotetypeDict = None, custom_note_type: NotetypeDict | None = None,
custom_template: TemplateDict = None, custom_template: TemplateDict | None = None,
fill_empty: bool = False, fill_empty: bool = False,
) -> anki.cards.Card: ) -> anki.cards.Card:
card = anki.cards.Card(self.col) card = anki.cards.Card(self.col)
card.ord = ord card.ord = ord
card.did = anki.decks.DEFAULT_DECK_ID card.did = anki.decks.DEFAULT_DECK_ID
model = custom_note_type or self.note_type() if custom_note_type is None:
template = copy.copy( model = self.note_type()
custom_template else:
or ( model = custom_note_type
model["tmpls"][ord] if model["type"] == MODEL_STD else model["tmpls"][0] 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 # may differ in cloze case
template["ord"] = card.ord template["ord"] = card.ord