Skip to content

Commit

Permalink
fix: workaround for python/cpython#95921
Browse files Browse the repository at this point in the history
  • Loading branch information
carterjsmarter committed Aug 28, 2022
1 parent 9188b29 commit 3f4e2c4
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 56 deletions.
34 changes: 34 additions & 0 deletions executing/executing.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,17 @@ class PositionNodeFinder(object):
There are only some exceptions for methods and attributes.
"""

types_cmp_issue = (
ast.IfExp,
ast.If,
ast.Assert,
ast.While,
ast.ListComp,
ast.SetComp,
ast.DictComp,
ast.GeneratorExp,
)

last_code = None
last_instructions = None

Expand Down Expand Up @@ -597,6 +608,29 @@ def __init__(self, frame, stmts, tree, lasti, source):
else:
raise

if isinstance(node, self.types_cmp_issue) and self.opname(lasti) in (
"COMPARE_OP",
"IS_OP",
"CONTAINS_OP",
):
# this is a workaround for https://github.com/python/cpython/issues/95921
# we can fix cases with only on comparison inside the test condition
#
# we can not fix cases like:
# if a<b<c and d<e<f: pass
# if (a<b<c)!=d!=e: pass
# because we don't know which comparison caused the problem

comparisons = [
n
for n in ast.walk(node.test)
if isinstance(n, ast.Compare) and len(n.ops) > 1
]

assert_(comparisons, "expected at least one comparison")

if len(comparisons) == 1:
node = comparisons[0]

if isinstance(node, ast.Assert):
# pytest assigns the position of the assertion to all expressions of the rewritten assertion.
Expand Down
Loading

0 comments on commit 3f4e2c4

Please sign in to comment.