Opened 6 years ago

Last modified 4 years ago

#24121 new New feature

Provide a better repr() experience

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


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 < … 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:

  • StaticNode
  • BlockTranslateNode
  • URLNode
  • Token
  • Lexer
  • Parser
  • FilterExpression
  • RedirectFallbackMiddleware
  • MessageMiddleware
  • RemoteUserMiddleware
  • Col
  • Lookup
  • BaseDatabaseWrapper
  • SQLCompiler
  • JoinPromoter
  • HttpResponseNotAllowed
  • PermWrapper
  • SessionStorage
  • CookieStorage
  • FallbackStorage
  • ChangeList
  • AdminForm
  • InlineAdminFormSet
  • CsrfViewMiddleware
  • Response
  • SimpleTemplateResponse
  • TemplateResponse
  • AdminSite
  • ModelAdmin
  • Origin
  • TemplateOrigin
  • BlockContext
  • IncludeNode
  • Template/Engine (would need work still I think)

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 (6)

comment:1 Changed 6 years ago by Josh Smeaton

Triage Stage: UnreviewedAccepted

comment:2 Changed 6 years ago by Berker Peksag

<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 Changed 5 years ago by Tim Graham <timograham@…>

In c96f112:

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

comment:4 Changed 5 years ago by Tim Graham <timograham@…>

In 9c40f01a:

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

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

In d4b10a72:

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

comment:6 Changed 4 years ago by Tim Graham

Component: UncategorizedCore (Other)
Note: See TracTickets for help on using tickets.
Back to Top