Skip to content

assert-with-print-message (RUF030)#

Fix is always available.

This rule is unstable and in preview. The --preview flag is required for use.

What it does#

Checks for uses of assert expression, print(message).

Why is this bad?#

The return value of the second expression is used as the contents of the AssertionError raised by the assert statement. Using a print expression in this context will output the message to stdout, before raising an empty AssertionError(None).

Instead, remove the print and pass the message directly as the second expression, allowing stderr to capture the message in a well-formatted context.

Example#

assert False, print("This is a message")

Use instead:

assert False, "This is a message"

Fix safety#

This rule's fix is marked as unsafe, as changing the second expression will result in a different AssertionError message being raised, as well as a change in stdout output.

References#