This is not how the filterwarnings works. In the documentation you can see Omitted arguments default to a value that matches everything.
and also you can see:message (default '') : is a string containing a regular expression that start of the warning message must match
.
This can be understood as using action "once" will affect each unique text message to display once. If you have a field in message that may change (for example filename), the warning will be displayed once for each filename.
If you set the message argument, than each matching unique text message will be diplayed once.
Here is a small example:
import warnings
import random
warnings.filterwarnings("ignore", category=UserWarning)
warnings.warn("You won't see this warning")
message_formater = "this message with number {} will be displayed once"
# deactivating previously created filter
warnings.resetwarnings()
# applying new filter
warnings.filterwarnings("once", message_formater.format("(.*)"), category=UserWarning)
for i in range(100):
warnings.warn(message_formater.format(random.randint(0, 3)))
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:8: UserWarning: this message with number 0 will be displayed once
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:8: UserWarning: this message with number 3 will be displayed once
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:8: UserWarning: this message with number 1 will be displayed once
/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py:8: UserWarning: this message with number 2 will be displayed once