Edgewall Software
Modify

Opened 5 years ago

Closed 5 years ago

Last modified 5 years ago

#11766 closed enhancement (fixed)

[PATCH] __repr__ for instances of Ticket, Attachment, WikiPage etc

Reported by: Alex Willmer <al.willmer@…> Owned by: Alex Willmer <al.willmer@…>
Priority: normal Milestone: 1.1.3
Component: general Version:
Severity: normal Keywords:
Cc: Branch:
Release Notes:

Model objects now use their name or id as part of their repr e.g. WikiPage u'StartPage@5'>.

API Changes:

Description

When working with tracebacks generated by Trac it's often useful to know which ticket/wiki page or milestone was instantiated in a particular variable. However it's not obvious from a repr such as <Ticket 0xabcdef> what that is.

Attached is a patch that replaces the repr() of model-like classes with one close to that of a Resource e.g. WikiPage u'StartPage@5'>.

Attachments (1)

0.12.x_models_repr.patch (15.1 KB ) - added by Alex Willmer <al.willmer@…> 5 years ago.

Download all attachments as: .zip

Change History (13)

by Alex Willmer <al.willmer@…>, 5 years ago

Attachment: 0.12.x_models_repr.patch added

comment:1 by Ryan J Ollos, 5 years ago

Milestone: 1.0.3

Looks like a good idea.

comment:2 by anonymous, 5 years ago

+1 on this

comment:3 by Christian Boos, 5 years ago

Milestone: 1.0.31.1.3

1.1dev perhaps?

comment:4 by Ryan J Ollos, 5 years ago

Changes rebased and some minor fixups in log:rjollos.git:t11766.

comment:5 by Jun Omae, 5 years ago

The test_repr failed with PostgreSQL and MySQL.

======================================================================
FAIL: test_repr (trac.ticket.tests.model.TicketTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/run/shm/37a7ce72d102deeee2ba03f8c708a43e37652eb5/py26-mysql/trac/ticket/tests/model.py", line 125, in test_repr
    self.assertEqual("<Ticket 1>", repr(ticket))
AssertionError: '<Ticket 1>' != '<Ticket 1L>'

----------------------------------------------------------------------
Ran 1681 tests in 501.982s

FAILED (failures=1)
make: *** [unit-test] Error 1

comment:6 by Ryan J Ollos, 5 years ago

Owner: set to Ryan J Ollos
Status: newassigned

comment:7 by Ryan J Ollos, 5 years ago

Any thoughts on this guideline for the __repr__ method?:

If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment).

For the case of Environment, we could use something like:

return '%s(%r)' % (self.__class__.__name__, self.path)

Rather than <Environment u'/path/to/env'> we'd see Environment(u'/path/to/env'), or we could leave the angled brackets: < Environment(u'/path/to/env') >.

in reply to:  7 comment:8 by Ryan J Ollos, 5 years ago

Resolution: fixed
Status: assignedclosed

Replying to rjollos:

Any thoughts on this guideline for the __repr__ method?:

If at all possible, this should look like a valid Python expression that could be used to recreate an object with the same value (given an appropriate environment).

I'll leave this as a possible future change, pending feedback.

Fixed test failures from comment:5 and committed to trunk in [13503].

comment:9 by Ryan J Ollos, 5 years ago

Owner: changed from Ryan J Ollos to Alex Willmer <al.willmer@…>

comment:10 by Peter Suter, 5 years ago

On Windows this test fails:

FAIL: test_repr (trac.tests.config.ConfigurationTestCase)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "d:\code\trac\trunk\trac\tests\config.py", line 52, in test_repr
    repr(config))
AssertionError: "<Configuration 'c:\\users\\peter\\appdata\\local\\temp\\trac-test.ini'>" != "<Configuration 'c:\\\\users\\\\peter\\\\appdata\\\\local\\\\temp\\
\\trac-test.ini'>"

It seems '%s' % filename and '%r' % filename behave differently with e.g. the backslash path separators. I guess the test should use %r as well?

Last edited 5 years ago by Peter Suter (previous) (diff)

comment:11 by Ryan J Ollos, 5 years ago

Thanks, should be fixed in [13514].

comment:12 by Ryan J Ollos, 5 years ago

[13567]: Fixed issue in [13503#file5] due to incorrect rebasing before commit.

Modify Ticket

Change Properties
Set your email in Preferences
Action
as closed The owner will remain Alex Willmer <al.willmer@…>.
The resolution will be deleted. Next status will be 'reopened'.
to as closed The owner will be changed from Alex Willmer <al.willmer@…> to the specified user.

Add Comment


E-mail address and name can be saved in the Preferences .
 
Note: See TracTickets for help on using tickets.