• Home
  • Raw
  • Download

Lines Matching full:warnings

1 :mod:`warnings` --- Warning control
4 .. module:: warnings
7 **Source code:** :source:`Lib/warnings.py`
9 .. index:: single: warnings
18 Python programmers issue warnings by calling the :func:`warn` function defined
23 can be changed flexibly, from ignoring all warnings to turning them into
24 exceptions. The disposition of warnings can vary based on the :ref:`warning category
44 :func:`logging.captureWarnings` allows you to handle all warnings with
54 This categorization is useful to be able to filter out groups of warnings.
58 documented here, because conceptually they belong to the warnings mechanism.
64 The following warnings category classes are currently defined:
77 | :exc:`DeprecationWarning` | Base category for warnings about deprecated |
78 | | features when those warnings are intended for |
82 | :exc:`SyntaxWarning` | Base category for warnings about dubious |
85 | :exc:`RuntimeWarning` | Base category for warnings about dubious |
88 | :exc:`FutureWarning` | Base category for warnings about deprecated |
89 | | features when those warnings are intended for |
93 | :exc:`PendingDeprecationWarning` | Base category for warnings about features |
97 | :exc:`ImportWarning` | Base category for warnings triggered during |
101 | :exc:`UnicodeWarning` | Base category for warnings related to |
104 | :exc:`BytesWarning` | Base category for warnings related to |
107 | :exc:`ResourceWarning` | Base category for warnings related to |
115 intended audience and the way they're handled by the default warnings
121 The Warnings Filter
124 The warnings filter controls whether warnings are ignored, displayed, or turned
127 Conceptually, the warnings filter maintains an ordered list of filter
139 | | warnings for each location (module + |
142 | ``"error"`` | turn matching warnings into exceptions |
144 | ``"ignore"`` | never print matching warnings |
146 | ``"always"`` | always print matching warnings |
149 | | warnings for each module where the warning |
153 | | warnings, regardless of location |
181 The warnings filter is initialized by :option:`-W` options passed to the Python
184 interpretation in :data:`sys.warnoptions`; the :mod:`warnings` module parses these
188 Individual warnings filters are specified as a sequence of fields separated by
200 Commonly used warning filters apply to either all warnings, warnings in a
201 particular category, or warnings raised by particular modules or packages.
204 default # Show all warnings (even those ignored by default)
205 ignore # Ignore all warnings
206 error # Convert all warnings to errors
209 ignore,default:::mymodule # Only report warnings triggered by "mymodule"
210 error:::mymodule[.*] # Convert warnings to errors in "mymodule"
254 warnings from their users by default, and only display them when running tests
257 indicate whether or not warnings should be disabled::
262 import warnings
263 warnings.simplefilter("ignore")
266 *all* warnings are displayed by default for the code under test, using code
272 import os, warnings
273 warnings.simplefilter("default") # Change the filter in this process
281 import warnings
282 warnings.filterwarnings("default", category=DeprecationWarning,
288 Temporarily Suppressing Warnings
292 function, but do not want to see the warning (even when warnings have been
296 import warnings
299 warnings.warn("deprecated", DeprecationWarning)
301 with warnings.catch_warnings():
302 warnings.simplefilter("ignore")
305 While within the context manager all warnings will simply be ignored. This
316 Testing Warnings
319 To test warnings raised by code, use the :class:`catch_warnings` context
320 manager. With it you can temporarily mutate the warnings filter to facilitate
321 your testing. For instance, do the following to capture all raised warnings to
324 import warnings
327 warnings.warn("deprecated", DeprecationWarning)
329 with warnings.catch_warnings(record=True) as w:
330 # Cause all warnings to always be triggered.
331 warnings.simplefilter("always")
339 One can also cause all warnings to be exceptions by using ``error`` instead of
342 set the warning will not be seen again unless the warnings registry related to
345 Once the context manager exits, the warnings filter is restored to its state
346 when the context was entered. This prevents tests from changing the warnings
355 a new warning (e.g. set warnings to be raised as exceptions and check the
358 entries from the warnings list before each new operation).
371 to test their code with typically ignored warnings made visible in order to
376 will take care of implicitly enabling all warnings when running tests
382 the environment. This enables default handling for all warnings, including those
384 warnings you can change what argument is passed to :option:`-W` (e.g.
403 :ref:`warnings filter <warning-filter>`. The *stacklevel* argument can be used by wrapper
407 warnings.warn(message, DeprecationWarning, stacklevel=2)
448 this function with any callable by assigning to ``warnings.showwarning``.
465 Insert an entry into the list of :ref:`warnings filter specifications
469 inserts them as a tuple in the list of warnings filters. Entries closer to
477 Insert a simple entry into the list of :ref:`warnings filter specifications
486 Reset the warnings filter. This discards the effect of all previous calls to
496 A context manager that copies and, upon exit, restores the warnings filter
506 module returned when you import :mod:`warnings` whose filter will be
507 protected. This argument exists primarily for testing the :mod:`warnings`