This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Support unittest assertion truncation of repr in error messages
Type: enhancement Stage: needs patch
Components: Library (Lib) Versions: Python 3.8
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: Camilla Ke, Mariatta, cheryl.sabella, gregory.p.smith, jiliuk, r.david.murray
Priority: normal Keywords: patch

Created on 2016-07-01 09:27 by Camilla Ke, last changed 2022-04-11 14:58 by admin.

Pull Requests
URL Status Linked Edit
PR 13091 closed python-dev, 2019-05-04 21:57
Messages (6)
msg269653 - (view) Author: Camilla Ke (Camilla Ke) Date: 2016-07-01 09:27
Builtin assert methods output messages no matter how length is the output messages.

However, content with exceeding length should be truncated.  

I found an error in the function.

The function is safe_repr() in util.py

if len(result) > _MAX_LENGTH, it should return truncated content, but "short" is default to False. 

unittest/util.py
def safe_repr(obj, short=False):
    try:
        result = repr(obj)
    except Exception:
        result = object.__repr__(obj)
    if not short or len(result) < _MAX_LENGTH:
        return result
    return result[:_MAX_LENGTH] + ' [truncated]...'
msg269668 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2016-07-01 13:52
This is by design.  The short option was introduced to use in specific asserts (assertDictEqual and assertMultilineEqual; see 8b8701551253).  These were later enhanced to do an even better job of abbreviating the output usefully, but the short option on safe_repr was not removed even though it is no longer used, for backward compatibility reasons.

If you would like for there to be a way to toggle safe_repr's default, that would be a new feature request, and seems like a reasononable feature to me.  The new feature should incorporate a way to set _MAX_LENGTH as well.
msg338217 - (view) Author: Cheryl Sabella (cheryl.sabella) * (Python committer) Date: 2019-03-18 13:11
This one is a little more complicated, but I'm going to assign to @Mariatta for the sprints.
msg341404 - (view) Author: Julia (jiliuk) * Date: 2019-05-04 20:57
Working on it at the moment (Mentored Sprint)
msg341792 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2019-05-07 17:49
safe_repr() as used internally by unittest wasn't intended to truncate by default as part of its "safety".  The "safe" part is that it catches Exception and provides an alternate repr if the __repr__ raised.

unittest.util.safe_repr() is a public API, just not documented.  Lets take this issue to enhance it a bit and document it.  Additionally as r.david.murray suggested, a way for unittest.TestCase to configure itself to enable safe_repr truncation by default would be nice.

I'm currently thinking a feature similar to the class variable maxDiff. maxReprLength perhaps.
msg344343 - (view) Author: Gregory P. Smith (gregory.p.smith) * (Python committer) Date: 2019-06-03 00:12
I spent a while looking at this and what it'd take to wind up with a TestCase.maxReprLength style implementation to limit the length of unittest assertion failure reprs. I don't see a nice path forward via safe_repr as is (as the PR started with).

adding max_length as the PR does is easy enough... but plumbing everything through is rather gross. mostly mechanical changes I started in a client before abandoning the work:

 1 change all TestCase method safe_repr calls to self._safe_repr method calls so that the method could pass self.maxReprLength in via max_length=.
 2 realize that this is mostly pointless without also updating unittest.util._common_shorten_repr which calls safe_repr in a similar manner. do that...
 3 and notice how we have many calls of safe_repr on safe_repr'ed things and the nesting between _common_shorten_repr and safe_repr which makes for messy uninformative potentially misleading error messages when maxReprLength was actually set to a value.

Overall I think we should backup and reconsider what we want our unittest assertions to do and how if we're going to add error message length constraining as a feature. shoehorning it in via the existing repr implementations doesn't feel right.

So I closed that PR.

unittest.safe_repr (aka unittest.util.safe_repr) remains an undocumented API.  future to be determined.
History
Date User Action Args
2022-04-11 14:58:33adminsetgithub: 71619
2019-06-03 00:12:40gregory.p.smithsetassignee: gregory.p.smith ->
stage: patch review -> needs patch
2019-06-03 00:12:27gregory.p.smithsetmessages: + msg344343
2019-05-07 17:52:20gregory.p.smithsettitle: Unittest truncating of error message not works -> Support unittest assertion truncation of repr in error messages
2019-05-07 17:49:37gregory.p.smithsetmessages: + msg341792
2019-05-07 17:42:59gregory.p.smithsetassignee: Mariatta -> gregory.p.smith

nosy: + gregory.p.smith
2019-05-04 21:57:55python-devsetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request13005
2019-05-04 20:57:13jiliuksetnosy: + jiliuk
messages: + msg341404
2019-03-18 13:11:50cheryl.sabellasetversions: + Python 3.8, - Python 3.6
nosy: + Mariatta, cheryl.sabella

messages: + msg338217

assignee: Mariatta
stage: needs patch
2017-07-05 20:27:37r.david.murraylinkissue30839 dependencies
2016-07-01 13:52:20r.david.murraysetversions: + Python 3.6, - Python 3.4
nosy: + r.david.murray

messages: + msg269668

type: behavior -> enhancement
2016-07-01 09:27:49Camilla Kecreate