Warning Filter in Python
Super Kai (Kazuya Ito)
Posted on October 24, 2024
*My post explains Warning with warn() in Python.
A warnings filter can set which warnings to show using these filters(actions
) below. *"once"
may not work properly:
Value | Disposition |
---|---|
"default" |
print the first occurrence of matching warnings for each location (module + line number) where the warning is issued |
"error" | turn matching warnings into exceptions |
"ignore" | never print matching warnings |
"always" | always print matching warnings |
"module" | print the first occurrence of matching warnings for each module where the warning is issued (regardless of line number) |
"once" | print only the first occurrence of matching warnings, regardless of location |
By default, these filters below are set to warnings.filters
and they can be reset with resetwarnings() as shown below:
import warnings
print(warnings.filters)
# [('default', None, <class 'DeprecationWarning'>, '__main__', 0),
# ('ignore', None, <class 'DeprecationWarning'>, None, 0),
# ('ignore', None, <class 'PendingDeprecationWarning'>, None, 0),
# ('ignore', None, <class 'ImportWarning'>, None, 0),
# ('ignore', None, <class 'ResourceWarning'>, None, 0)]
warnings.resetwarnings()
print(warnings.filters)
# []
Now, filterwarnings() or simplefilter() can filter warnings as shown below:
*Memos for filterwarnings()
:
- The 1st argument is
action
(Required-Type:str
). *"default"
,"error"
,"ignore"
,"always"
,"module"
or"once"
can be set. - The 2nd argument is
message
(Optional-Default:""
-Type:str
). - The 3rd argument is
category
(Optional-Default:Warning-Type:Warning
). - The 4th argument is
module
(Optional-Default:""
-Type:str
). *It may not work properly. - The 5th argument is
lineno
(Optional-Default:0
-Type:int
): *Memos:- It decides a line number.
- It must be
x >= 0
.
- The 6th argument is
append
(Optional-Default:False
-Type:bool
). *If it'sFalse
, a filter is add before other filters while if it'sTrue
, a filter is add after other filters.
*Memos for simplefilter()
:
- The 1st argument is
action
(Required-Type:str
). *"default"
,"error"
,"ignore"
,"always"
,"module"
or"once"
can be set. - The 2nd argument is
category
(Optional-Default:Warning-Type:Warning
). - The 3rd argument is
lineno
(Optional-Default:0
-Type:int
): *Memos:- It decides a line number.
- It must be
x >= 0
.
- The 4th argument is
append
(Optional-Default:False
-Type:bool
). *If it'sFalse
, a filter is add before other filters while if it'sTrue
, a filter is add after other filters.
my_project
|-main.py
|-file1.py(module)
└-file2.py(module)
file1.py
:
import warnings # Line 1
# Line 2
warnings.warn(message="Warning 1", category=UserWarning) # Line 3
warnings.warn(message="Warning 2", category=DeprecationWarning) # Line 4
warnings.warn(message="Warning 2", category=DeprecationWarning) # Line 5
warnings.warn(message="Warning 3", category=UserWarning) # Line 6
file2.py
:
import warnings # Line 1
# Line 2
warnings.warn(message="Warning 1", category=UserWarning) # Line 3
warnings.warn(message="Warning 2", category=DeprecationWarning) # Line 4
warnings.warn(message="Warning 2", category=DeprecationWarning) # Line 5
warnings.warn(message="Warning 3", category=UserWarning) # Line 6
main.py
with "default"
filter(action
):
import warnings
warnings.resetwarnings()
warnings.filterwarnings(action="default")
warnings.filterwarnings(action="default", message="", category=Warning,
module="", lineno=0, append=False)
warnings.simplefilter(action="default")
warnings.simplefilter(action="default", category=Warning,
lineno=0, append=False)
print(warnings.filters)
# [('default', None, <class 'Warning'>, None, 0)]
import file1, file2
# ...\my_project\file1.py:3: UserWarning: Warning 1
# warnings.warn(message="Warning 1", category=UserWarning)
# ...\my_project\file1.py:4: DeprecationWarning: Warning 2
# warnings.warn(message="Warning 2", category=DeprecationWarning)
# ...\my_project\file1.py:5: DeprecationWarning: Warning 2
# warnings.warn(message="Warning 2", category=DeprecationWarning)
# ...\my_project\file1.py:6: UserWarning: Warning 3
# warnings.warn(message="Warning 3", category=UserWarning)
# ...\my_project\file2.py:3: UserWarning: Warning 1
# warnings.warn(message="Warning 1", category=UserWarning)
# ...\my_project\file2.py:4: DeprecationWarning: Warning 2
# warnings.warn(message="Warning 2", category=DeprecationWarning)
# ...\my_project\file2.py:5: DeprecationWarning: Warning 2
# warnings.warn(message="Warning 2", category=DeprecationWarning)
# ...\my_project\file2.py:6: UserWarning: Warning 3
# warnings.warn(message="Warning 3", category=UserWarning)
main.py
with "error"
filter(action
):
import warnings
warnings.resetwarnings()
warnings.filterwarnings(action="error")
warnings.simplefilter(action="error")
print(warnings.filters)
# [('error', None, <class 'Warning'>, None, 0)]
import file1, file2
# ...\my_project\file1.py", line 3, in <module>
# warnings.warn(message="Warning 1", category=UserWarning)
# UserWarning: Warning 1
main.py
with "ignore"
filter(action
):
import warnings
warnings.resetwarnings()
warnings.filterwarnings(action="ignore")
warnings.simplefilter(action="ignore")
print(warnings.filters)
# [('ignore', None, <class 'Warning'>, None, 0)]
import file1, file2 # Warnings are not shown
import warnings
warnings.resetwarnings()
warnings.filterwarnings(action="ignore", category=UserWarning)
warnings.simplefilter(action="ignore", category=UserWarning)
print(warnings.filters)
# [('ignore', None, <class 'UserWarning'>, None, 0)]
import file1, file2
# ...\my_project\file1.py:4: DeprecationWarning: Warning 2
# warnings.warn(message="Warning 2", category=DeprecationWarning)
# ...\my_project\file1.py:5: DeprecationWarning: Warning 2
# warnings.warn(message="Warning 2", category=DeprecationWarning)
# ...\my_project\file2.py:4: DeprecationWarning: Warning 2
# warnings.warn(message="Warning 2", category=DeprecationWarning)
# ...\my_project\file2.py:5: DeprecationWarning: Warning 2
# warnings.warn(message="Warning 2", category=DeprecationWarning)
import warnings
warnings.resetwarnings()
warnings.filterwarnings(action="ignore", message="Warning 1",
category=UserWarning)
print(warnings.filters)
# [('ignore', re.compile('Warning 1', re.IGNORECASE),
# <class 'UserWarning'>, None, 0)]
import file1, file2
# ...\my_project\file1.py:4: DeprecationWarning: Warning 2
# warnings.warn(message="Warning 2", category=DeprecationWarning)
# ...\my_project\file1.py:5: DeprecationWarning: Warning 2
# warnings.warn(message="Warning 2", category=DeprecationWarning)
# ...\my_project\file1.py:6: UserWarning: Warning 3
# warnings.warn(message="Warning 3", category=UserWarning)
# ...\my_project\file2.py:4: DeprecationWarning: Warning 2
# warnings.warn(message="Warning 2", category=DeprecationWarning)
# ...\my_project\file2.py:5: DeprecationWarning: Warning 2
# warnings.warn(message="Warning 2", category=DeprecationWarning)
# ...\my_project\file2.py:6: UserWarning: Warning 3
# warnings.warn(message="Warning 3", category=UserWarning)
import warnings
warnings.resetwarnings()
warnings.filterwarnings(action="ignore", lineno=6)
warnings.simplefilter(action="ignore", lineno=6)
print(warnings.filters)
# [('ignore', None, <class 'Warning'>, None, 6)]
import file1, file2
# ...\my_project\file1.py:3: UserWarning: Warning 1
# warnings.warn(message="Warning 1", category=UserWarning)
# ...\my_project\file1.py:4: DeprecationWarning: Warning 2
# warnings.warn(message="Warning 2", category=DeprecationWarning)
# ...\my_project\file1.py:5: DeprecationWarning: Warning 2
# warnings.warn(message="Warning 2", category=DeprecationWarning)
# ...\my_project\file2.py:3: UserWarning: Warning 1
# warnings.warn(message="Warning 1", category=UserWarning)
# ...\my_project\file2.py:4: DeprecationWarning: Warning 2
# warnings.warn(message="Warning 2", category=DeprecationWarning)
# ...\my_project\file2.py:5: DeprecationWarning: Warning 2
# warnings.warn(message="Warning 2", category=DeprecationWarning)
main.py
with "always"
filter(action
):
import warnings
warnings.resetwarnings()
warnings.filterwarnings(action="always")
warnings.simplefilter(action="always")
print(warnings.filters)
# [('always', None, <class 'Warning'>, None, 0)]
import file1, file2
# ...\my_project\file1.py:3: UserWarning: Warning 1
# warnings.warn(message="Warning 1", category=UserWarning)
# ...\my_project\file1.py:4: DeprecationWarning: Warning 2
# warnings.warn(message="Warning 2", category=DeprecationWarning)
# ...\my_project\file1.py:5: DeprecationWarning: Warning 2
# warnings.warn(message="Warning 2", category=DeprecationWarning)
# ...\my_project\file1.py:6: UserWarning: Warning 3
# warnings.warn(message="Warning 3", category=UserWarning)
# ...\my_project\file2.py:3: UserWarning: Warning 1
# warnings.warn(message="Warning 1", category=UserWarning)
# ...\my_project\file2.py:4: DeprecationWarning: Warning 2
# warnings.warn(message="Warning 2", category=DeprecationWarning)
# ...\my_project\file2.py:5: DeprecationWarning: Warning 2
# warnings.warn(message="Warning 2", category=DeprecationWarning)
# ...\my_project\file2.py:6: UserWarning: Warning 3
# warnings.warn(message="Warning 3", category=UserWarning)
main.py
with "module"
filter(action
):
import warnings
warnings.resetwarnings()
warnings.filterwarnings(action="module")
warnings.simplefilter(action="module")
print(warnings.filters)
# [('module', None, <class 'Warning'>, None, 0)]
import file1, file2
# ...\my_project\file1.py:3: UserWarning: Warning 1
# warnings.warn(message="Warning 1", category=UserWarning)
# ...\my_project\file1.py:4: DeprecationWarning: Warning 2
# warnings.warn(message="Warning 2", category=DeprecationWarning)
# ...\my_project\file1.py:6: UserWarning: Warning 3
# warnings.warn(message="Warning 3", category=UserWarning)
# ...\my_project\file2.py:3: UserWarning: Warning 1
# warnings.warn(message="Warning 1", category=UserWarning)
# ...\my_project\file2.py:4: DeprecationWarning: Warning 2
# warnings.warn(message="Warning 2", category=DeprecationWarning)
# ...\my_project\file2.py:6: UserWarning: Warning 3
# warnings.warn(message="Warning 3", category=UserWarning)
main.py
with "once"
filter(action
):
import warnings
warnings.resetwarnings()
warnings.filterwarnings(action="once")
warnings.simplefilter(action="once")
print(warnings.filters)
# [('once', None, <class 'Warning'>, None, 0)]
import file1, file2
# ...\my_project\file1.py:3: UserWarning: Warning 1
# warnings.warn(message="Warning 1", category=UserWarning)
# ...\my_project\file1.py:4: DeprecationWarning: Warning 2
# warnings.warn(message="Warning 2", category=DeprecationWarning)
# ...\my_project\file1.py:6: UserWarning: Warning 3
# warnings.warn(message="Warning 3", category=UserWarning)
# ...\my_project\file2.py:3: UserWarning: Warning 1
# warnings.warn(message="Warning 1", category=UserWarning)
# ...\my_project\file2.py:4: DeprecationWarning: Warning 2
# warnings.warn(message="Warning 2", category=DeprecationWarning)
# ...\my_project\file2.py:6: UserWarning: Warning 3
# warnings.warn(message="Warning 3", category=UserWarning)
main.py
with append
argument. *If append=False
, a filter is add before other filters while if append=True
, a filter is add after other filters:
import warnings
warnings.resetwarnings()
print(warnings.filters)
# []
warnings.filterwarnings(action="default", append=False)
warnings.simplefilter(action="default", append=False)
print(warnings.filters)
# [('default', None, <class 'Warning'>, None, 0)]
warnings.filterwarnings(action="always", append=False)
warnings.simplefilter(action="always", append=False)
print(warnings.filters)
# [('always', None, <class 'Warning'>, None, 0),
# ('default', None, <class 'Warning'>, None, 0)]
warnings.filterwarnings(action="once", append=True)
warnings.simplefilter(action="once", append=True)
print(warnings.filters)
# [('always', None, <class 'Warning'>, None, 0),
# ('default', None, <class 'Warning'>, None, 0),
# ('once', None, <class 'Warning'>, None, 0)]
Posted on October 24, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.