Skip to content

Add support for HttpResponse.text #2621

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

Closed
cuu508 opened this issue Apr 23, 2025 · 1 comment
Closed

Add support for HttpResponse.text #2621

cuu508 opened this issue Apr 23, 2025 · 1 comment

Comments

@cuu508
Copy link
Contributor

cuu508 commented Apr 23, 2025

Django 5.2 added the HttpResponse.text field.

This field would be handy to use in tests, but _MonkeyPatchedWSGIResponse does not yet know about it.

I wanted to make a quick PR adding support for it, but ended up a little over my head :-)

I added the text field to the HttpResponse stub:

--- a/django-stubs/http/response.pyi
+++ b/django-stubs/http/response.pyi
@@ -6,6 +6,7 @@ from json import JSONEncoder
 from typing import Any, Literal, TypeVar, overload, type_check_only
 
 from django.utils.datastructures import CaseInsensitiveMapping, _PropertyDescriptor
+from django.utils.functional import cached_property
 
 class BadHeaderError(ValueError): ...
 
@@ -102,6 +103,8 @@ class HttpResponse(HttpResponseBase, Iterable[bytes]):
     __bytes__ = serialize
     def __iter__(self) -> Iterator[bytes]: ...
     def getvalue(self) -> bytes: ...
+    @cached_property
+    def text(self) -> str: ...

However, _MonkeyPatchedWSGIResponse inherits from HttpResponseBase not from HttpResponse, and so if I access it in tests, I get type errors.

I think this is a similar situation as in #968

  • The easy fix would be to add text to the _MonkeyPatchedWSGIResponse as well. Doing so would not be quite correct though, as HttpResponseBase subclasses other than HttpResponse should not have the text field.
  • Same as in Support content attribute for a Testing Response. #968, the alternative would be to have multiple variants of _MonkeyPatchedWSGIResponse, and in tests assert which specific variant we expect. This would be more correct but more clumsy.

I'm not sure how to proceed here.

@cuu508
Copy link
Contributor Author

cuu508 commented Apr 23, 2025

I created a PR for the "easy fix" option here: #2622

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants