PyPDF4 icon indicating copy to clipboard operation
PyPDF4 copied to clipboard

Assertions vs. raising exceptions

Open acsor opened this issue 7 years ago • 1 comments

While deploying unit tests for filters.py I have noticed that the use of assert is much more frequent than a reasonable exception. AFAIK, even in Python the assert keyword has a wide use in debugging, but not when launching production code.

Take for example filters.py:390 where I would substitute

assert b < (2 ** 32 - 1)

with something like

if b >= (2 ** 32) - 1:
    raise OverflowError("See ISO 32000, p. ...")

Shall I, and all the other developers, take the freedom of changing these occurrences when stumbled upon and when it is reasonable to do so?

acsor avatar Aug 13 '18 14:08 acsor

I fully agree with you. Haven't used or seen such usage in Python outside debugging. Reading through https://mail.python.org/pipermail/python-list/2013-November/660568.html , especially the point that assert can be compiled away (through optimisations) probably is another good argument not to use asserts in this case, unless you don't care about them triggering.

A reasonable and descriptive exception seems way more appropriate to me.

sim0nx avatar Aug 13 '18 23:08 sim0nx