Opened 10 years ago

Closed 3 years ago

Last modified 7 weeks ago

#24121 closed New feature (fixed)

Provide a better repr() experience

Reported by: Keryn Knight Owned by: Nicolás Restrepo
Component: Core (Other) Version: dev
Severity: Normal Keywords:
Cc: django@… Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: yes UI/UX: no

Description (last modified by Mariusz Felisiak)

For a long time, Django has shipped with the debug 500 handler, which defaults to a view where "local vars" for every stack frame can be expanded and inspected to see the internal state at the various points in the stack trace.

Between that renderer, the heavy usage of django-debug-toolbar, which ships with a template context inspector, and my preference throwing pdb everywhere, there is a notable lack of a good repr() experience in much of Django, both in debugging public API (that is, the bits a user will use and encounter), and internals that might be exposed, or could otherwise be served better by having a repr that says something more than it's address/id.

A couple of previous tickets have addressed this in minor, one off ways - #23167, #22906, #22531 - but as I've begun collecting places they could supply useful info as I spot them in my stuff, they've become a hodgepodge across a number of modules.

For example, a couple I've got implemented:

<TemplateResponse status_code=200, charset=utf-8, _is_rendered=False>

<LoaderOrigin name=/absolute/path/to/templates/admin/change_list.html, loadname=admin/change_list.html>

<FilterExpression var=u'admin/change_list.html', filters=[]>

All of which, IMHO, provide a more useful context in which to evaluate state.

The branch I have implemented my reprs in is in no way merge ready yet, partially because there's no tests, and it'd need rebasing & squashing, but also because there are some parts I'd like to see reprs on but haven't landed on a good way to do so.
An example being the concept of a Loader, which currently has no repr() and thus shows up as <path.to.cached.Loader … 0xETC> which at least tells us which loader is being referenced. Given the convention so far for reprs() is just the class name (rather than module + class), the fact all Loaders (and now Engines) all share the same class name means in exposing more useful internal state we lose knowledge of the loader in question's module namespace.

As I write this, I've got at least rudimentary (as in, it was possibly to replace the 0x... with at least one object-state variable) reprs for:

If accepted, this ticket could just track whatever I can end up getting reviewed/merged, or it could be a meta-ticket for accepting as a principle, and I could spin off tickets for more targeted areas.

Change History (57)

comment:1 by Josh Smeaton, 10 years ago

Triage Stage: UnreviewedAccepted

comment:2 by Berker Peksag, 10 years ago

<TemplateResponse status_code=200, charset=utf-8, _is_rendered=False>

<LoaderOrigin name=/absolute/path/to/templates/admin/change_list.html, loadname=admin/change_list.html>

<FilterExpression var=u'admin/change_list.html', filters=[]>

charset, name and loadname should also be a string.

<TemplateResponse status_code=200, charset='utf-8', _is_rendered=False>

<LoaderOrigin name='/absolute/path/to/templates/admin/change_list.html', loadname='admin/change_list.html'>

Here's an example from Python:

>>> open('README')
<_io.TextIOWrapper name='README' mode='r' encoding='UTF-8'>

comment:3 by Tim Graham <timograham@…>, 9 years ago

In c96f112:

Refs #24121 -- Added meaningful repr() to HttpResponse and subclasses.

comment:4 by Tim Graham <timograham@…>, 9 years ago

In 9c40f01a:

Refs #24121 -- Added repr() to LazySettings, Settings, and UserSettingsHolder.

comment:5 by Tim Graham <timograham@…>, 9 years ago

In d4b10a72:

Refs #24121 -- Added a repr() to gis.GeoIP and gis.GeoIP2.

comment:6 by Tim Graham, 8 years ago

Component: UncategorizedCore (Other)

comment:7 by David Smith, 4 years ago

Easy pickings: set

Marking as easy pickings as I think this can be achieved in stages -- I'm not expecting someone to propose a PR that addresses all of these in one go!

comment:8 by Rohith P R, 4 years ago

In https://github.com/django/django/pull/14045: Refs #24121 -- Added repr to django.template.defaulttags.URLNode

Last edited 4 years ago by Rohith P R (previous) (diff)

comment:9 by Mariusz Felisiak, 4 years ago

Description: modified (diff)

comment:10 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

In 179ee13:

Refs #24121 -- Added repr() to FilterExpression, Lexer, Parser, and Token.

comment:11 by Mariusz Felisiak, 4 years ago

Description: modified (diff)

comment:12 by Rohith P R, 4 years ago

Has patch: set

comment:13 by Mariusz Felisiak, 4 years ago

Patch needs improvement: set

comment:14 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

In d3ecef26:

Refs #24121 -- Added repr() to URLNode.

comment:15 by Mariusz Felisiak, 4 years ago

Description: modified (diff)
Has patch: unset
Patch needs improvement: unset

comment:16 by Nick Pope, 4 years ago

Has patch: set

comment:17 by GitHub <noreply@…>, 4 years ago

In afb0eb8:

Refs #24121 -- Added repr() to OrderedSet.

comment:18 by Mariusz Felisiak, 4 years ago

Description: modified (diff)
Has patch: unset

comment:19 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

In dc86a25a:

Refs #24121 -- Added repr() to MiddlewareMixin and subclasses.

comment:20 by Mariusz Felisiak, 4 years ago

Description: modified (diff)

comment:21 by Mariusz Felisiak, 4 years ago

Description: modified (diff)

comment:22 by Mariusz Felisiak, 4 years ago

Description: modified (diff)

comment:23 by Jonny Park, 4 years ago

Query Does not have __repr__ as well. Doesn't it need one?

Ah I just found it has __str__

Last edited 4 years ago by Jonny Park (previous) (diff)

comment:24 by tiagohonorato, 4 years ago

comment:25 by Mariusz Felisiak, 4 years ago

Description: modified (diff)

comment:26 by Mariusz Felisiak <felisiak.mariusz@…>, 4 years ago

In 4f4f770:

Refs #24121 -- Added repr() to Origin and Template.

comment:27 by Mariusz Felisiak, 4 years ago

Description: modified (diff)

comment:28 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

In e6406853:

Refs #24121 -- Addedrepr() to StaticNode.

comment:29 by Mariusz Felisiak, 3 years ago

Description: modified (diff)

comment:30 by Mariusz Felisiak, 3 years ago

Description: modified (diff)

comment:31 by Mariusz Felisiak, 3 years ago

Description: modified (diff)

comment:32 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

In f0a9413b:

Refs #24121 -- Improved Value.repr().

comment:33 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

In d3d95d6:

Refs #24121 -- Added repr() to Lookup.

comment:34 by Mariusz Felisiak, 3 years ago

Description: modified (diff)

comment:35 by Abhyudai, 3 years ago

Description: modified (diff)

comment:36 by Abhyudai, 3 years ago

Description: modified (diff)

comment:37 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

In 22da686c:

Refs #24121 -- Added repr() to PermWrapper.

comment:38 by Mariusz Felisiak, 3 years ago

Description: modified (diff)

comment:39 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

In c609d514:

Refs #24121 -- Added repr() to Engine

comment:40 by Mariusz Felisiak, 3 years ago

Description: modified (diff)

comment:41 by Mariusz Felisiak, 3 years ago

Description: modified (diff)

comment:42 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

In 7dd502b:

Refs #24121 -- Added repr() to ChangeList and BaseStorage.

comment:43 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

In 66ed03e:

Refs #24121 -- Added repr() to AdminForm, BlockContext, BlockTranslateNode, and IncludeNode.

comment:44 by Mariusz Felisiak, 3 years ago

Description: modified (diff)

comment:45 by Nicolás Restrepo, 3 years ago

Owner: changed from nobody to Nicolás Restrepo
Status: newassigned

comment:46 by Nicolás Restrepo, 3 years ago

add StreamingHttpResponse repr() method => https://github.com/django/django/pull/14531

comment:47 by Nicolás Restrepo, 3 years ago

Description: modified (diff)

comment:48 by Nicolás Restrepo, 3 years ago

Description: modified (diff)

comment:49 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

In bbb39658:

Refs #24121 -- Added repr() to StreamingHttpResponse and subclasses.

comment:50 by Mariusz Felisiak, 3 years ago

Description: modified (diff)

comment:51 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

In e4da3654:

Refs #24121 -- Added repr() to AdminSite, DefaultAdminSite, and ModelAdmin.

Thanks tlebrize for the initial patch.

comment:52 by Mariusz Felisiak, 3 years ago

Description: modified (diff)

comment:53 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

In 6fa29305:

Refs #24121 -- Added repr() to BaseDatabaseWrapper, JoinPromoter, and SQLCompiler.

comment:54 by Mariusz Felisiak, 3 years ago

Description: modified (diff)
Resolution: fixed
Status: assignedclosed

comment:55 by Mariusz Felisiak <felisiak.mariusz@…>, 3 years ago

In e95e642:

Refs #24121 -- Added repr() to BaseFormSet.

comment:56 by Mariusz Felisiak <felisiak.mariusz@…>, 15 months ago

In 7bba231b:

Refs #24121 -- Added repr() to Message.

comment:57 by Sarah Boyce <42296566+sarahboyce@…>, 7 weeks ago

In 9ca1f6ef:

Refs #373, Refs #24121 -- Added ColPairs.repr().

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