"Si un ouvrier veut bien faire son travail, il doit d'abord affûter ses outils." - Confucius, "Les Entretiens de Confucius. Lu Linggong"
Page de garde > La programmation > Filtre d'avertissement en Python

Filtre d'avertissement en Python

Publié le 2024-11-08
Parcourir:371

Warning Filter in Python

Buy Me a Coffee☕

*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, , '__main__', 0),
#  ('ignore', None, , None, 0),
#  ('ignore', None, , None, 0),
#  ('ignore', None, , None, 0),
#  ('ignore', None, , 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's False, a filter is add before other filters while if it's True, 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's False, a filter is add before other filters while if it's True, 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, , 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, , None, 0)]

import file1, file2
# ...\my_project\file1.py", line 3, in 
#     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, , 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, , 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),
#  , 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, , 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, , 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, , 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, , 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, , None, 0)]

warnings.filterwarnings(action="always", append=False)
warnings.simplefilter(action="always", append=False)

print(warnings.filters)
# [('always', None, , None, 0),
#  ('default', None, , None, 0)]

warnings.filterwarnings(action="once", append=True)
warnings.simplefilter(action="once", append=True)

print(warnings.filters)
# [('always', None, , None, 0),
#  ('default', None, , None, 0),
#  ('once', None, , None, 0)]
Déclaration de sortie Cet article est reproduit sur : https://dev.to/hyperkai/warning-filter-in-python-4g0e?1 En cas de violation, veuillez contacter [email protected] pour le supprimer.
Dernier tutoriel Plus>

Clause de non-responsabilité: Toutes les ressources fournies proviennent en partie d'Internet. En cas de violation de vos droits d'auteur ou d'autres droits et intérêts, veuillez expliquer les raisons détaillées et fournir une preuve du droit d'auteur ou des droits et intérêts, puis l'envoyer à l'adresse e-mail : [email protected]. Nous nous en occuperons pour vous dans les plus brefs délais.

Copyright© 2022 湘ICP备2022001581号-3