The use of contextvar.set(old_value)
always seemed unsafe to me. But after looking at it closer, it seems actually perfectly safe when used to implement a context manager?
There are some advantages to tokens (sanity checking, unsetting), but changing API that currently has:
old_state = set_state(new)
# reset again:
set_state(old_state)
is churn and I would not do it unless there is some (potentially long-term!) advantage to use token
s beyond sanity checking.