-
-
Notifications
You must be signed in to change notification settings - Fork 32k
bpo-42272: fix misleading warning filter message/module docs #23172
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
Conversation
This PR is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 5 days |
"The Warnings Filter" section of the warnings module documentation describes the message and module filters as "a string containing a regular expression". While that is true when they are arguments to the filterwarnings function, it is not true when they appear in -W or $PYTHONWARNINGS where they are matched literally (after stripping any starting/ending whitespace). Update the documentation to note when they are matched literally. Also clarify that module matches the "fully-qualified module name", rather than "module name" which is ambiguous. skip news (since this is a doc fix) Signed-off-by: Kevin Locke <[email protected]>
The `error:::mymodule[.*]` example in the "Describing Warning Filters" section of the warnings module documentation does not behave as the comment describes. Since the module portion of the filter string is interpreted literally, it would match a module with a fully-qualified name that is literally `mymodule[.*]`. Unfortunately, there is not a way to match '"module" and any subpackages of "mymodule"' as documented, since the module part of a filter string is matched literally. Instead, update the filter and comment to match only "mymodule". skip news (since this is a doc fix) Signed-off-by: Kevin Locke <[email protected]>
7a87f02
to
1767ef9
Compare
This PR is still relevant. I've rebased on current master and added "skip news" to commit messages. No changes to patch content. |
This PR is stale because it has been open for 30 days with no activity. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree that this change to the documentation reflects the current behavior of -W
and PYTHONWARNINGS
. I'm not sure if this behavior of using a literal string instead of a regular expression is intended -- @kevinoid do you know if this behavior is a regression from a previous version? Did -W
ever treat the input message/module as a regular expression as the documentation claims?
I checked Python 2.7 and Python 3.5, just to see if this is a regression from some older version of Python. It appears that the behavior of $ python2.7 -W "ignore:::mymodule.*" -c "import warnings; print warnings.filters[0][3].pattern"
mymodule\.\*$
$ PYTHONWARNINGS="ignore:::mymodule.*" python2.7 -c "import warnings; print warnings.filters[0][3].pattern"
mymodule\.\*$
$ python2.7 -c 'import warnings; warnings.filterwarnings("ignore", module="mymodule.*"); print warnings.filters[0][3].pattern'
mymodule.* $ python3.5 -W "ignore:::mymodule.*" -c "import warnings; print(warnings.filters[0][3].pattern)"
mymodule\.\*$
$ PYTHONWARNINGS="ignore:::mymodule.*" python3.5 -c "import warnings; print(warnings.filters[0][3].pattern)"
mymodule\.\*$
$ python3.5 -c 'import warnings; warnings.filterwarnings("ignore", module="mymodule.*"); print(warnings.filters[0][3].pattern)'
mymodule.* |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These changes look good to me. It fixes a problem in the docs that has resulted in multiple bug reports and StackOverflow questions:
No, but there was an attempt on 2018-2021 in bpo-34624 -W option and PYTHONWARNINGS env variable does not accept module regexes. (Note: probably a PEP, Python Enhancement Proposal is needed to boost things up because there was a concern about ReDoS attack) In that issue Nick Coghlan (an author of Describing Warning Filters) writes:
|
Also, Thomas Gläßle reported in the same BPO issue on December 2021 that:
To check if this PR is still relevant, I got familiarized myself with a chain of hoops PYTHONWARNINGS is thrown through on the C side and what processing happens on the way, then compared it to the first occurence of PYTHONWARNINGS in the module documentation. I can verify that:
So it looks like that |
Thanks for reviewing/investigating @arhadthedev! Good finds.
Is that something you'd like me to fix in this PR, or is that something for future work? If here, could you clarify what you'd like it to say? |
It would be great if #9358 could be given another shot before removing the possibility to filter hierarchically is given up upon entirely. The lack of proper hierarchical filtering makes strict filterwarnings hard to use, and it could lead many projects to the bad solution of not reporting warnings as errors (over-broad warning silencing), because expected warnings in third party modules can neither be fixed nor filtered out appropriately. For use case see this SO question. |
I just ran into this, and I suspect it might have been a typo for This was added in GH-4458 -- @ncoghlan, can you confirm whether this was your intention? Also note that since the code is using Lines 353 to 357 in 1971014
FWIW: I found this PR starting from pytest-dev/pytest#8759 and I also wrote pytest-dev/pytest#8759 (comment) with some examples and the result of my experiments. |
The PR looks good to me (I closed it and reopened it to re-trigger the bots). @kevinoid, can you add a NEWS entry to say that the docs now acknowledge that @arhadthedev, if you think something else should be changed, we can create a new PR/issue. I think more examples (like the ones I added in pytest-dev/pytest#8759 (comment)) would be helpful. |
Signed-off-by: Kevin Locke <[email protected]>
Thanks @ezio-melotti! I just pushed an additional commit with a NEWS entry. Let me know if you'd prefer I rebase the PR and merge the NEWS entry into a previous commit. And, as always, let me know if you notice any other issues. |
Thanks @kevinoid for the PR, and @ezio-melotti for merging it 🌮🎉.. I'm working now to backport this PR to: 3.10. |
Thanks @kevinoid for the PR, and @ezio-melotti for merging it 🌮🎉.. I'm working now to backport this PR to: 3.11. |
…H-23172) * bpo-42272: improve message/module warning filter docs "The Warnings Filter" section of the warnings module documentation describes the message and module filters as "a string containing a regular expression". While that is true when they are arguments to the filterwarnings function, it is not true when they appear in -W or $PYTHONWARNINGS where they are matched literally (after stripping any starting/ending whitespace). Update the documentation to note when they are matched literally. Also clarify that module matches the "fully-qualified module name", rather than "module name" which is ambiguous. skip news (since this is a doc fix) Signed-off-by: Kevin Locke <[email protected]> * bpo-42272: remove bad submodule warning filter doc The `error:::mymodule[.*]` example in the "Describing Warning Filters" section of the warnings module documentation does not behave as the comment describes. Since the module portion of the filter string is interpreted literally, it would match a module with a fully-qualified name that is literally `mymodule[.*]`. Unfortunately, there is not a way to match '"module" and any subpackages of "mymodule"' as documented, since the module part of a filter string is matched literally. Instead, update the filter and comment to match only "mymodule". skip news (since this is a doc fix) Signed-off-by: Kevin Locke <[email protected]> * bpo-42272: add warning filter doc changes to NEWS Signed-off-by: Kevin Locke <[email protected]> (cherry picked from commit 8136606) Co-authored-by: Kevin Locke <[email protected]>
GH-93341 is a backport of this pull request to the 3.10 branch. |
…H-23172) * bpo-42272: improve message/module warning filter docs "The Warnings Filter" section of the warnings module documentation describes the message and module filters as "a string containing a regular expression". While that is true when they are arguments to the filterwarnings function, it is not true when they appear in -W or $PYTHONWARNINGS where they are matched literally (after stripping any starting/ending whitespace). Update the documentation to note when they are matched literally. Also clarify that module matches the "fully-qualified module name", rather than "module name" which is ambiguous. skip news (since this is a doc fix) Signed-off-by: Kevin Locke <[email protected]> * bpo-42272: remove bad submodule warning filter doc The `error:::mymodule[.*]` example in the "Describing Warning Filters" section of the warnings module documentation does not behave as the comment describes. Since the module portion of the filter string is interpreted literally, it would match a module with a fully-qualified name that is literally `mymodule[.*]`. Unfortunately, there is not a way to match '"module" and any subpackages of "mymodule"' as documented, since the module part of a filter string is matched literally. Instead, update the filter and comment to match only "mymodule". skip news (since this is a doc fix) Signed-off-by: Kevin Locke <[email protected]> * bpo-42272: add warning filter doc changes to NEWS Signed-off-by: Kevin Locke <[email protected]> (cherry picked from commit 8136606) Co-authored-by: Kevin Locke <[email protected]>
GH-93342 is a backport of this pull request to the 3.11 branch. |
* bpo-42272: improve message/module warning filter docs "The Warnings Filter" section of the warnings module documentation describes the message and module filters as "a string containing a regular expression". While that is true when they are arguments to the filterwarnings function, it is not true when they appear in -W or $PYTHONWARNINGS where they are matched literally (after stripping any starting/ending whitespace). Update the documentation to note when they are matched literally. Also clarify that module matches the "fully-qualified module name", rather than "module name" which is ambiguous. skip news (since this is a doc fix) Signed-off-by: Kevin Locke <[email protected]> * bpo-42272: remove bad submodule warning filter doc The `error:::mymodule[.*]` example in the "Describing Warning Filters" section of the warnings module documentation does not behave as the comment describes. Since the module portion of the filter string is interpreted literally, it would match a module with a fully-qualified name that is literally `mymodule[.*]`. Unfortunately, there is not a way to match '"module" and any subpackages of "mymodule"' as documented, since the module part of a filter string is matched literally. Instead, update the filter and comment to match only "mymodule". skip news (since this is a doc fix) Signed-off-by: Kevin Locke <[email protected]> * bpo-42272: add warning filter doc changes to NEWS Signed-off-by: Kevin Locke <[email protected]> (cherry picked from commit 8136606) Co-authored-by: Kevin Locke <[email protected]>
* bpo-42272: improve message/module warning filter docs "The Warnings Filter" section of the warnings module documentation describes the message and module filters as "a string containing a regular expression". While that is true when they are arguments to the filterwarnings function, it is not true when they appear in -W or $PYTHONWARNINGS where they are matched literally (after stripping any starting/ending whitespace). Update the documentation to note when they are matched literally. Also clarify that module matches the "fully-qualified module name", rather than "module name" which is ambiguous. skip news (since this is a doc fix) Signed-off-by: Kevin Locke <[email protected]> * bpo-42272: remove bad submodule warning filter doc The `error:::mymodule[.*]` example in the "Describing Warning Filters" section of the warnings module documentation does not behave as the comment describes. Since the module portion of the filter string is interpreted literally, it would match a module with a fully-qualified name that is literally `mymodule[.*]`. Unfortunately, there is not a way to match '"module" and any subpackages of "mymodule"' as documented, since the module part of a filter string is matched literally. Instead, update the filter and comment to match only "mymodule". skip news (since this is a doc fix) Signed-off-by: Kevin Locke <[email protected]> * bpo-42272: add warning filter doc changes to NEWS Signed-off-by: Kevin Locke <[email protected]> (cherry picked from commit 8136606) Co-authored-by: Kevin Locke <[email protected]>
"The Warnings Filter" section of the warnings module documentation describes the message and module filters as "a string containing a regular expression". While that is true when they are arguments to the filterwarnings function, it is not true when they appear in -W orPYTHONWARNINGS where they are matched literally (after stripping any starting/ending whitespace). This PR updates the documentation to note when they are matched literally and clarifies that module matches the "fully-qualified module name", rather than "module name" which is ambiguous.
Additionally, the
error:::mymodule[.*]
example in the "Describing Warning Filters" section of the warnings module documentation does not behave as the comment describes. Since the module portion of the filter string is interpreted literally, it would match a module with a fully-qualified name that is literallymymodule[.*]
.Unfortunately, there is not a way to match '"module" and any subpackages of "mymodule"' as documented, since the module part of a filter string is matched literally. Instead, this PR updates the filter and comment to match only "mymodule".
Thanks for considering,
Kevin
https://bugs.python.org/issue42272