Understanding assert Statements
This behavior can be a common source of confusion and issues, as you’ll learn later on. An
assert statement consists of the
assert keyword, the expressional condition to test, and an optional message.
The condition is always supposed to be true. If the assertion condition is true, then nothing happens, and the program continues its normal execution. On the other hand, if the condition becomes false, then
assert halts the program by raising an
assert is a simple statement with a syntax seen on-screen. Here,
expression can be any valid Python expression or object, which is then tested for truthiness. If
expression is false, then the statement throws an
Note that the
assertion_message argument to
assert is optional. However, it can help you better understand the condition under test and figure out the problem that you are facing. So whenever you use
assert, it’s a good idea to use a descriptive assertion message for the traceback of the
An important point regarding the
assert syntax is the statement does not require a pair of parentheses to group the expression and the optional message. In Python,
assert is a statement, not a function.
This warning has to do with non-empty tuples always being truthy in Python. In this example, the parentheses turn the assertion expression and message into a two-item tuple, which always evaluates to true. Fortunately, recent versions of Python throw a
SyntaxWarning to alert you of this. However, in older versions of the language, an
assert statement as seen on-screen will always succeed.
03:24 This issue often appears when you’re using long expressions or messages that take more than a single line. In these cases, the parentheses are the natural way to format the code, and you may end up with something like the code seen on-screen.
Using a pair of parentheses to split a long line into multiple lines is a common formatting practice in Python code. However, in the context of an assert statement, the parentheses turn the assertion expression and the message into a two-item tuple. In practice, if you want to split a long assertion into several lines, then you can use the backslash character (
\) for explicit line joining. The backslash at the end of the first line of this assertion joins the two physical lines into a single logical line. This way, you can have an appropriate line length without the risk of a warning or a logical error in your code.
04:18 Pep 679 was created in January 2022 and is proposing to allow parentheses around the assertion expression message. If the PEP gets approved and implemented, then the issue of accidental tuples won’t affect Python code in the future.
Why is this happening? To create a single-item tuple, you need to place a comma after the item itself. In the code on-screen, the parentheses by themselves don’t create a tuple, and that’s why the interpreter ignores the parentheses, and
assert works as expected. Even though the parentheses work in this scenario, it’s not a recommended practice. You can end up shooting yourself in the foot.
If the condition of an
assert statement evaluates to false, then
assert raises an
AssertionError. If you provide the optional assertion message, then this message is internally used as an argument to the
AssertionError class. Either way, the raised exception breaks your program’s execution.
Most of the time, you won’t raise
AssertionError exceptions explicitly in your code. The
assert statement takes care of raising this exception when the assertion condition fails. Additionally, as you’ll learn later on, you shouldn’t attempt to handle errors by writing code that catches the
AssertionError is a built-in exception that inherits from the
Exception class and is considered a concrete exception that should be raised instead of subclassed.
Become a Member to join the conversation.