Replace assert statement with exception
This PR addresses an issue highlighted by bandit by replacing use of an assert
statement with a real exception. assert
statements are optimised out of compiled code, see https://docs.python.org/3/reference/simple_stmts.html#the-assert-statement.