Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#16398 closed New feature (invalid)

HttpResponse object should return self when write() method is called

Reported by: Jonathan Sawyer Owned by: nobody
Component: HTTP handling Version: 1.3
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description (last modified by Luke Plant)

My use case:

# Current procedure
def myview(req):
    resp = HttpResponse(mimetype='application/javascript')
    resp.write(json.dumps({'msg': 'Hello World!'}))
    return resp

# Desired procedure
def myview(req):
    resp = HttpResponse(mimetype='application/javascript')
    return resp.write(json.dumps({'msg': 'Hello World!'}))

HttpResponse.write() returns None initially. It would be really nice if .write() would return self so I can return an HttpResponse object. Instead I have to take up an extra line of code (and in my application, that is a lot when duplicated).

An extension on this request could be that all public methods on HttpResponse should return self.

Attachments (1)

16398.patch (422 bytes) - added by Jonathan Sawyer 5 years ago.
Patch to #16389 where self is returned on .write() method

Download all attachments as: .zip

Change History (6)

Changed 5 years ago by Jonathan Sawyer

Attachment: 16398.patch added

Patch to #16389 where self is returned on .write() method

comment:1 Changed 5 years ago by Jonathan Sawyer

Attached is my patch. I have tested this functionality and it works when objects use HttpResponse as file objects. As far as I know, this won't break python compatibility because on errors, exceptions are raised rather than the output being the form of an error code. Thanks in advance.

comment:3 Changed 5 years ago by Jonathan Sawyer

Has patch: set

comment:4 Changed 5 years ago by Luke Plant

Description: modified (diff)

comment:5 Changed 5 years ago by Luke Plant

Resolution: invalid
Status: newclosed

The write() method follows the pattern of file-like objects, the write method for files returns None. This change would make one small use case easier, but is not at all an obvious thing for write to return.

comment:6 Changed 5 years ago by Jacob

milestone: 1.4

Milestone 1.4 deleted

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