diff --git a/executing/executing.py b/executing/executing.py index 3b5ad07..df5f653 100644 --- a/executing/executing.py +++ b/executing/executing.py @@ -584,6 +584,14 @@ def __init__(self, frame, stmts, tree, lasti, source): else: raise + if isinstance(node, ast.Assert): + # pytest assigns the position of the assertion to all expressions of the rewritten assertion. + # All the rewritten expressions get mapped to ast.Assert, which is the wrong ast-node. + # We don't report this wrong result. + self.result = None + self.decorator = None + return + # find decorators if ( isinstance(node.parent, (ast.ClassDef, function_node_types)) diff --git a/tests/sample_results/3.11.json b/tests/sample_results/3.11.json index 495daad..6f6ba6a 100644 --- a/tests/sample_results/3.11.json +++ b/tests/sample_results/3.11.json @@ -18148,11 +18148,11 @@ ], [ "COMPARE_OP", - "assert 1 <= month <= 12, month" + "" ], [ "COMPARE_OP", - "assert 1 <= month <= 12, month" + "" ], [ "LOAD_FAST", @@ -18160,7 +18160,7 @@ ], [ "CALL", - "assert 1 <= month <= 12, month" + "" ], [ "LOAD_FAST", @@ -18200,15 +18200,15 @@ ], [ "COMPARE_OP", - "assert 1 <= month <= 12, 'month must be in 1..12'" + "" ], [ "COMPARE_OP", - "assert 1 <= month <= 12, 'month must be in 1..12'" + "" ], [ "CALL", - "assert 1 <= month <= 12, 'month must be in 1..12'" + "" ], [ "LOAD_GLOBAL", @@ -18252,15 +18252,15 @@ ], [ "COMPARE_OP", - "assert 1 <= month <= 12, 'month must be in 1..12'" + "" ], [ "COMPARE_OP", - "assert 1 <= month <= 12, 'month must be in 1..12'" + "" ], [ "CALL", - "assert 1 <= month <= 12, 'month must be in 1..12'" + "" ], [ "LOAD_GLOBAL", @@ -18288,7 +18288,7 @@ ], [ "COMPARE_OP", - "assert 1 <= day <= dim, ('day must be in 1..%d' % dim)" + "" ], [ "LOAD_FAST", @@ -18296,7 +18296,7 @@ ], [ "COMPARE_OP", - "assert 1 <= day <= dim, ('day must be in 1..%d' % dim)" + "" ], [ "LOAD_FAST", @@ -18308,7 +18308,7 @@ ], [ "CALL", - "assert 1 <= day <= dim, ('day must be in 1..%d' % dim)" + "" ], [ "LOAD_GLOBAL", @@ -18724,7 +18724,7 @@ ], [ "COMPARE_OP", - "assert 0 <= n < _days_in_month(year, month)" + "" ], [ "LOAD_GLOBAL", @@ -18744,7 +18744,7 @@ ], [ "COMPARE_OP", - "assert 0 <= n < _days_in_month(year, month)" + "" ], [ "LOAD_FAST", @@ -23388,11 +23388,11 @@ ], [ "COMPARE_OP", - "assert isinstance(s, int) and 0 <= s < 24*3600" + "" ], [ "COMPARE_OP", - "assert isinstance(s, int) and 0 <= s < 24*3600" + "" ], [ "LOAD_GLOBAL", @@ -23416,11 +23416,11 @@ ], [ "COMPARE_OP", - "assert isinstance(us, int) and 0 <= us < 1000000" + "" ], [ "COMPARE_OP", - "assert isinstance(us, int) and 0 <= us < 1000000" + "" ], [ "LOAD_GLOBAL", @@ -29076,7 +29076,7 @@ ], [ "CALL", - "assert not m % timedelta(minutes=1), \"whole minute\"" + "" ], [ "LOAD_FAST", @@ -50008,11 +50008,11 @@ ], [ "COMPARE_OP", - "assert list(gen()) == list(gen2) == [1, 2]" + "" ], [ "COMPARE_OP", - "assert list(gen()) == list(gen2) == [1, 2]" + "" ], [ "STORE_FAST", @@ -51900,7 +51900,7 @@ ], [ "CALL", - "assert isinstance(node, typ), (node, typ)" + "" ], [ "LOAD_FAST", @@ -52016,7 +52016,7 @@ ], [ "CALL", - "assert result == value, (result, value)" + "" ], [ "LOAD_FAST", @@ -54970,7 +54970,7 @@ ], [ "CALL", - "assert isinstance(result, ChangeValue), \"after_expr must return None or an instance of ChangeValue\"" + "" ], [ "LOAD_FAST", @@ -58044,7 +58044,7 @@ ], [ "CALL", - "assert len(pair) == 2, \"Watch extra must return pair or None\"" + "" ], [ "LOAD_FAST", diff --git a/tests/test_pytest.py b/tests/test_pytest.py index 3189010..e9f0013 100644 --- a/tests/test_pytest.py +++ b/tests/test_pytest.py @@ -1,10 +1,14 @@ import os import sys +import inspect +import ast from littleutils import SimpleNamespace from executing.executing import is_ipython_cell_code +from executing import Source,NotOneValueFound + sys.path.append(os.path.dirname(os.path.dirname(__file__))) @@ -23,6 +27,18 @@ def test_pytest(): assert x is tester +def this_expression(): + try: + return Source.executing(inspect.currentframe().f_back).node + except NotOneValueFound: + # TODO: python < 3.11 raises an exception. Should we do the same for the PositionNodeFinder instead of returning None? + return None + + +def test_assert(): + assert isinstance(this_expression(),(ast.Call, type(None))) + + def test_ipython_cell_code(): assert is_ipython_cell_code( SimpleNamespace(