-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Let pytest.warns check the warning's stacklevel #4343
Comments
I think the idea of providing some assistance with testing that The pandas code has a couple of problems:
In this state I don't think it's good to add to If we're fine with reducing the scope to only checking the filename, we can do this: with pytest.warns(DeprecationWarning, filename='test_stuff.py'):
deprecated_function() which is the equivalent of with pytest.warns(DeprecationWarning) as warning_messages:
deprecated_function()
assert any(wm.filename == 'test_stuff.py' for wm in warning_messages) To ease the common case, we can support I think that can be nice, but not 100% sure it's worth it. |
an helper that expects the warning to happen either in the current block, or in a specified function would help as well |
I think checking for a function would be ideal, but it doesn't seem possible to do cleanly, without hacks like reverse-mapping filename+lineno to a function and such. |
Agreed that a function name would be ideal. Happily I also think that such a reverse mapping is in fact practical for most functions. We get the filename and lineno from the warning; we check whether the filename matches the If you accept a function object instead of name (which I'd support as an option), we can skip all the iteration - we've got |
If you are happy to hard-code/vendor more of the warning handling you could implement
with the current stacklevel (or an additional Dunno if it is worth the trouble with vendoring and potential Python changes. but maybe it is actually nicer then inspecting files. |
Checking stack level cannot be done safely, what can be done is asserting a warning happens within a function via the record attributes |
This is a feature request for adding a
check_stacklevel
flag topytest.warns
. This helps library authors test that warnings point back to the user's source code, rather than some internal method.An example:
f
is the public API exposed to users.g
is some internal function that emits a warning.Ideally,
test_foo
would fail because the warning message points back to line 7, which is insideg
The test would succeed if the warning pointed back to the call
f(2)
, inside the test. This would require settingstacklevel=2
insideg
.Pandas added this check a couple years ago, and we've been quite happy with it: https://github.com/pandas-dev/pandas/blob/af602c5b702366d8b978ed04cafa371d63f6c32f/pandas/util/testing.py#L2707-L2718
It requires a frame hack, but a small one. Having it be a flag is necessary, in case an internal function is called from two places with different stacklevels. In pandas, we like the default of True. Perhaps this could be handled similarly to
strict
for xfails.The text was updated successfully, but these errors were encountered: