mw.col.sched.answerCard Add-on coding problem

I’m currently trying to write an add-on which automatically does the first review of cards added in the Add Cards dialog. I’m not the most experienced with Anki and Python, so I’m having troubles to understand why the following does not work:

mw.col.sched.answerCard(mw.col.getCard(1605091898523), 3)

Note that I just wanted to do a small test with a random card of my collection

Anki 2.1.36 (53a984ba) Python 3.8.0 Qt 5.14.2 PyQt 5.14.2
Platform: Mac 10.15.7
Flags: frz=True ao=True sv=1
Add-ons, last update check: 2020-11-10 13:12:15
Add-ons possibly involved: ⁨add-card-do-first-review⁩

Caught exception:
Traceback (most recent call last):
  File "anki/sched.py", line 368, in _logLrn
  File "anki/sched.py", line 363, in log
  File "anki/cards.py", line 184, in timeTaken
TypeError: unsupported operand type(s) for -: 'float' and 'NoneType'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "aqt/webview.py", line 508, in handler
  File "aqt/editor.py", line 483, in <lambda>
  File "aqt/addcards.py", line 191, in _addCards
  File "aqt/addcards.py", line 183, in addNote
  File "aqt/gui_hooks.py", line 46, in __call__
  File "/Users/p4nix/Library/Application Support/Anki2/addons21/add-card-do-first-review/__init__.py", line 37, in added_note
    mw.col.sched.answerCard(mw.col.getCard(1605091898523), 3)
  File "anki/sched.py", line 75, in answerCard
  File "anki/sched.py", line 275, in _answerLrnCard
  File "anki/sched.py", line 372, in _logLrn
  File "anki/sched.py", line 363, in log
  File "anki/cards.py", line 184, in timeTaken
TypeError: unsupported operand type(s) for -: 'float' and 'NoneType'

Whereas mw.col.sched.answerCard(mw.col.sched.getCard(), 3) works for rescheduling the latest card in the scheduler?

1 Like

This seems to be caused by the Card.timerStarted attribute not being initialized through Card.startTimer for cards obtained through mw.col.getCard, while it’s initialized for mw.col.sched.getCard as can be seen here.

Also see https://github.com/ankitects/anki/blob/f8f2839b55ec01c8796727ec5b9773e49272e9e7/pylib/anki/cards.py#L184 for the source of the error.

Looks like a bug.

1 Like

I forgot to mention that you can work around this by explicitly calling startTimer before answerCard:

card = mw.col.getCard(1605091898523)
card.startTimer()
mw.col.sched.answerCard(card, 3)
3 Likes

Thanks, already figured that out, wasn’t able to post yet :slight_smile:

(Too excited writing the remaining code)

1 Like

( https://ankiweb.net/shared/info/1394953283 )

1 Like