Correct way to apply arbitrary scheduling changes to a card

In an add-on, I am syncing cards from a different tool into Anki. Some of these cards will already have previous scheduling information, so for these, after adding the cards, I need to update the due date, interval, ease, and lapses to arbitrary values (as determined by the data source). The sample of doing this in the add-on writing guide appears to be completely out of date, so especially with large portions of this moving to Rust and the “set due date” function changing recently, I’m not sure what the best practices are for this.

I got the following test case working in the REPL:

import anki.consts
from anki.notes import Note

n = Note(mw.col, mw.col.models.by_name("Basic"))
n['Front'] = "my front"
n['Back'] = "my back"
deck = mw.col.decks.id("Default")
mw.col.add_note(n, deck)

cids = mw.col.find_cards(f"nid:{n.id}")
c = mw.col.get_card(cids[0])
c.ivl = 6 # days
c.queue = anki.consts.QUEUE_TYPE_REV
c.type = anki.consts.CARD_TYPE_REV
c.factor = 1600 #‰
c.lapses = 3
mw.col.update_card(c)
# in the real code, we'll calculate the next due date by subtracting today's date from the current due date (stored as a datetime), rather than hard-coding 150 days from today
mw.col.sched.set_due_date([c.id], "150") 

Is this a sensible approach, or am I going to get myself in trouble by directly updating these attributes in Python?

Related question: if I need to set a due date in the past (because the card being imported is overdue), how can I do that? When I try to pass a negative number to set_due_date(), I get an anki.errors.InvalidInput exception.

Since you’re manually modifying the other card properties, it’s probably simpler to just update card.due directly as well, based on col.sched.today(). There isn’t an API to apply arbitrary scheduling to cards at the moment, so direct property access is still required.

Thanks, I couldn’t figure out where to get the integer to start my date math from. col.sched.today() works great and solves the InvalidInput exception.