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 |
186 The warnings filter is initialized by :option:`-W` options passed to the Python
189 interpretation in :data:`sys.warnoptions`; the :mod:`warnings` module parses these
193 Individual warnings filters are specified as a sequence of fields separated by
205 Commonly used warning filters apply to either all warnings, warnings in a
206 particular category, or warnings raised by particular modules or packages.
209 default # Show all warnings (even those ignored by default)
210 ignore # Ignore all warnings
211 error # Convert all warnings to errors
214 ignore,default:::mymodule # Only report warnings triggered by "mymodule"
215 error:::mymodule # Convert warnings to errors in "mymodule"
258 warnings from their users by default, and only display them when running tests
261 indicate whether or not warnings should be disabled::
266 import warnings
267 warnings.simplefilter("ignore")
270 *all* warnings are displayed by default for the code under test, using code
276 import os, warnings
277 warnings.simplefilter("default") # Change the filter in this process
285 import warnings
286 warnings.filterwarnings("default", category=DeprecationWarning,
292 Temporarily Suppressing Warnings
296 function, but do not want to see the warning (even when warnings have been
300 import warnings
303 warnings.warn("deprecated", DeprecationWarning)
305 with warnings.catch_warnings():
306 warnings.simplefilter("ignore")
309 While within the context manager all warnings will simply be ignored. This
320 Testing Warnings
323 To test warnings raised by code, use the :class:`catch_warnings` context
324 manager. With it you can temporarily mutate the warnings filter to facilitate
325 your testing. For instance, do the following to capture all raised warnings to
328 import warnings
331 warnings.warn("deprecated", DeprecationWarning)
333 with warnings.catch_warnings(record=True) as w:
334 # Cause all warnings to always be triggered.
335 warnings.simplefilter("always")
343 One can also cause all warnings to be exceptions by using ``error`` instead of
346 set the warning will not be seen again unless the warnings registry related to
349 Once the context manager exits, the warnings filter is restored to its state
350 when the context was entered. This prevents tests from changing the warnings
359 a new warning (e.g. set warnings to be raised as exceptions and check the
362 entries from the warnings list before each new operation).
375 to test their code with typically ignored warnings made visible in order to
380 will take care of implicitly enabling all warnings when running tests
386 the environment. This enables default handling for all warnings, including those
388 warnings you can change what argument is passed to :option:`-W` (e.g.
407 :ref:`warnings filter <warning-filter>`. The *stacklevel* argument can be used by wrapper
411 warnings.warn(message, DeprecationWarning, stacklevel=2)
452 this function with any callable by assigning to ``warnings.showwarning``.
469 Insert an entry into the list of :ref:`warnings filter specifications
473 inserts them as a tuple in the list of warnings filters. Entries closer to
481 Insert a simple entry into the list of :ref:`warnings filter specifications
490 Reset the warnings filter. This discards the effect of all previous calls to
500 A context manager that copies and, upon exit, restores the warnings filter
510 module returned when you import :mod:`warnings` whose filter will be
511 protected. This argument exists primarily for testing the :mod:`warnings`