Card.template() does not work when called in card_will_show hook in template editor

Hello,
I develop a: an addon for writing Hanzi in Anki. In the newest version of Anki, I think around 2.1.30, card.template()called by acard_will_show` hook in the template editor starts throwing:

Traceback (most recent call last):
  File "aqt\progress.py", line 43, in handler
  File "aqt\clayout.py", line 468, in _renderPreview
  File "aqt\gui_hooks.py", line 787, in __call__
  File "D:\AnkiData\addons21\maobi\__init__.py", line 31, in my_hook
    sys.stderr.write(str(card.template()))
  File "anki\cards.py", line 155, in template
  File "anki\cards.py", line 146, in note_type
  File "anki\cards.py", line 142, in note
  File "anki\collection.py", line 309, in getNote
  File "anki\notes.py", line 37, in __init__
TypeError: 'NoneType' object is not subscriptable

Minimal addon for testing:

import sys
from aqt import gui_hooks


def my_hook(html, card, context):
    sys.stderr.write(str(card))
    sys.stderr.write("\n")
    sys.stderr.write(str(card.template()))
    sys.stderr.write("\n")


gui_hooks.card_will_show.append(my_hook)

Users reported a bug on my Github, there is a more detailed description. It worked before so I assume that is a regression in Anki itself. I looked in the Anki source but have no idea why the error happens, as it is related to some database access which I am not familiar with.

If I would guess then it looks like ephemeral_card_for_rendering does not set all the required fields and so the card cannot be loaded properly.

Thanks for the report, this will be fixed in the next release.

1 Like