-
Notifications
You must be signed in to change notification settings - Fork 134
Expand file tree
/
Copy pathprettyassert.py
More file actions
390 lines (325 loc) · 13.3 KB
/
prettyassert.py
File metadata and controls
390 lines (325 loc) · 13.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
"""
Make assert statements print pretty output, including source.
This makes ``assert x == y`` more usable, as an alternative to
``self.assertEqual(x, y)``
This plugin implements :func:`outcomeDetail` and checks for event.exc_info
If it finds that an AssertionError happened, it will inspect the traceback and
add additional detail to the error report.
"""
from __future__ import annotations
import collections
import inspect
import io
import re
import textwrap
import tokenize
import typing as t
from nose2 import events
__unittest = True
class PrettyAssert(events.Plugin):
"""Add pretty output for "assert" statements"""
configSection = "pretty-assert"
commandLineSwitch = (
None,
"pretty-assert",
'Add pretty output for "assert" statements',
)
def outcomeDetail(self, event):
# skip if no exception or expected error
if (not event.outcomeEvent.exc_info) or event.outcomeEvent.expected:
return
# unpack, but skip if it's not an AssertionError
excty, exc, trace = event.outcomeEvent.exc_info
if excty is not AssertionError:
return
self.addAssertDetail(event.extraDetail, exc, trace)
@staticmethod
def addAssertDetail(extraDetail, exc, trace):
"""
Add details to output regarding AssertionError and its context
extraDetail: a list of lines which will be joined with newlines and
added to the output for this test failure -- defined as part of the
event format
exc: the AssertionError exception which was thrown
trace: a traceback object for the exception
"""
assert_statement, token_descriptions = _collect_assert_data(trace)
# no message was given
if len(exc.args) == 0:
message = None
else:
message = exc.args[0]
# if there is no assertion statement found, do not add detail to output
#
# in cases like unittest assert*() methods, an assertion error is
# raised, but it doesn't originate with an `assert` statement and has
# an autogenerated message
if not assert_statement:
return
#
# actually add exception info to detail
#
# add the assert statement to output with '>>>' prefix
extraDetail.append(re.sub("^", ">>> ", assert_statement, flags=re.MULTILINE))
if message:
extraDetail.append("\nmessage:")
extraDetail.append(f" {message}")
if token_descriptions:
extraDetail.append("\nvalues:")
for k, v in token_descriptions.items():
extraDetail.append(f" {k} = {v}")
def _collect_assert_data(trace):
"""
Given a traceback, extract the assertion statement and get the set of bound
variable names (i.e. tokens)
"""
# inspect the trace, collecting various data and determining whether or not
# it can be tokenized at all
source_lines, frame_locals, frame_globals, can_tokenize = _get_inspection_info(
trace
)
# if things will tokenize cleanly, actually do it
if can_tokenize:
assert_startline, token_descriptions = _tokenize_assert(
source_lines, frame_locals, frame_globals
)
# otherwise, indicate that we can't render detail by use of Nones
else:
assert_startline = None
token_descriptions = None
# if we found an "assert" (we might not, if someone raises
# AssertionError themselves), grab the whole assertion statement
#
# as a fallback, stick with whatever we think the statement was
# - this is easily deceived by multiline expressions
if assert_startline is not None:
statement = textwrap.dedent(
"".join(source_lines[assert_startline:]).rstrip("\n")
)
else:
statement = None
return statement, token_descriptions
def _get_inspection_info(trace):
"""
Pick apart a traceback for the info we actually want to inspect from it
- lines of source (truncated)
- locals and globals from execution frame
- statement which failed (which can be garbage -- don't trust it)
- can_tokenize: a bool indicating that the lines of source can be parsed
"""
frame, fname, lineno, funcname, context, ctx_index = inspect.getinnerframes(trace)[
-1
]
original_source_lines, firstlineno = inspect.getsourcelines(frame)
# truncate to the code in this frame to remove anything after current
# assert statement
last_index = lineno - firstlineno + 1
source_lines = original_source_lines[:last_index]
# in case the current line is actually an incomplete expression, as in
# assert x == (y
# ).z
#
# in which case the the current line is "assert x == (y", which is not a
# complete expression
# try to append lines to complete the expression, retrying parsing each and
# every time until it succeeds
for line in original_source_lines[last_index:]:
if _can_tokenize(source_lines):
break
else:
source_lines.append(line)
return (source_lines, frame.f_locals, frame.f_globals, _can_tokenize(source_lines))
def _can_tokenize(source_lines):
"""
Check if a list of lines of source can successfully be tokenized
"""
# tokenize.generate_tokens requires a file-like object, so we need to
# convert source_lines to a StringIO to give it that interface
filelike = io.StringIO(textwrap.dedent("".join(source_lines)))
try:
for _tokty, _tok, _start, _end, _tok_lineno in tokenize.generate_tokens(
filelike.readline
):
pass
except tokenize.TokenError:
return False
return True
def _tokenize_assert(source_lines, frame_locals, frame_globals):
"""
Given a set of lines of source ending in a failing assert, plus the frame
locals and globals, tokenize source.
Only look at tokens in the final assert statement
Resolve all names to repr() of values
Return
The line on which the assert starts (relative to start of
source_lines)
A collection of token descriptions as a name=val ordered dict
"""
# tokenize.generate_tokens requires a file-like object, so we need to
# convert source_lines to a StringIO to give it that interface
filelike_context = io.StringIO(textwrap.dedent("".join(source_lines)))
# track the first line of the assert statement
# when the assert is on oneline, we'll have it easily, but a multiline
# statement like
# assert (x ==
# 1)
# will leave us holding the last line of the statement,
# e.g. " 1)", which is not useful
# so every time a new assert is found, we get a value back indicate
# that it's the start line
#
# assert True
# assert False
# works fine, because we'll just hold the last value
#
# assert True
# assert False
# assert True
# also works because we truncated source_lines to remove the final
# assert, which we didn't reach during execution
assert_startline = None
token_processor = TokenProcessor(frame_locals, frame_globals)
# tokenize and process each token
for tokty, tok, start, end, tok_lineno in tokenize.generate_tokens(
filelike_context.readline
):
ret = token_processor.handle_token(tokty, tok, start, end, tok_lineno)
if ret:
assert_startline = ret
# adjust assert_startline by 1 to become a valid index into the
# source_lines -- "line 1" means "index 0"
if assert_startline:
assert_startline -= 1
token_descriptions = collections.OrderedDict()
for name, obj in token_processor.get_token_collection().items():
# okay, get repr() for a good string representation
strvalue = repr(obj)
# add in the form we want to print
token_descriptions[name] = strvalue
return assert_startline, token_descriptions
class TokenProcessor:
def __init__(self, frame_locals, frame_globals) -> None:
# local and global variables from the frame which we're inspecting
self.frame_locals, self.frame_globals = frame_locals, frame_globals
# None or a tuple of (object, name) where
# - "object" is the object whose attributes we are currently resolving
# - "name" is its name, as we would like to display it
#
# start each time we see a sequence of NAME OP NAME OP NAME (etc.)
# end each time we see a token which is neither NAME nor OP
self.doing_resolution = None
# an index of known token names (including the long "x.y.z" names we
# get from attribute resolution) to their values, in the order in which
# they were encountered
# track which tokens we've seen to avoid duplicates if a name appears
# twice, as in `assert x != x`
self.seen_tokens: dict[str, t.Any] = {}
# the previous token seen as a tuple of (tok_type, token_name)
# (or None when we start)
self.last_tok = None
def get_token_collection(self):
return self.seen_tokens
def handle_token(self, toktype, tok, start, end, line):
"""
A tokenization processor for tokenize.generate_tokens
Skips certain token types, class names, etc
When an identifiable/usable token is found, add it to the token
collection (self.seen_tokens)
When an "assert" statement is found, reset the token collection
and return the start line (relative to the text being tokenized)
"""
prior_tok = self.last_tok
self.last_tok = (toktype, tok)
# CASE 0: skip non "NAME" or "OP" tokens and clear current resolution
#
# NAME is most identifiers and keywords
# OP is operators, including .
#
# special note: don't clear resolution for whitespace (e.g. newline)
if toktype not in (tokenize.NAME, tokenize.OP):
# only newline for now, maybe we'll find others
if toktype not in (tokenize.NEWLINE,):
self.doing_resolution = None
return
# CASE 1: Operator token
#
# skip tokens and either leave resolution in progress or reset,
# depending
#
# continue resolution for
# "."
# because that's what attribute resolution *is*
# ")"
# this is handy, as it means that "(x).y" works
#
# reset resolution for everything else, e.g. "[", "]", ":"
# special note: reset resolution for "("
#
# failing to filter out "(" can result in badness in cases like this:
# >>> def foo():
# >>> return [1]
# >>> foo.pop = 2
# >>> ...
# >>> def test_foo():
# >>> assert foo().pop() == 2
#
# if we stop resolution when we see an LPAREN, we resolve `foo`
# successfully, fail on `pop` and everything is OK, but if we try to
# traverse the LPAREN, we get `foo.pop = 2` in our values, which is
# wrong
if toktype == tokenize.OP:
if tok not in (".", ")"):
self.doing_resolution = None
return
# CASE 2: "assert" statement
# assert statement was reached, reset
# return the start line (start = (startrow, startcol))
if tok == "assert":
self.seen_tokens.clear()
self.doing_resolution = None
return start[0]
# handle tokens
# CASE 3: a name is being resolved,
# there is a previous token,
# and it's a "." operator
if (
self.doing_resolution
and prior_tok
and (prior_tok[0] == tokenize.OP and prior_tok[1] == ".")
):
# unpack and look for the attribute
obj, name = self.doing_resolution
if hasattr(obj, tok):
obj = getattr(obj, tok)
name = name + "." + tok
self.doing_resolution = (obj, name)
self.seen_tokens[name] = obj
# if we couldn't find a relevant attribute, reset on resolution so
# that we can try afresh
else:
self.doing_resolution = None
# CASE 4: a name is being resolved and there is no preceding "." or
# resolution was explicitly stopped
else:
# skip tokens we've seen, but grab them as the current things under
# resolution
if tok in self.seen_tokens:
self.doing_resolution = (self.seen_tokens[tok], tok)
return
# we've never seen this token before
else:
# try to resolve to a value
try:
value = self.frame_locals[tok]
except KeyError:
try:
value = self.frame_globals[tok]
except KeyError:
# unresolveable name -- short circuit
# shows up in some cases like `f().x` in which `x`
# might not be a name bound to a value
return
# add it (so we don't try it again unless we hit a new assert
# and reset)
self.seen_tokens[tok] = value
self.doing_resolution = (value, tok)