blob: f67f4bc24c4c6b5e63845bb3b4e0f2ccc019bb56 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001:mod:`warnings` --- Warning control
2===================================
3
Georg Brandl116aa622007-08-15 14:28:22 +00004.. module:: warnings
5 :synopsis: Issue warning messages and control their disposition.
6
Raymond Hettinger469271d2011-01-27 20:38:46 +00007**Source code:** :source:`Lib/warnings.py`
8
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04009.. index:: single: warnings
10
Raymond Hettinger469271d2011-01-27 20:38:46 +000011--------------
Georg Brandl116aa622007-08-15 14:28:22 +000012
Georg Brandl116aa622007-08-15 14:28:22 +000013Warning messages are typically issued in situations where it is useful to alert
14the user of some condition in a program, where that condition (normally) doesn't
15warrant raising an exception and terminating the program. For example, one
16might want to issue a warning when a program uses an obsolete module.
17
18Python programmers issue warnings by calling the :func:`warn` function defined
Georg Brandl60203b42010-10-06 10:11:56 +000019in this module. (C programmers use :c:func:`PyErr_WarnEx`; see
Georg Brandl116aa622007-08-15 14:28:22 +000020:ref:`exceptionhandling` for details).
21
22Warning messages are normally written to ``sys.stderr``, but their disposition
23can be changed flexibly, from ignoring all warnings to turning them into
24exceptions. The disposition of warnings can vary based on the warning category
25(see below), the text of the warning message, and the source location where it
26is issued. Repetitions of a particular warning for the same source location are
27typically suppressed.
28
29There are two stages in warning control: first, each time a warning is issued, a
30determination is made whether a message should be issued or not; next, if a
31message is to be issued, it is formatted and printed using a user-settable hook.
32
33The determination whether to issue a warning message is controlled by the
34warning filter, which is a sequence of matching rules and actions. Rules can be
35added to the filter by calling :func:`filterwarnings` and reset to its default
36state by calling :func:`resetwarnings`.
37
38The printing of warning messages is done by calling :func:`showwarning`, which
39may be overridden; the default implementation of this function formats the
40message by calling :func:`formatwarning`, which is also available for use by
41custom implementations.
42
Antoine Pitroucdddf2b2011-07-09 21:29:36 +020043.. seealso::
44 :func:`logging.captureWarnings` allows you to handle all warnings with
45 the standard logging infrastructure.
46
Georg Brandl116aa622007-08-15 14:28:22 +000047
48.. _warning-categories:
49
50Warning Categories
51------------------
52
53There are a number of built-in exceptions that represent warning categories.
54This categorization is useful to be able to filter out groups of warnings. The
55following warnings category classes are currently defined:
56
Georg Brandl44ea77b2013-03-28 13:28:44 +010057.. tabularcolumns:: |l|p{0.6\linewidth}|
58
Georg Brandl116aa622007-08-15 14:28:22 +000059+----------------------------------+-----------------------------------------------+
60| Class | Description |
61+==================================+===============================================+
62| :exc:`Warning` | This is the base class of all warning |
63| | category classes. It is a subclass of |
64| | :exc:`Exception`. |
65+----------------------------------+-----------------------------------------------+
66| :exc:`UserWarning` | The default category for :func:`warn`. |
67+----------------------------------+-----------------------------------------------+
68| :exc:`DeprecationWarning` | Base category for warnings about deprecated |
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +000069| | features (ignored by default). |
Georg Brandl116aa622007-08-15 14:28:22 +000070+----------------------------------+-----------------------------------------------+
71| :exc:`SyntaxWarning` | Base category for warnings about dubious |
72| | syntactic features. |
73+----------------------------------+-----------------------------------------------+
74| :exc:`RuntimeWarning` | Base category for warnings about dubious |
75| | runtime features. |
76+----------------------------------+-----------------------------------------------+
77| :exc:`FutureWarning` | Base category for warnings about constructs |
78| | that will change semantically in the future. |
79+----------------------------------+-----------------------------------------------+
80| :exc:`PendingDeprecationWarning` | Base category for warnings about features |
81| | that will be deprecated in the future |
82| | (ignored by default). |
83+----------------------------------+-----------------------------------------------+
84| :exc:`ImportWarning` | Base category for warnings triggered during |
85| | the process of importing a module (ignored by |
86| | default). |
87+----------------------------------+-----------------------------------------------+
88| :exc:`UnicodeWarning` | Base category for warnings related to |
89| | Unicode. |
90+----------------------------------+-----------------------------------------------+
Guido van Rossum98297ee2007-11-06 21:34:58 +000091| :exc:`BytesWarning` | Base category for warnings related to |
Serhiy Storchakabfdcd432013-10-13 23:09:14 +030092| | :class:`bytes` and :class:`bytearray`. |
Guido van Rossum98297ee2007-11-06 21:34:58 +000093+----------------------------------+-----------------------------------------------+
Georg Brandl08be72d2010-10-24 15:11:22 +000094| :exc:`ResourceWarning` | Base category for warnings related to |
95| | resource usage. |
96+----------------------------------+-----------------------------------------------+
Guido van Rossum98297ee2007-11-06 21:34:58 +000097
Georg Brandl116aa622007-08-15 14:28:22 +000098
99While these are technically built-in exceptions, they are documented here,
100because conceptually they belong to the warnings mechanism.
101
102User code can define additional warning categories by subclassing one of the
103standard warning categories. A warning category must always be a subclass of
104the :exc:`Warning` class.
105
106
107.. _warning-filter:
108
109The Warnings Filter
110-------------------
111
112The warnings filter controls whether warnings are ignored, displayed, or turned
113into errors (raising an exception).
114
115Conceptually, the warnings filter maintains an ordered list of filter
116specifications; any specific warning is matched against each filter
117specification in the list in turn until a match is found; the match determines
118the disposition of the match. Each entry is a tuple of the form (*action*,
119*message*, *category*, *module*, *lineno*), where:
120
121* *action* is one of the following strings:
122
123 +---------------+----------------------------------------------+
124 | Value | Disposition |
125 +===============+==============================================+
126 | ``"error"`` | turn matching warnings into exceptions |
127 +---------------+----------------------------------------------+
128 | ``"ignore"`` | never print matching warnings |
129 +---------------+----------------------------------------------+
130 | ``"always"`` | always print matching warnings |
131 +---------------+----------------------------------------------+
132 | ``"default"`` | print the first occurrence of matching |
133 | | warnings for each location where the warning |
134 | | is issued |
135 +---------------+----------------------------------------------+
136 | ``"module"`` | print the first occurrence of matching |
137 | | warnings for each module where the warning |
138 | | is issued |
139 +---------------+----------------------------------------------+
140 | ``"once"`` | print only the first occurrence of matching |
141 | | warnings, regardless of location |
142 +---------------+----------------------------------------------+
143
Martin Panter22194502016-07-19 02:26:38 +0000144* *message* is a string containing a regular expression that the start of
145 the warning message must match. The expression is compiled to always be
146 case-insensitive.
Georg Brandl116aa622007-08-15 14:28:22 +0000147
148* *category* is a class (a subclass of :exc:`Warning`) of which the warning
Benjamin Peterson8719ad52009-09-11 22:24:02 +0000149 category must be a subclass in order to match.
Georg Brandl116aa622007-08-15 14:28:22 +0000150
151* *module* is a string containing a regular expression that the module name must
Martin Panter22194502016-07-19 02:26:38 +0000152 match. The expression is compiled to be case-sensitive.
Georg Brandl116aa622007-08-15 14:28:22 +0000153
154* *lineno* is an integer that the line number where the warning occurred must
Benjamin Peterson8719ad52009-09-11 22:24:02 +0000155 match, or ``0`` to match all line numbers.
Georg Brandl116aa622007-08-15 14:28:22 +0000156
157Since the :exc:`Warning` class is derived from the built-in :exc:`Exception`
158class, to turn a warning into an error we simply raise ``category(message)``.
159
160The warnings filter is initialized by :option:`-W` options passed to the Python
161interpreter command line. The interpreter saves the arguments for all
162:option:`-W` options without interpretation in ``sys.warnoptions``; the
163:mod:`warnings` module parses these when it is first imported (invalid options
164are ignored, after printing a message to ``sys.stderr``).
165
Georg Brandl116aa622007-08-15 14:28:22 +0000166
Georg Brandl20629372010-10-24 15:16:02 +0000167Default Warning Filters
168~~~~~~~~~~~~~~~~~~~~~~~
169
170By default, Python installs several warning filters, which can be overridden by
171the command-line options passed to :option:`-W` and calls to
172:func:`filterwarnings`.
173
174* :exc:`DeprecationWarning` and :exc:`PendingDeprecationWarning`, and
175 :exc:`ImportWarning` are ignored.
176
177* :exc:`BytesWarning` is ignored unless the :option:`-b` option is given once or
178 twice; in this case this warning is either printed (``-b``) or turned into an
Georg Brandl19208902010-10-26 06:59:23 +0000179 exception (``-bb``).
Georg Brandl20629372010-10-24 15:16:02 +0000180
181* :exc:`ResourceWarning` is ignored unless Python was built in debug mode.
182
183.. versionchanged:: 3.2
184 :exc:`DeprecationWarning` is now ignored by default in addition to
185 :exc:`PendingDeprecationWarning`.
186
187
Brett Cannon1cd02472008-09-09 01:52:27 +0000188.. _warning-suppress:
189
190Temporarily Suppressing Warnings
191--------------------------------
192
Benjamin Petersonfcf5d632008-10-16 23:24:44 +0000193If you are using code that you know will raise a warning, such as a deprecated
194function, but do not want to see the warning, then it is possible to suppress
195the warning using the :class:`catch_warnings` context manager::
Brett Cannon1cd02472008-09-09 01:52:27 +0000196
197 import warnings
198
199 def fxn():
200 warnings.warn("deprecated", DeprecationWarning)
201
202 with warnings.catch_warnings():
203 warnings.simplefilter("ignore")
204 fxn()
205
206While within the context manager all warnings will simply be ignored. This
207allows you to use known-deprecated code without having to see the warning while
208not suppressing the warning for other code that might not be aware of its use
Benjamin Peterson08bf91c2010-04-11 16:12:57 +0000209of deprecated code. Note: this can only be guaranteed in a single-threaded
210application. If two or more threads use the :class:`catch_warnings` context
211manager at the same time, the behavior is undefined.
212
Brett Cannon1cd02472008-09-09 01:52:27 +0000213
214
215.. _warning-testing:
216
217Testing Warnings
218----------------
219
220To test warnings raised by code, use the :class:`catch_warnings` context
221manager. With it you can temporarily mutate the warnings filter to facilitate
222your testing. For instance, do the following to capture all raised warnings to
223check::
224
225 import warnings
226
227 def fxn():
228 warnings.warn("deprecated", DeprecationWarning)
229
230 with warnings.catch_warnings(record=True) as w:
231 # Cause all warnings to always be triggered.
232 warnings.simplefilter("always")
233 # Trigger a warning.
234 fxn()
235 # Verify some things
236 assert len(w) == 1
Alexandre Vassalotti6d3dfc32009-07-29 19:54:39 +0000237 assert issubclass(w[-1].category, DeprecationWarning)
Brett Cannon1cd02472008-09-09 01:52:27 +0000238 assert "deprecated" in str(w[-1].message)
239
240One can also cause all warnings to be exceptions by using ``error`` instead of
241``always``. One thing to be aware of is that if a warning has already been
242raised because of a ``once``/``default`` rule, then no matter what filters are
243set the warning will not be seen again unless the warnings registry related to
244the warning has been cleared.
245
246Once the context manager exits, the warnings filter is restored to its state
247when the context was entered. This prevents tests from changing the warnings
248filter in unexpected ways between tests and leading to indeterminate test
Benjamin Petersonfcf5d632008-10-16 23:24:44 +0000249results. The :func:`showwarning` function in the module is also restored to
Benjamin Peterson08bf91c2010-04-11 16:12:57 +0000250its original value. Note: this can only be guaranteed in a single-threaded
251application. If two or more threads use the :class:`catch_warnings` context
252manager at the same time, the behavior is undefined.
Benjamin Petersonfcf5d632008-10-16 23:24:44 +0000253
254When testing multiple operations that raise the same kind of warning, it
255is important to test them in a manner that confirms each operation is raising
256a new warning (e.g. set warnings to be raised as exceptions and check the
257operations raise exceptions, check that the length of the warning list
258continues to increase after each operation, or else delete the previous
259entries from the warnings list before each new operation).
Brett Cannon1cd02472008-09-09 01:52:27 +0000260
261
Ezio Melotti60901872010-12-01 00:56:10 +0000262.. _warning-ignored:
263
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +0000264Updating Code For New Versions of Python
265----------------------------------------
266
267Warnings that are only of interest to the developer are ignored by default. As
268such you should make sure to test your code with typically ignored warnings
Martin Panter00ccacc2016-04-16 04:59:38 +0000269made visible. You can do this from the command-line by passing :option:`-Wd <-W>`
Martin Panter5c679332016-10-30 04:20:17 +0000270to the interpreter (this is shorthand for :option:`!-W default`). This enables
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +0000271default handling for all warnings, including those that are ignored by default.
272To change what action is taken for encountered warnings you simply change what
Martin Panter5c679332016-10-30 04:20:17 +0000273argument is passed to :option:`-W`, e.g. :option:`!-W error`. See the
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +0000274:option:`-W` flag for more details on what is possible.
275
Martin Panter5c679332016-10-30 04:20:17 +0000276To programmatically do the same as :option:`!-Wd`, use::
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +0000277
278 warnings.simplefilter('default')
279
280Make sure to execute this code as soon as possible. This prevents the
281registering of what warnings have been raised from unexpectedly influencing how
282future warnings are treated.
283
284Having certain warnings ignored by default is done to prevent a user from
285seeing warnings that are only of interest to the developer. As you do not
286necessarily have control over what interpreter a user uses to run their code,
287it is possible that a new version of Python will be released between your
288release cycles. The new interpreter release could trigger new warnings in your
289code that were not there in an older interpreter, e.g.
290:exc:`DeprecationWarning` for a module that you are using. While you as a
291developer want to be notified that your code is using a deprecated module, to a
292user this information is essentially noise and provides no benefit to them.
293
Ezio Melotti60901872010-12-01 00:56:10 +0000294The :mod:`unittest` module has been also updated to use the ``'default'``
295filter while running tests.
296
Benjamin Peterson7ab4b8d2010-06-28 00:01:59 +0000297
Georg Brandl116aa622007-08-15 14:28:22 +0000298.. _warning-functions:
299
300Available Functions
301-------------------
302
303
Victor Stinnere19558a2016-03-23 00:28:08 +0100304.. function:: warn(message, category=None, stacklevel=1, source=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000305
306 Issue a warning, or maybe ignore it or raise an exception. The *category*
307 argument, if given, must be a warning category class (see above); it defaults to
308 :exc:`UserWarning`. Alternatively *message* can be a :exc:`Warning` instance,
309 in which case *category* will be ignored and ``message.__class__`` will be used.
310 In this case the message text will be ``str(message)``. This function raises an
311 exception if the particular warning issued is changed into an error by the
312 warnings filter see above. The *stacklevel* argument can be used by wrapper
313 functions written in Python, like this::
314
315 def deprecation(message):
316 warnings.warn(message, DeprecationWarning, stacklevel=2)
317
318 This makes the warning refer to :func:`deprecation`'s caller, rather than to the
319 source of :func:`deprecation` itself (since the latter would defeat the purpose
320 of the warning message).
321
Victor Stinnere19558a2016-03-23 00:28:08 +0100322 *source*, if supplied, is the destroyed object which emitted a
323 :exc:`ResourceWarning`.
324
325 .. versionchanged:: 3.6
326 Added *source* parameter.
327
Georg Brandl116aa622007-08-15 14:28:22 +0000328
Victor Stinner914cde82016-03-19 01:03:51 +0100329.. function:: warn_explicit(message, category, filename, lineno, module=None, registry=None, module_globals=None, source=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000330
331 This is a low-level interface to the functionality of :func:`warn`, passing in
332 explicitly the message, category, filename and line number, and optionally the
333 module name and the registry (which should be the ``__warningregistry__``
334 dictionary of the module). The module name defaults to the filename with
335 ``.py`` stripped; if no registry is passed, the warning is never suppressed.
336 *message* must be a string and *category* a subclass of :exc:`Warning` or
337 *message* may be a :exc:`Warning` instance, in which case *category* will be
338 ignored.
339
340 *module_globals*, if supplied, should be the global namespace in use by the code
341 for which the warning is issued. (This argument is used to support displaying
Christian Heimes3279b5d2007-12-09 15:58:13 +0000342 source for modules found in zipfiles or other non-filesystem import
343 sources).
Georg Brandl116aa622007-08-15 14:28:22 +0000344
Victor Stinner914cde82016-03-19 01:03:51 +0100345 *source*, if supplied, is the destroyed object which emitted a
346 :exc:`ResourceWarning`.
347
348 .. versionchanged:: 3.6
349 Add the *source* parameter.
350
Georg Brandl116aa622007-08-15 14:28:22 +0000351
Georg Brandl7f01a132009-09-16 15:58:14 +0000352.. function:: showwarning(message, category, filename, lineno, file=None, line=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000353
354 Write a warning to a file. The default implementation calls
Christian Heimes33fe8092008-04-13 13:53:33 +0000355 ``formatwarning(message, category, filename, lineno, line)`` and writes the
356 resulting string to *file*, which defaults to ``sys.stderr``. You may replace
Brett Cannone52181c2011-07-17 19:25:50 -0700357 this function with any callable by assigning to ``warnings.showwarning``.
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +0000358 *line* is a line of source code to be included in the warning
Georg Brandl48310cd2009-01-03 21:18:54 +0000359 message; if *line* is not supplied, :func:`showwarning` will
Alexandre Vassalottia79e33e2008-05-15 22:51:26 +0000360 try to read the line specified by *filename* and *lineno*.
Georg Brandl116aa622007-08-15 14:28:22 +0000361
362
Georg Brandl7f01a132009-09-16 15:58:14 +0000363.. function:: formatwarning(message, category, filename, lineno, line=None)
Georg Brandl116aa622007-08-15 14:28:22 +0000364
Benjamin Peterson8719ad52009-09-11 22:24:02 +0000365 Format a warning the standard way. This returns a string which may contain
366 embedded newlines and ends in a newline. *line* is a line of source code to
367 be included in the warning message; if *line* is not supplied,
368 :func:`formatwarning` will try to read the line specified by *filename* and
369 *lineno*.
Georg Brandl116aa622007-08-15 14:28:22 +0000370
371
Georg Brandl7f01a132009-09-16 15:58:14 +0000372.. function:: filterwarnings(action, message='', category=Warning, module='', lineno=0, append=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000373
Benjamin Peterson8719ad52009-09-11 22:24:02 +0000374 Insert an entry into the list of :ref:`warnings filter specifications
375 <warning-filter>`. The entry is inserted at the front by default; if
376 *append* is true, it is inserted at the end. This checks the types of the
377 arguments, compiles the *message* and *module* regular expressions, and
378 inserts them as a tuple in the list of warnings filters. Entries closer to
Georg Brandl116aa622007-08-15 14:28:22 +0000379 the front of the list override entries later in the list, if both match a
380 particular warning. Omitted arguments default to a value that matches
381 everything.
382
383
Georg Brandl7f01a132009-09-16 15:58:14 +0000384.. function:: simplefilter(action, category=Warning, lineno=0, append=False)
Georg Brandl116aa622007-08-15 14:28:22 +0000385
Benjamin Peterson8719ad52009-09-11 22:24:02 +0000386 Insert a simple entry into the list of :ref:`warnings filter specifications
387 <warning-filter>`. The meaning of the function parameters is as for
388 :func:`filterwarnings`, but regular expressions are not needed as the filter
389 inserted always matches any message in any module as long as the category and
390 line number match.
Georg Brandl116aa622007-08-15 14:28:22 +0000391
392
393.. function:: resetwarnings()
394
395 Reset the warnings filter. This discards the effect of all previous calls to
396 :func:`filterwarnings`, including that of the :option:`-W` command line options
397 and calls to :func:`simplefilter`.
398
Brett Cannonec92e182008-09-02 02:46:59 +0000399
Brett Cannon1cd02472008-09-09 01:52:27 +0000400Available Context Managers
401--------------------------
Brett Cannonec92e182008-09-02 02:46:59 +0000402
Georg Brandl7f01a132009-09-16 15:58:14 +0000403.. class:: catch_warnings(\*, record=False, module=None)
Brett Cannonec92e182008-09-02 02:46:59 +0000404
Benjamin Petersonfcf5d632008-10-16 23:24:44 +0000405 A context manager that copies and, upon exit, restores the warnings filter
406 and the :func:`showwarning` function.
407 If the *record* argument is :const:`False` (the default) the context manager
408 returns :class:`None` on entry. If *record* is :const:`True`, a list is
409 returned that is progressively populated with objects as seen by a custom
410 :func:`showwarning` function (which also suppresses output to ``sys.stdout``).
411 Each object in the list has attributes with the same names as the arguments to
412 :func:`showwarning`.
Brett Cannonec92e182008-09-02 02:46:59 +0000413
Brett Cannon1cd02472008-09-09 01:52:27 +0000414 The *module* argument takes a module that will be used instead of the
415 module returned when you import :mod:`warnings` whose filter will be
Benjamin Petersonfcf5d632008-10-16 23:24:44 +0000416 protected. This argument exists primarily for testing the :mod:`warnings`
Brett Cannon1cd02472008-09-09 01:52:27 +0000417 module itself.
Benjamin Peterson08bf91c2010-04-11 16:12:57 +0000418
419 .. note::
420
421 The :class:`catch_warnings` manager works by replacing and
422 then later restoring the module's
423 :func:`showwarning` function and internal list of filter
424 specifications. This means the context manager is modifying
425 global state and therefore is not thread-safe.