-
-
Notifications
You must be signed in to change notification settings - Fork 454
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
Support content attribute for a Testing Response. #968
Conversation
`content` is a documented attribute of a testing response which isn't part of the parent HttpResponseBase: https://docs.djangoproject.com/en/4.0/topics/testing/tools/#testing-responses
This was discussed with @sterliakov in #909 (comment) TL;DR: Use the The response class hierarchy is the following:
Only Although I haven't verified how |
Indeed I checked, and the def dummy_view(_request: HttpRequest) -> HttpResponseBase:
return FileResponse(__file__)
urlpatterns: list[URLPattern] = [
path("dummy", dummy_view),
]
@pytest.mark.urls("tests_module")
def test_dummy(client: Client) -> None:
resp = client.get("/dummy")
print(resp.content) Fails with
|
So arguably Django documentation should be updated to recommend |
Thanks for the details, I'll update my code accordingly. It's a bit unfortunate that code suggested by the official Django documentation fails on this. Is the MyPy plugin setup flexible enough to let django-stubs give a suggestion in this case? |
Indeed. After thinking a bit, I'm not opposed to merging this. I'd like to see more opinions. The lack of |
I'm here because the lack of both url and content properties on the MonkeyPatched type stub bit me. If the response type would be a Union of various MonkeyPatched types (HTTP, Stream etc), then all the end developer would have to do to settle would be to assert a specific type for mypy to recognize accordingly. It would be kind of both typesafe and almost in the spirit of Django's docs. |
After learning why the current behavior is the way it is I personally I agree with @intgr's reasoning: having the MonkeyPatched objects have attributes that are only true in 99% of the cases is fine since the other 1% will result at runtime in a helpful AttributeError, and it will only affect testing code (which won't affect production, and if run automatically won't stay out-of-sight). For me MyPy is doing its work on a best-effort basis, and trading type-correctness for usability seems a sensible thing in this particular case. Having to constantly |
This also hit me:
|
Maybe we can add |
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.
This is exaclty what you did :)
I have made yet another thing!
content
is a documented attribute of a testing response which isn't part of the parentHttpResponseBase
, thus we need to add it to our MonkeyPatched types.Without this attribute the example code from the documentation will fail typing:
This worked on 1.10.1 but broke after upgrading to 1.11.0 in my own code:
Related issues
None that I am aware of.