Skip to content

Commit 65edf87

Browse files
committed
display the repr of some global names (fixes #171)
1 parent 4d4b551 commit 65edf87

File tree

3 files changed

+22
-7
lines changed

3 files changed

+22
-7
lines changed

CHANGELOG

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
Changes between 2.3.4 and 2.3.5dev
22
-----------------------------------
33

4+
- fix issue171 - in assertion rewriting, show the repr of some
5+
global variables
6+
47
- fix option help for "-k"
58

69
- move long description of distribution into README.rst
@@ -119,7 +122,7 @@ Changes between 2.2.4 and 2.3.0
119122

120123
- fix issue202 - better automatic names for parametrized test functions
121124
- fix issue139 - introduce @pytest.fixture which allows direct scoping
122-
and parametrization of funcarg factories.
125+
and parametrization of funcarg factories.
123126
- fix issue198 - conftest fixtures were not found on windows32 in some
124127
circumstances with nested directory structures due to path manipulation issues
125128
- fix issue193 skip test functions with were parametrized with empty

_pytest/assertion/rewrite.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,9 @@ def rewrite_asserts(mod):
262262
_saferepr = py.io.saferepr
263263
from _pytest.assertion.util import format_explanation as _format_explanation
264264

265+
def _should_repr_global_name(obj):
266+
return not hasattr(obj, "__name__") and not py.builtin.callable(obj)
267+
265268
def _format_boolop(explanations, is_or):
266269
return "(" + (is_or and " or " or " and ").join(explanations) + ")"
267270

@@ -473,11 +476,12 @@ def visit_Assert(self, assert_):
473476
return self.statements
474477

475478
def visit_Name(self, name):
476-
# Check if the name is local or not.
479+
# Display the repr of the name if it's a local variable or
480+
# _should_repr_global_name() thinks it's acceptable.
477481
locs = ast.Call(self.builtin("locals"), [], [], None, None)
478-
globs = ast.Call(self.builtin("globals"), [], [], None, None)
479-
ops = [ast.In(), ast.IsNot()]
480-
test = ast.Compare(ast.Str(name.id), ops, [locs, globs])
482+
inlocs = ast.Compare(ast.Str(name.id), [ast.In()], [locs])
483+
dorepr = self.helper("should_repr_global_name", name)
484+
test = ast.BoolOp(ast.Or(), [inlocs, dorepr])
481485
expr = ast.IfExp(test, self.display(name), ast.Str(name.id))
482486
return name, self.explanation_param(expr)
483487

testing/test_assertrewrite.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,15 @@ def f():
107107
assert getmsg(f) == "assert False"
108108
def f():
109109
assert a_global
110-
assert getmsg(f, {"a_global" : False}) == "assert a_global"
110+
assert getmsg(f, {"a_global" : False}) == "assert False"
111+
def f():
112+
assert sys == 42
113+
assert getmsg(f, {"sys" : sys}) == "assert sys == 42"
114+
def f():
115+
assert cls == 42
116+
class X(object):
117+
pass
118+
assert getmsg(f, {"cls" : X}) == "assert cls == 42"
111119

112120
def test_assert_already_has_message(self):
113121
def f():
@@ -232,7 +240,7 @@ def f():
232240
def test_attribute(self):
233241
class X(object):
234242
g = 3
235-
ns = {"X" : X, "x" : X()}
243+
ns = {"x" : X}
236244
def f():
237245
assert not x.g
238246
assert getmsg(f, ns) == """assert not 3

0 commit comments

Comments
 (0)