Lines Matching full:warnings

1 :mod:`warnings` --- Warning control
4 .. index:: single: warnings
6 .. module:: warnings
12 **Source code:** :source:`Lib/warnings.py`
21 Python programmers issue warnings by calling the :func:`warn` function defined
26 can be changed flexibly, from ignoring all warnings to turning them into
27 exceptions. The disposition of warnings can vary based on the warning category
47 :func:`logging.captureWarnings` allows you to handle all warnings with
57 This categorization is useful to be able to filter out groups of warnings. The
58 following warnings category classes are currently defined:
71 | :exc:`DeprecationWarning` | Base category for warnings about deprecated |
74 | :exc:`SyntaxWarning` | Base category for warnings about dubious |
77 | :exc:`RuntimeWarning` | Base category for warnings about dubious |
80 | :exc:`FutureWarning` | Base category for warnings about constructs |
83 | :exc:`PendingDeprecationWarning` | Base category for warnings about features |
87 | :exc:`ImportWarning` | Base category for warnings triggered during |
91 | :exc:`UnicodeWarning` | Base category for warnings related to |
96 because conceptually they belong to the warnings mechanism.
108 The Warnings Filter
111 The warnings filter controls whether warnings are ignored, displayed, or turned
114 Conceptually, the warnings filter maintains an ordered list of filter
125 | ``"error"`` | turn matching warnings into exceptions |
127 | ``"ignore"`` | never print matching warnings |
129 | ``"always"`` | always print matching warnings |
132 | | warnings for each location where the warning |
136 | | warnings for each module where the warning |
140 | | warnings, regardless of location |
159 The warnings filter is initialized by :option:`-W` options passed to the Python
162 :mod:`warnings` module parses these when it is first imported (invalid options
183 Temporarily Suppressing Warnings
190 import warnings
193 warnings.warn("deprecated", DeprecationWarning)
195 with warnings.catch_warnings():
196 warnings.simplefilter("ignore")
199 While within the context manager all warnings will simply be ignored. This
210 Testing Warnings
213 To test warnings raised by code, use the :class:`catch_warnings` context
214 manager. With it you can temporarily mutate the warnings filter to facilitate
215 your testing. For instance, do the following to capture all raised warnings to
218 import warnings
221 warnings.warn("deprecated", DeprecationWarning)
223 with warnings.catch_warnings(record=True) as w:
224 # Cause all warnings to always be triggered.
225 warnings.simplefilter("always")
233 One can also cause all warnings to be exceptions by using ``error`` instead of
236 set the warning will not be seen again unless the warnings registry related to
239 Once the context manager exits, the warnings filter is restored to its state
240 when the context was entered. This prevents tests from changing the warnings
249 a new warning (e.g. set warnings to be raised as exceptions and check the
252 entries from the warnings list before each new operation).
258 Warnings that are only of interest to the developer are ignored by default. As
259 such you should make sure to test your code with typically ignored warnings
262 default handling for all warnings, including those that are ignored by default.
263 To change what action is taken for encountered warnings you simply change what
269 warnings.simplefilter('default')
272 registering of what warnings have been raised from unexpectedly influencing how
273 future warnings are treated.
275 Having certain warnings ignored by default is done to prevent a user from
276 seeing warnings that are only of interest to the developer. As you do not
279 release cycles. The new interpreter release could trigger new warnings in your
300 warnings filter see above. The *stacklevel* argument can be used by wrapper
304 warnings.warn(message, DeprecationWarning, stacklevel=2)
333 Issue a warning related to Python 3.x deprecation. Warnings are only shown
347 ``warnings.showwarning``.
370 Insert an entry into the list of :ref:`warnings filter specifications
374 inserts them as a tuple in the list of warnings filters. Entries closer to
382 Insert a simple entry into the list of :ref:`warnings filter specifications
391 Reset the warnings filter. This discards the effect of all previous calls to
401 A context manager that copies and, upon exit, restores the warnings filter
411 module returned when you import :mod:`warnings` whose filter will be
412 protected. This argument exists primarily for testing the :mod:`warnings`