#11766 closed enhancement (fixed)
[PATCH] __repr__ for instances of Ticket, Attachment, WikiPage etc
| Reported by: | Owned by: | ||
|---|---|---|---|
| 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 |
||
| API Changes: | |||
| Internal 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)
Change History (13)
by , 11 years ago
| Attachment: | 0.12.x_models_repr.patch added |
|---|
comment:1 by , 11 years ago
| Milestone: | → 1.0.3 |
|---|
comment:5 by , 11 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 , 11 years ago
| Owner: | set to |
|---|---|
| Status: | new → assigned |
follow-up: 8 comment:7 by , 11 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') >.
comment:8 by , 11 years ago
| Resolution: | → fixed |
|---|---|
| Status: | assigned → closed |
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 , 11 years ago
| Owner: | changed from to |
|---|
comment:10 by , 11 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?
comment:12 by , 11 years ago
[13567]: Fixed issue in [13503#file5] due to incorrect rebasing before commit.



Looks like a good idea.