Skip to content

Commit 4fbbe45

Browse files
fix: do not return Assert nodes, because they are caused by pytest assert rewriting
This affects also chained comparisons inside normal assertions because of python/cpython#95921. This is also the reason for the changes in sample results. once that is fixed we could revert the changes in the sample_results
1 parent 54f026d commit 4fbbe45

File tree

3 files changed

+49
-25
lines changed

3 files changed

+49
-25
lines changed

executing/executing.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -584,6 +584,14 @@ def __init__(self, frame, stmts, tree, lasti, source):
584584
else:
585585
raise
586586

587+
if isinstance(node, ast.Assert):
588+
# pytest assigns the position of the assertion to all expressions of the rewritten assertion.
589+
# All the rewritten expressions get mapped to ast.Assert, which is the wrong ast-node.
590+
# We don't report this wrong result.
591+
self.result = None
592+
self.decorator = None
593+
return
594+
587595
# find decorators
588596
if (
589597
isinstance(node.parent, (ast.ClassDef, function_node_types))

tests/sample_results/3.11.json

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -18148,19 +18148,19 @@
1814818148
],
1814918149
[
1815018150
"COMPARE_OP",
18151-
"assert 1 <= month <= 12, month"
18151+
""
1815218152
],
1815318153
[
1815418154
"COMPARE_OP",
18155-
"assert 1 <= month <= 12, month"
18155+
""
1815618156
],
1815718157
[
1815818158
"LOAD_FAST",
1815918159
"month"
1816018160
],
1816118161
[
1816218162
"CALL",
18163-
"assert 1 <= month <= 12, month"
18163+
""
1816418164
],
1816518165
[
1816618166
"LOAD_FAST",
@@ -18200,15 +18200,15 @@
1820018200
],
1820118201
[
1820218202
"COMPARE_OP",
18203-
"assert 1 <= month <= 12, 'month must be in 1..12'"
18203+
""
1820418204
],
1820518205
[
1820618206
"COMPARE_OP",
18207-
"assert 1 <= month <= 12, 'month must be in 1..12'"
18207+
""
1820818208
],
1820918209
[
1821018210
"CALL",
18211-
"assert 1 <= month <= 12, 'month must be in 1..12'"
18211+
""
1821218212
],
1821318213
[
1821418214
"LOAD_GLOBAL",
@@ -18252,15 +18252,15 @@
1825218252
],
1825318253
[
1825418254
"COMPARE_OP",
18255-
"assert 1 <= month <= 12, 'month must be in 1..12'"
18255+
""
1825618256
],
1825718257
[
1825818258
"COMPARE_OP",
18259-
"assert 1 <= month <= 12, 'month must be in 1..12'"
18259+
""
1826018260
],
1826118261
[
1826218262
"CALL",
18263-
"assert 1 <= month <= 12, 'month must be in 1..12'"
18263+
""
1826418264
],
1826518265
[
1826618266
"LOAD_GLOBAL",
@@ -18288,15 +18288,15 @@
1828818288
],
1828918289
[
1829018290
"COMPARE_OP",
18291-
"assert 1 <= day <= dim, ('day must be in 1..%d' % dim)"
18291+
""
1829218292
],
1829318293
[
1829418294
"LOAD_FAST",
1829518295
"dim"
1829618296
],
1829718297
[
1829818298
"COMPARE_OP",
18299-
"assert 1 <= day <= dim, ('day must be in 1..%d' % dim)"
18299+
""
1830018300
],
1830118301
[
1830218302
"LOAD_FAST",
@@ -18308,7 +18308,7 @@
1830818308
],
1830918309
[
1831018310
"CALL",
18311-
"assert 1 <= day <= dim, ('day must be in 1..%d' % dim)"
18311+
""
1831218312
],
1831318313
[
1831418314
"LOAD_GLOBAL",
@@ -18724,7 +18724,7 @@
1872418724
],
1872518725
[
1872618726
"COMPARE_OP",
18727-
"assert 0 <= n < _days_in_month(year, month)"
18727+
""
1872818728
],
1872918729
[
1873018730
"LOAD_GLOBAL",
@@ -18744,7 +18744,7 @@
1874418744
],
1874518745
[
1874618746
"COMPARE_OP",
18747-
"assert 0 <= n < _days_in_month(year, month)"
18747+
""
1874818748
],
1874918749
[
1875018750
"LOAD_FAST",
@@ -23388,11 +23388,11 @@
2338823388
],
2338923389
[
2339023390
"COMPARE_OP",
23391-
"assert isinstance(s, int) and 0 <= s < 24*3600"
23391+
""
2339223392
],
2339323393
[
2339423394
"COMPARE_OP",
23395-
"assert isinstance(s, int) and 0 <= s < 24*3600"
23395+
""
2339623396
],
2339723397
[
2339823398
"LOAD_GLOBAL",
@@ -23416,11 +23416,11 @@
2341623416
],
2341723417
[
2341823418
"COMPARE_OP",
23419-
"assert isinstance(us, int) and 0 <= us < 1000000"
23419+
""
2342023420
],
2342123421
[
2342223422
"COMPARE_OP",
23423-
"assert isinstance(us, int) and 0 <= us < 1000000"
23423+
""
2342423424
],
2342523425
[
2342623426
"LOAD_GLOBAL",
@@ -29076,7 +29076,7 @@
2907629076
],
2907729077
[
2907829078
"CALL",
29079-
"assert not m % timedelta(minutes=1), \"whole minute\""
29079+
""
2908029080
],
2908129081
[
2908229082
"LOAD_FAST",
@@ -50008,11 +50008,11 @@
5000850008
],
5000950009
[
5001050010
"COMPARE_OP",
50011-
"assert list(gen()) == list(gen2) == [1, 2]"
50011+
""
5001250012
],
5001350013
[
5001450014
"COMPARE_OP",
50015-
"assert list(gen()) == list(gen2) == [1, 2]"
50015+
""
5001650016
],
5001750017
[
5001850018
"STORE_FAST",
@@ -51900,7 +51900,7 @@
5190051900
],
5190151901
[
5190251902
"CALL",
51903-
"assert isinstance(node, typ), (node, typ)"
51903+
""
5190451904
],
5190551905
[
5190651906
"LOAD_FAST",
@@ -52016,7 +52016,7 @@
5201652016
],
5201752017
[
5201852018
"CALL",
52019-
"assert result == value, (result, value)"
52019+
""
5202052020
],
5202152021
[
5202252022
"LOAD_FAST",
@@ -54970,7 +54970,7 @@
5497054970
],
5497154971
[
5497254972
"CALL",
54973-
"assert isinstance(result, ChangeValue), \"after_expr must return None or an instance of ChangeValue\""
54973+
""
5497454974
],
5497554975
[
5497654976
"LOAD_FAST",
@@ -58044,7 +58044,7 @@
5804458044
],
5804558045
[
5804658046
"CALL",
58047-
"assert len(pair) == 2, \"Watch extra must return pair or None\""
58047+
""
5804858048
],
5804958049
[
5805058050
"LOAD_FAST",

tests/test_pytest.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import os
22
import sys
3+
import inspect
4+
import ast
35

46
from littleutils import SimpleNamespace
57

68
from executing.executing import is_ipython_cell_code
79

10+
from executing import Source,NotOneValueFound
11+
812
sys.path.append(os.path.dirname(os.path.dirname(__file__)))
913

1014

@@ -23,6 +27,18 @@ def test_pytest():
2327
assert x is tester
2428

2529

30+
def this_expression():
31+
try:
32+
return Source.executing(inspect.currentframe().f_back).node
33+
except NotOneValueFound:
34+
# TODO: python < 3.11 raises an exception. Should we do the same for the PositionNodeFinder instead of returning None?
35+
return None
36+
37+
38+
def test_assert():
39+
assert isinstance(this_expression(),(ast.Call, type(None)))
40+
41+
2642
def test_ipython_cell_code():
2743
assert is_ipython_cell_code(
2844
SimpleNamespace(

0 commit comments

Comments
 (0)