[Python-ideas] Type hints for functions with side-effects and for functions raising exceptions
Chris Angelico
rosuav at gmail.com
Tue Feb 19 18:25:51 EST 2019
On Wed, Feb 20, 2019 at 10:18 AM Steven D'Aprano <steve at pearwood.info> wrote:
> So the promises made will nearly always be incredibly weak:
>
> - raises(A) means "may raise A, or any other unexpected exception"
> - sideeffects(True) means "may have expected side-effects"
> - sideeffects(False) means "may have unexpected side-effects"
Perhaps, but sometimes it's nice to know that a function is not
intended to have any side effects. For instance, I'm currently working
on some code that has a bunch of lines like this:
type = _decode("WeaponTypes" if is_weapon else "ItemTypes")
balance = _decode("BalanceDefs")
brand = _decode("Manufacturers")
Suppose we decide that the "balance" variable isn't being used
anywhere. Is it okay to not decode the BalanceDefs? Nope, it isn't,
because the decoding is done sequentially, which means that omitting
one will leave all the subsequent ones desynchronized. Knowing that a
function has no intentional side effects (which is weaker than
declaring that it's a pure function) may well be useful when
refactoring a messy codebase.
(A messy codebase, such as the one that I was reading through when
building that code. It's not code I'm intrinsically proud of, but I
can say with confidence that it is WAY cleaner than the reference
code. And if I'd been able to ascertain which functions were pure, or
at least side-effect-free, it would have saved me a lot of hassle.)
ChrisA
More information about the Python-ideas
mailing list