Skip to content

Commit

Permalink
fix: do not return Assert nodes, because they are caused by pytest as…
Browse files Browse the repository at this point in the history
…sert 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
  • Loading branch information
carterjsmarter committed Aug 12, 2022
1 parent 54f026d commit 4fbbe45
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 25 deletions.
8 changes: 8 additions & 0 deletions executing/executing.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
50 changes: 25 additions & 25 deletions tests/sample_results/3.11.json
Original file line number Diff line number Diff line change
Expand Up @@ -18148,19 +18148,19 @@
],
[
"COMPARE_OP",
"assert 1 <= month <= 12, month"
""
],
[
"COMPARE_OP",
"assert 1 <= month <= 12, month"
""
],
[
"LOAD_FAST",
"month"
],
[
"CALL",
"assert 1 <= month <= 12, month"
""
],
[
"LOAD_FAST",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -18288,15 +18288,15 @@
],
[
"COMPARE_OP",
"assert 1 <= day <= dim, ('day must be in 1..%d' % dim)"
""
],
[
"LOAD_FAST",
"dim"
],
[
"COMPARE_OP",
"assert 1 <= day <= dim, ('day must be in 1..%d' % dim)"
""
],
[
"LOAD_FAST",
Expand All @@ -18308,7 +18308,7 @@
],
[
"CALL",
"assert 1 <= day <= dim, ('day must be in 1..%d' % dim)"
""
],
[
"LOAD_GLOBAL",
Expand Down Expand Up @@ -18724,7 +18724,7 @@
],
[
"COMPARE_OP",
"assert 0 <= n < _days_in_month(year, month)"
""
],
[
"LOAD_GLOBAL",
Expand All @@ -18744,7 +18744,7 @@
],
[
"COMPARE_OP",
"assert 0 <= n < _days_in_month(year, month)"
""
],
[
"LOAD_FAST",
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -29076,7 +29076,7 @@
],
[
"CALL",
"assert not m % timedelta(minutes=1), \"whole minute\""
""
],
[
"LOAD_FAST",
Expand Down Expand Up @@ -50008,11 +50008,11 @@
],
[
"COMPARE_OP",
"assert list(gen()) == list(gen2) == [1, 2]"
""
],
[
"COMPARE_OP",
"assert list(gen()) == list(gen2) == [1, 2]"
""
],
[
"STORE_FAST",
Expand Down Expand Up @@ -51900,7 +51900,7 @@
],
[
"CALL",
"assert isinstance(node, typ), (node, typ)"
""
],
[
"LOAD_FAST",
Expand Down Expand Up @@ -52016,7 +52016,7 @@
],
[
"CALL",
"assert result == value, (result, value)"
""
],
[
"LOAD_FAST",
Expand Down Expand Up @@ -54970,7 +54970,7 @@
],
[
"CALL",
"assert isinstance(result, ChangeValue), \"after_expr must return None or an instance of ChangeValue\""
""
],
[
"LOAD_FAST",
Expand Down Expand Up @@ -58044,7 +58044,7 @@
],
[
"CALL",
"assert len(pair) == 2, \"Watch extra must return pair or None\""
""
],
[
"LOAD_FAST",
Expand Down
16 changes: 16 additions & 0 deletions tests/test_pytest.py
Original file line number Diff line number Diff line change
@@ -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__)))


Expand All @@ -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(
Expand Down

0 comments on commit 4fbbe45

Please sign in to comment.