-
-
Notifications
You must be signed in to change notification settings - Fork 30.6k
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
Enhance frame handing in warnings.warn() to skip by module rather that stacklevel #39615
Comments
This patch enhances warnings.warn() in the following So when you have the following call stack: and the innermost function() m2.m22.f() call |
I don't know how desireable this feature is, but Also, your description is wrong; when warn() in I think that a better solution would be to add |
Here's an updated patch that implements your suggestion: skipmodules is a regular expression. Any module whose name matches the RE will be skipped when determining the stacklevel. |
Looks good to me. I like that skipmodules is a http://www.python.org/sf/1631171 It would be nice if the documentation mentioned "If you want to issue a warning outside your module, If this patch is accepted, it should be put to use in >>> struct.Struct("<l").pack(4023029188) # note: module is sys
sys:1: DeprecationWarning: struct integer overflow masking is deprecated |
Patch has tests, needs updating. |
See similar http://bugs.python.org/issue25216 aka #69403 |
Assigning to me to see if I get to this as I'm really wanting this feature while trying to add a warning deep within an an complicated package with multiple public API surfaces at different stacklevels. |
`warnings.warn()` gains the ability to skip stack frames based on code filename prefix rather than only a numeric `stacklevel=` via a new `skip_file_prefixes=` keyword argument.
Skipping by module seems good, but the implementation relies on file names — could it be module or package name? |
It seems like "by fully qualified module name" is what code authors might want as that's how we think... But in practice that might be hard to implement in a reliable manner. The Investigating... The code objects in the frame appears to have a It might be possible to get the base module name itself via I'll poke around, but I'm not sure if it is possible. The other constraint I've been keeping in this code: It must be a fast check. warn() should not do a lot of computation. |
I don't believe module or package name is possible. Indeed |
`warnings.warn()` gains the ability to skip stack frames based on code filename prefix rather than only a numeric `stacklevel=` via a new `skip_file_prefixes=` keyword argument.
…n#100840) `warnings.warn()` gains the ability to skip stack frames based on code filename prefix rather than only a numeric `stacklevel=` via a new `skip_file_prefixes=` keyword argument.
I'm calling this fixed as it can skip by filename now which was a performant way to implement this. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
Linked PRs
The text was updated successfully, but these errors were encountered: