A function compiled with tf.function calls print, which runs while tracing and not on each call

tx0/python:print-in-tf-functionrecommendedwarningframeworks/data-sciencedepth fastbug

What it finds

The Python body of a tf.function runs only when TensorFlow traces it; later calls run the graph, so a print shows up once, with symbolic tensors, and then never again. tf.print becomes part of the graph and prints on every call.

From the rule's source, print_in_tf_function.rs.

Example from the rule's tests
import tensorflow as tf @tf.functiondef step(x):    print("tracing", x)tx0 reports: print runs only while this tf.function is traced    tf.print("value", x)    return x + 1 def eager(x):    print(x)
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the recommended tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/python:print-in-tf-function" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

# tx0: ignore[tx0/python:print-in-tf-function] reason="..."

tx0 explain tx0/python:print-in-tf-function shows the same in the terminal. Found a false positive? Report it.