+ def pop(self, amount: int = 4) -> int:
+ """Pop `amount` spaces off of the indent, default four."""
+ if self._level < amount:
+ raise ArithmeticError(
+ "Can't pop {:d} spaces from {:s}".format(amount, repr(self)))
I think assert(amount <= self._level) would do just fine.
Secretly, asserts can be disabled in Python just like they can in C code.
There are compilers that let you switch off array bounds checking.
Would you advocate manual bounds checking to protect people from their
own folly?
My habit: if it's something that should already be true given the
nature of how the code is laid out, use an assertion. If I am
preventing an erroneous state (Especially from callers in other
modules), explicitly raise an exception.
I check function preconditions ruthlessly with assert. There's no sane
way to recover anyway.
Without a way to recover, the only benefit is crashing more prettily.
If the error is six levels deep in a some fancy-pants framework, then
prettier crashes might actually help someone finding the error slightly
faster. But it ain't.
My final argument is local consistency: use of assertions to check
preconditions is pervasive in scripts/qapi/.