Opened 2 months ago

Last modified 2 months ago

#29268 assigned Cleanup/optimization

Support passing None to test Client and RequestFactory request methods for default content_type

Reported by: Chris Jerdonek Owned by: Chris Jerdonek
Component: Testing framework Version: 2.0
Severity: Normal Keywords:
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Currently, passing content_type=None to `test.Client.post()` results in the following error:

TypeError: expected string or bytes-like object

instead of causing the default content_type value. It would be more user-friendly if passing None resulted in the default value.

One reason is as follows. Currently, if one wants to create a wrapper function around post() with the same default behavior, the user either needs to (1) duplicate / hard-code the default value in their wrapper function, or (2) special-case None with a second invocation of post() using a different function signature (i.e. one that doesn't pass any content_type value). You can even see this happening in client.py with the way that Client.post() invokes RequestFactory.post() -- the default needs to be hard-coded twice.

Change History (4)

comment:1 Changed 2 months ago by Tim Graham

Triage Stage: UnreviewedAccepted
Type: New featureCleanup/optimization

Should the same design be used for the other HTTP methods that have content_type='application/octet-stream'?

comment:2 Changed 2 months ago by Chris Jerdonek

Oh, good observation. I didn't examine the other methods. Yes, I do.

Generally, I've found this type of style / pattern helpful anywhere an API has a default value. Perhaps other areas of Django could use a similar treatment. Might it be a candidate for the style guide?

comment:3 Changed 2 months ago by Chris Jerdonek

Owner: changed from nobody to Chris Jerdonek
Status: newassigned
Summary: Support passing content_type=None to test.Client.post() to get the default valueSupport passing None to test Client and RequestFactory request methods for default content_type

comment:4 Changed 2 months ago by Chris Jerdonek

Note: See TracTickets for help on using tickets.
Back to Top