Skip to content

Commit 630144f

Browse files
authored
fix: Add context_data attribute to _MonkeyPatchedW/ASGIResponse. (#2509)
As detailed in Django's documentation [0], when a non-standard template backend is being used, the `context_data` attribute is populated instead of `context` when the test API call was successful. In reality, I've seen this to be a populated attribute regardless of the backend template engine being used. Regardless, this near hidden attribute is missing and should be included to help improve unit testing when validating the response data contains the expected data.
1 parent 60cdba0 commit 630144f

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

django-stubs/test/client.pyi

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,15 @@ class _MonkeyPatchedWSGIResponse(_WSGIResponse):
189189
request: dict[str, Any]
190190
client: Client
191191
templates: list[Template]
192+
193+
# `context` and `context_data` are populated based on whether you use the standard template
194+
# backend or a custom one respectively.
195+
# `context_data` only exists if the response was successful too as the return type changes.
196+
# `HTTPResponse` when API failed and `TemplateResponse` when successful.
197+
# https://docs.djangoproject.com/en/stable/topics/testing/tools/#django.test.Response.context
192198
context: ContextList | dict[str, Any]
199+
context_data: ContextList | dict[str, Any]
200+
193201
content: bytes
194202
resolver_match: ResolverMatch
195203
redirect_chain: list[tuple[str, int]]
@@ -200,7 +208,15 @@ class _MonkeyPatchedASGIResponse(_ASGIResponse):
200208
request: dict[str, Any]
201209
client: AsyncClient
202210
templates: list[Template]
211+
212+
# `context` and `context_data` are populated based on whether you use the standard template
213+
# backend or a custom one respectively.
214+
# `context_data` only exists if the response was successful too as the return type changes.
215+
# `HTTPResponse` when API failed and `TemplateResponse` when successful.
216+
# https://docs.djangoproject.com/en/stable/topics/testing/tools/#django.test.Response.context
203217
context: ContextList | dict[str, Any]
218+
context_data: ContextList | dict[str, Any]
219+
204220
content: bytes
205221
resolver_match: ResolverMatch
206222
redirect_chain: list[tuple[str, int]]

tests/typecheck/test/test_client.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
reveal_type(response.templates) # N: Revealed type is "builtins.list[django.template.base.Template]"
99
reveal_type(response.client) # N: Revealed type is "django.test.client.Client"
1010
reveal_type(response.context) # N: Revealed type is "Union[django.test.utils.ContextList, builtins.dict[builtins.str, Any]]"
11+
reveal_type(response.context_data) # N: Revealed type is "Union[django.test.utils.ContextList, builtins.dict[builtins.str, Any]]"
1112
reveal_type(response.content) # N: Revealed type is "builtins.bytes"
1213
reveal_type(response.redirect_chain) # N: Revealed type is "builtins.list[Tuple[builtins.str, builtins.int]]"
1314
response.json()
@@ -22,6 +23,7 @@
2223
reveal_type(response.templates) # N: Revealed type is "builtins.list[django.template.base.Template]"
2324
reveal_type(response.client) # N: Revealed type is "django.test.client.AsyncClient"
2425
reveal_type(response.context) # N: Revealed type is "Union[django.test.utils.ContextList, builtins.dict[builtins.str, Any]]"
26+
reveal_type(response.context_data) # N: Revealed type is "Union[django.test.utils.ContextList, builtins.dict[builtins.str, Any]]"
2527
reveal_type(response.content) # N: Revealed type is "builtins.bytes"
2628
reveal_type(response.redirect_chain) # N: Revealed type is "builtins.list[Tuple[builtins.str, builtins.int]]"
2729
response.json()

0 commit comments

Comments
 (0)