-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Bug in 8.0.0 pytest.warns
handling
#11906
Comments
Also reproducible outside pytest: >>> w = AWarning(1, 2)
>>> warnings.warn_explicit(message=str(w), category=w.__class__, filename="test.py", lineno=1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: AWarning.__init__() missing 1 required positional argument: 'b' So while I can't find this documented explicitly anywhere, it looks like Python doesn't expect the signature of a warning's |
Thanks for the quick reply @The-Compiler!
Although pytest handled this fine prior to 8.0,0? Isn't the following call merely an election on the part of pytest? warnings.warn_explicit(message=str(w), category=w.__class__, filename="test.py", lineno=1) I'm not clear on why the implication of Python providing for raising a warning in this way is that warning messages shouldn't be dynamic (which they obviously can be) (?). Thanks again. |
Yes, the call was added in #10937 - but unless others disagree (@reaganjlee @Zac-HD thoughts?), I'd argue that the bug is in your code and not pytest. |
So we're saying that the class CustomWarning(UserWarning):
def __init__(self, a, b):
self._msg = (
"This warning message is customised to include reference to"
f"parameters {a} and {b}"
)
def __str__(self):
return self._msg Like yourself, I can't find any documentation suggesting that this shouldn't be done... |
Indeed - looking at the call pytest does in isolation, I can't see anything that would be wrong with it. The failure happens inside Python's own That being said, the
So I suppose pytest could still work around this by using |
Interesting. I'm not sure of the purposes for why pytest now makes this call in 8.0. Although, I notice that you now re-raise captured warnings that don't match any passed regex, so I'm guessing that's it (?), in which case passing the instance to 'message' sounds reasonable? I just find the change in behaviour unexpected. Regardless of any action taken, or not, thank you for looking at this. |
I think it's worth check that this would have the desired effect, and shipping it in our next patch if so. I'm aware that re-emitting warnings can be a breaking change and think that missing fewer warnings is worth it, but if we can make a slight change to our use of the stdlib to break less downstream code that'd be nice. |
Thank you to everyone who looked at this and arranged for the fix! |
There seems to be a handling error in 8.0.0, when
pytest.warns
fails to match the message. Seems that the warning class itself is being called towards the end of the handling...NB: only seems to happen if the Warning class takes more than one argument. For example, if arg b is stripped from all the above then the failure to match is handled as expected.
Also, worked fine in 7.4.4:
Thanks is advance for looking at this!
OS: 'Windows-10-10.0.22631-SP0'
The text was updated successfully, but these errors were encountered: