-
Notifications
You must be signed in to change notification settings - Fork 62
Description
In some places assert statements are being used to perform some kind of validation, e.g.: type of an argument, value of an argument, etc.
These statements should be replaced with proper validation mechanisms that will raise an appropriate Exception and do whatever is necessary from the point of view of the protocol, because such assert statements may be removed if optimization is turned on. From the Python docs:
The current code generator emits no code for an assert statement when optimization is requested at compile time.
Resources
-
https://wiki.python.org/moin/UsingAssertionsEffectively:
Assertions should not be used to test for failure cases that can occur because of bad user input or operating system/environment failures, such as a file not being found. Instead, you should raise an exception, or print an error message, or whatever is appropriate. One important reason why assertions should only be used for self-tests of the program is that assertions can be disabled at compile time.
-
https://dbader.org/blog/python-assert-tutorial:
... do our validation with regular if-statements and raise validation exceptions if necessary.
-
https://stackoverflow.com/questions/5142418/what-is-the-use-of-assert-in-python