Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions 36 Doc/library/warnings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,42 @@ Some examples::
ignore,default:::mymodule # Only report warnings triggered by "mymodule"
error:::mymodule # Convert warnings to errors in "mymodule"

.. _warning-filter-examples:

Warning Filter Examples
~~~~~~~~~~~~~~~~~~~~~~~

Here are some complex examples for filtering warnings.

Note that :func:`filterwarnings` filters have subtle differences
from :option:`-W` and :envvar:`PYTHONWARNINGS` regarding the *message* and *module*
parts of the filter (as described in :ref:`warning-filter`).
Mainly, 'message' and 'module' are regular expressions in the former,
but literal strings in the latter two.

::

filterwarnings("ignore", message=".*generic", module=r"yourmodule\.submodule")

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
filterwarnings("ignore", message=".*generic", module=r"yourmodule\.submodule")
filterwarnings("ignore", message="\.*generic", module="yourmodule\.submodule")

Shouldn't this be escaped as well? The r here is not necessary, and its usage should be consistent between the two args (assuming they are both regexes).

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No since the first arg wants to capture strings that contain 'generic' so that the '.' catches everything, while the first arg want to capture 'yourmodule.submodule' specifically, meaning that the '.' actually captures dot and should be escaped

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, it sounds to me you should put that explanation into the docs, @daniel-shimon; if it is unclear for a reviewer, it is not going to be clear for the average docs reader ;)

# Ignore warnings in "yourmodule.submodule" which contain "generic".
# Note that the '.' in 'message' marks any character and in 'module' it is escaped,
# in order to match a literal dot character.

filterwarnings("ignore", message="generic", module=r"yourmodule\.submodule")
# Ignore warnings in "yourmodule.submodule" which START with "generic".

filterwarnings("ignore", module="yourmodule.*")
Comment thread
daniel-shimon marked this conversation as resolved.
# Ignore all warnings in "yourmodule" and its submodules.
# Note that the '.' in 'module' marks any character so is not escaped.

-W "ignore:generic::yourmodule.submodule:"
# Ignore warnings in "yourmodule.submodule" which START with "generic"
# (but not those containing it).
# Also note that the '.' in the module part does not need to be escaped
# since it is not a regular expression.

-W "ignore:::yourmodule:"
# Ignore all warnings in "yourmodule", but NOT in its submodules.


.. _default-warning-filter:

Expand Down
24 changes: 24 additions & 0 deletions 24 Lib/test/test_warnings/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,18 @@ def test_message_matching(self):
self.module.warn("something completely different")
self.assertEqual(w, [])

def test_message_matching_regex(self):
with original_warnings.catch_warnings(record=True,
module=self.module) as w:
self.module.simplefilter("ignore", UserWarning)
self.module.filterwarnings("error", ".*match", UserWarning)
self.assertRaises(UserWarning, self.module.warn, "match")
self.assertRaises(UserWarning, self.module.warn, "match prefix")
self.assertRaises(UserWarning, self.module.warn, "suffix match")
self.assertEqual(w, [])
self.module.warn("not a m4tch")
self.assertEqual(w, [])

def test_mutate_filter_list(self):
class X:
def match(self, a):
Expand Down Expand Up @@ -1352,6 +1364,18 @@ def test_single_warning(self):
PYTHONDEVMODE="")
self.assertEqual(stdout, b"['ignore::DeprecationWarning']")

def test_string_literals(self):
# Ensure message/module are treated as string literals
_, stdout, stderr = assert_python_ok("-c",
"import sys, warnings; "
"sys.stdout.write(warnings.filters[0][1].pattern); "
"sys.stderr.write(warnings.filters[0][3].pattern)",
PYTHONWARNINGS="ignore:.generic::yourmodule.submodule",
PYTHONDEVMODE="")
self.assertEqual(stdout, rb"\.generic")
# '\Z' is added to the module name, so check start of pattern:
self.assertTrue(stderr.startswith(rb"yourmodule\.submodule"))

def test_comma_separated_warnings(self):
rc, stdout, stderr = assert_python_ok("-c",
"import sys; sys.stdout.write(str(sys.warnoptions))",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Add examples of warning filters and the difference between programmatic and
environmental filters.
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.