[Django] #23167: BaseForm lacks a useful repr

7 views
Skip to first unread message

Django

unread,
Aug 4, 2014, 3:51:25 AM8/4/14
to django-...@googlegroups.com
#23167: BaseForm lacks a useful repr
-----------------------------------------+--------------------
Reporter: Keryn Knight <django@…> | Owner: nobody
Type: New feature | Status: new
Component: Forms | Version: master
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-----------------------------------------+--------------------
Currently, form instances have a useful `__str__` which renders them out
as a table, but the `__repr__` (or lack thereof) for them is the same old
unhelpful `<module.FormName at 0xDEADBEEF>` which all Python classes get
by default.

It would be more useful to those of us who live in pdb/repl (and debug-
toolbar template contexts -- which use `pprint.pformat` for rendering
values, which internally uses `saferepr`, which as you might expect,
eventually calls `repr(x)`) if the repr had some info about the form
itself.

Off the top of my head, is_bound, field names, valid state would be a
reasonable representation of the form, such that you get something like
`<module.FormName is_bound=True, is_valid=False, fields=('a', 'b', 'c')>`

A slight complication is that the is_valid bool couldn't be obtained via
`is_valid` because that triggers validation -- it would instead need to be
`is_bound and not self._errors` or something (I'm assuming ErrorDict
returns truthy/falsy the same as dict does)
Further, the field names ought to come from the keys of `self.fields`
rather than `base_fields`, as the former is instance specific and often
gets modified.

One could also make the case for adding `has_changed` into the repr, but
in my experience, it's a lesser-used piece of the API; ditto the prefix.

There've been a few tickets for repr-adding goodness (#22906, #22531,
#19543) so I'm assuming that providing a richer REPL experience in general
means there may be merit to this ticket.

--
Ticket URL: <https://code.djangoproject.com/ticket/23167>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Aug 4, 2014, 9:58:20 AM8/4/14
to django-...@googlegroups.com
#23167: BaseForm lacks a useful repr
-------------------------------------+-------------------------------------
Reporter: Keryn Knight | Owner: nobody
<django@…> | Status: new
Type: New feature | Version: master
Component: Forms | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 0 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by timo):

* needs_better_patch: => 0
* needs_docs: => 0
* needs_tests: => 0
* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/23167#comment:1>

Django

unread,
Aug 21, 2014, 1:11:17 PM8/21/14
to django-...@googlegroups.com
#23167: BaseForm lacks a useful repr
-------------------------------------+-------------------------------------
Reporter: Keryn Knight | Owner: nobody
<django@…> | Status: new
Type: New feature | Version: master
Component: Forms | Resolution:
Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 1 | Patch needs improvement: 0

Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by areski):

* cc: areski (added)
* has_patch: 0 => 1


Comment:

Here a PR: https://github.com/django/django/pull/3097
Keryn, do you see this satisfactory?

--
Ticket URL: <https://code.djangoproject.com/ticket/23167#comment:2>

Django

unread,
Aug 22, 2014, 8:07:46 PM8/22/14
to django-...@googlegroups.com
#23167: BaseForm lacks a useful repr
-------------------------------------+-------------------------------------
Reporter: Keryn Knight | Owner: nobody
<django@…> | Status: closed

Type: New feature | Version: master
Component: Forms | Resolution: fixed

Severity: Normal | Triage Stage: Accepted
Keywords: | Needs documentation: 0
Has patch: 1 | Patch needs improvement: 0
Needs tests: 0 | UI/UX: 0
Easy pickings: 0 |
-------------------------------------+-------------------------------------
Changes (by timgraham):

* status: new => closed
* resolution: => fixed


Comment:

In a6691e5dcfdfd1529987be3bdcf06e7ab9948356:

Fixed #23167 -- Added BaseForm.__repr__()

Thanks Keryn Knight for the idea.

--
Ticket URL: <https://code.djangoproject.com/ticket/23167#comment:3>

Reply all
Reply to author
Forward
0 new messages