-
-
Notifications
You must be signed in to change notification settings - Fork 7.5k
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
django-stubs: Fix HttpResponse missing attribute error. #22185
Conversation
98f0b6f
to
53f97e0
Compare
The - response_dict = self.assert_json_error_data(result, use_zulip_error_format=False)
+ self.assertEqual(result.status_code, 400)
self.assertEqual(
- response_dict,
+ orjson.loads(result.content),
{
"schemas": ["urn:ietf:params:scim:api:messages:2.0:Error"],
"detail": "Must specify name.formatted, name.givenName or name.familyName when creating a new user",
"status": 400,
},
) |
corporate/tests/test_stripe.py
Outdated
@@ -695,8 +696,10 @@ def test_upgrade_by_card(self, *mocks: Mock) -> None: | |||
response = self.upgrade() | |||
[payment_intent] = PaymentIntent.objects.all() | |||
assert payment_intent.stripe_payment_intent_id is not None | |||
|
|||
success = self.assert_json_success(response) |
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.
I think result_dict
or response_dict
would probably be a better conventional variable name for these objects than success
.
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.
I’m fine with success
or response_dict
I guess, but result_dict
is strictly worse. (Requests have responses; computations have results. We have a lot of result
variables of type HttpResponse
that should be named response
.)
My justification for success
is the same as the one in the OpenAPI schema, where JsonResponse
is the type of all API responses and JsonSuccess
is the more specific subtype of all successful API responses.
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.
response_dict
WFM. I don't like success
because the object it a dictionary containing the data returned in the response -- sans headers and the like. success_dict
would be fine too, but I think it's cleaner to have the name directly associated with response
.
Also agreed that it would be a nice refactor to s/result/response/
in the tests.
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.
The PR is updated to go with response_dict
.
53f97e0
to
177ce9a
Compare
I noticed that django-stubs actually knows about |
Yes. We might need to change the return type of the wrapped test client to |
You can actually write this today, without from typing import TYPE_CHECKING
if TYPE_CHECKING:
from django.test.client import _MonkeyPatchedWSGIResponse
def client_get(…) -> "_MonkeyPatchedWSGIResponse"
… Even after we do install |
Makes sense. Though I think it's probably a cleanup to the tests to not have tons of them doing the |
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 still basically a fine cleanup. If we have an assert_json_success
we might as well use its return value.
The commit has two changes, though: use the assert_json_success
return value, and add assert_json_error_data
. The commit message only mentions the latter. (Unintentional squash perhaps?) It should be split into two commits. Or perhaps assert_json_error_data
should be dropped—it has only two callers.
71e0ef3
to
e28ea5f
Compare
Signed-off-by: Zixuan James Li <p359101898@gmail.com>
e28ea5f
to
2e8c2b7
Compare
This is great, merged, thanks @PIG208! |
This is a part of #18777.
As Django monkey patch attributes on the
HttpResponse
the test client returns. This does not really make the patched attributes available on the response object of the test client, but instead avoids accessing these attributes by taking a detour.It refactors away
response.json()
withresponse.assert_json_success
and a new helperresponse.assert_json_error_data
manually and with regexes.A flag
use_zulip_api_format
is added forresponse.assert_json_error_data
to properly testtest_scim.py
.Self-review checklist
(variable names, code reuse, readability, etc.).
Communicate decisions, questions, and potential concerns.
Individual commits are ready for review (see commit discipline).
Completed manual review and testing of the following: