-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Improvement: extend pytest.raises to support Exception having __repr__ method and initialized with kwargs #11073
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
Improvement: extend pytest.raises to support Exception having __repr__ method and initialized with kwargs #11073
Conversation
…_ method and initialized with kwargs.
…_ method and initialized with kwargs parameter, except default repr.
Hi @BoWuGit, Do you know why FastAPI decided to use Instinctively I'm somewhat negative on this change, because it replaces a clear assertion (matching how an exception "renders", which is usually its For you specific example, an alternative would be:
|
This seems like very unexpected behavior to me as well - the last thing I'd want from a test framework is it guessing what I might have meant if something about my implementation is strange. If I forget to implement a |
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.
It seems necessary to reject misbehaved exception type support here
Imho fastapi should fix the exception classes
Alternatively a fastapi specific wrapper could match values on correct types instead of yolo on repr
What you said makes sense, so how about print some warning info when value = str(self.value)
msg = f"Regex pattern did not match.\n Regex: {regexp!r}\n Input: {value!r}"
if regexp == value:
msg += "\n Did you mean to `re.escape()` the regex?"
elif not value and not self.value.args:
msg += "\n Did you initialize Exception with kwargs? If so, you can try to initialize with args for `str()` to get the message." Personally I really puzzled when HTTPException didn't work suddenly, only because that I initialized it with kwargs. And only when I debugged source code of pytest can I know where went wrong finally. Thanks for all your attention. |
I see what you mean now... whoa, that seems like very magical Python behavior, I never noticed this before: >>> class WeirdException(Exception):
... def __init__(self, code):
... pass
...
>>> str(WeirdException(42))
'42'
>>> str(WeirdException(code=42))
'' because >>> WeirdException(42).args
(42,)
>>> WeirdException(code=42).args
() notably, without I'm however not very convinced about that message either. Like @bluetech said, it doesn't look like what you actually wanted to test is the string representation of the exception. If you want to test the I'd be open to making the existing output a bit more clear (say, maybe displaying |
And if you call >>> class WeirdException(Exception):
... def __init__(self, code):
... print(code)
... super().__init__(code)
...
>>> str(WeirdException(code=42))
42
'42'
What I really want to check is
OK, I agree, if that's the case, then others meeting this issue would not spend time to figure why like I did. |
Genuine question: Why not? Not calling the super-class IMHO, starlette not doing that is rather questionable.
I disagree. If you want to check the
So you think that would have helped in your case? If so, happy to review a change for that. |
OK, so I'll try to submit a pr to starlette.
As a user not so familiar with pytest, I admit that
I think that's helpful, because from |
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.
Seems fine by me. Let's see what others think!
As a heads up, #11227 just changed how |
Based on the upstream fix I propose we close this |
OK, I updated document of |
Issue
I met this issue when using FastAPI's HTTPException, which defines as follow. And when I use
pytest.raises
to assertHTTPException
initialized with kwargs as follows, it does't work, so I extended it.Backward compatibility
I think this pr would not break current behavior, because we generally use
pytest.raises match
to check details, it defaults toException.__str__
method, which prints args. What I extend is to supportException.__repr__
method and kwargs whenException
is initialized without args, so it's intelligent than ever.Thanks for your attention.