8000 gh-93343: Expand warning filter examples by daniel-shimon · Pull Request #106618 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-93343: Expand warning filter examples #106618

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Clarify escaped dots in regexes
  • Loading branch information
daniel-shimon authored May 6, 2025
commit f16ae2dbfc6b2063c4a6cbe0ad0e606dcbb2994b
17 changes: 11 additions & 6 deletions Doc/library/warnings.rst
Original file line number Diff line number Diff line change
Expand Up @@ -224,23 +224,28 @@ 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
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
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
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"
# 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"
# Ignore warnings in "yourmodule.submodule" which START with "generic".
filterwarnings("ignore", module="yourmodule.*")
Copy link
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", module="yourmodule.*")
filterwarnings("ignore", module="yourmodule\.*")

I think this should be escaped too.

Copy link
Contributor
@erlend-aasland erlend-aasland Jul 11, 2023

Choose a reason for hiding this comment

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

Also, instead of using comments, would it be clearer (formatting wise) to use multiple code blocks? (Goes for the change below as well)

Copy link
Author

Choose a reason for hiding this comment

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

No because here we actually want to catch all characters after 'yourmodule' and not only dot

Copy link
Contributor

Choose a reason for hiding this comment

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

# Ignore all warnings in "yourmodule" and its submodules
# 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 does not need to be escaped
# since it is not a regex.
# 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
# Ignore all warnings in "yourmodule", but NOT in its submodules.


.. _default-warning-filter:
Expand Down
Loading
0