Code

Opened 3 years ago

Closed 3 years ago

Last modified 3 years ago

#16398 closed New feature (invalid)

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

Reported by: jonmsawyer 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 lukeplant)

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 jonmsawyer 3 years ago.
Patch to #16389 where self is returned on .write() method

Download all attachments as: .zip

Change History (6)

Changed 3 years ago by jonmsawyer

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

comment:1 Changed 3 years ago by jonmsawyer

  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset

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 3 years ago by jonmsawyer

  • Has patch set

comment:4 Changed 3 years ago by lukeplant

  • Description modified (diff)

comment:5 Changed 3 years ago by lukeplant

  • Resolution set to invalid
  • Status changed from new to closed

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 3 years ago by jacob

  • milestone 1.4 deleted

Milestone 1.4 deleted

Add Comment

Modify Ticket

Change Properties
<Author field>
Action
as closed
as The resolution will be set. Next status will be 'closed'
The resolution will be deleted. Next status will be 'new'
Author


E-mail address and user name can be saved in the Preferences.

 
Note: See TracTickets for help on using tickets.