Skip to content

Clear notetypes cache on import #3969

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 8, 2025

Conversation

abdnh
Copy link
Collaborator

@abdnh abdnh commented Apr 29, 2025

Noticed this is needed after importing a deck with merge_notetypes enabled (which added a new template to a notetype) then opening the Card Types screen and getting this error:

Traceback (most recent call last):
  File "D:\dev\anki\repo\qt\aqt\webview.py", line 747, in handler
    cb(val)
  File "D:\dev\anki\repo\qt\aqt\editor.py", line 649, in <lambda>
    self.web.evalWithCallback("saveNow(%d)" % keepFocus, lambda res: callback())
  File "D:\dev\anki\repo\qt\aqt\editor.py", line 380, in _onCardLayout
    CardLayout(
  File "D:\dev\anki\repo\qt\aqt\clayout.py", line 92, in __init__
    self.redraw_everything()
  File "D:\dev\anki\repo\qt\aqt\clayout.py", line 106, in redraw_everything
    self.update_current_ordinal_and_redraw(self.ord)
  File "D:\dev\anki\repo\qt\aqt\clayout.py", line 113, in update_current_ordinal_and_redraw
    self.fill_fields_from_template()
  File "D:\dev\anki\repo\qt\aqt\clayout.py", line 488, in fill_fields_from_template
    t = self.current_template()
  File "D:\dev\anki\repo\qt\aqt\clayout.py", line 485, in current_template
    return self.templates[self.ord]
IndexError: list index out of range

I guess any backend operation on note types should invalidate the cache. We can add this to AnkiQt.on_operation_did_execute() but then the cache will be less useful when there are any updates:

if changes.notetype:
    self.col.models._clear_cache()

@dae
Copy link
Member

dae commented May 5, 2025

I think your other proposal might be a better option Abdo. Explicitly clearly the cache on import doesn't solve the inverse problem, where the user undoes the import again, and there may be other situations where we should be invalidating the cache but are not. I'm not so worried about a less-useful cache - unless the notetype is being rapidly updated, the occasional extra cache miss is unlikely to be perceptible. WDYT?

@abdnh
Copy link
Collaborator Author

abdnh commented May 6, 2025

That's reasonable. I forgot about the undo case.

@dae dae merged commit 80618ca into ankitects:main May 8, 2025
1 check passed
@abdnh abdnh deleted the clear-notetypes-cache-on-import branch May 10, 2025 18:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants