Skip to content

Commit 002fc11

Browse files
authored
Fix comment confusion bug in update_lib (RustPython#7802)
* Use `unittest.expectedSuccess` to mark failing tests * Fix comment find logic * Add test for it
1 parent ae5c911 commit 002fc11

3 files changed

Lines changed: 44 additions & 10 deletions

File tree

Lib/test/test_descr.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1860,8 +1860,8 @@ class D(C):
18601860
self.assertEqual(b.foo, 3)
18611861
self.assertEqual(b.__class__, D)
18621862

1863-
# TODO: RUSTPYTHON; The `expectedFailure` here is from CPython, so this test must fail
1864-
# @unittest.expectedFailure
1863+
@unittest.expectedSuccess # TODO: RUSTPYTHON; The `expectedFailure` here is from CPython, so this test must fail
1864+
@unittest.expectedFailure
18651865
def test_bad_new(self):
18661866
self.assertRaises(TypeError, object.__new__)
18671867
self.assertRaises(TypeError, object.__new__, '')
@@ -1908,8 +1908,8 @@ def __init__(self, foo):
19081908
object.__init__(A(3))
19091909
self.assertRaises(TypeError, object.__init__, A(3), 5)
19101910

1911-
# TODO: RUSTPYTHON; The `expectedFailure` here is from CPython, so this test must fail
1912-
# @unittest.expectedFailure
1911+
@unittest.expectedSuccess # TODO: RUSTPYTHON; The `expectedFailure` here is from CPython, so this test must fail
1912+
@unittest.expectedFailure
19131913
def test_restored_object_new(self):
19141914
class A(object):
19151915
def __new__(cls, *args, **kwargs):

scripts/update_lib/patch_spec.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,23 @@ def iter_patch_entries(
190190
if ut_method.has_cond():
191191
cond = ast.unparse(dec_node.args[0])
192192
else:
193-
# Search first on decorator line, then in the line before
194-
for line in lines[dec_node.lineno - 1 : dec_node.lineno - 3 : -1]:
195-
if found := re.search(rf"{COMMENT}.?(.*)", line):
196-
reason = found.group()
197-
break
193+
pattern = re.compile(rf"{COMMENT}.?(.*)")
194+
dec_lineno = dec_node.lineno
195+
196+
curr_line = lines[dec_lineno - 1]
197+
prev_line = lines[dec_lineno - 2]
198+
199+
# If we see our comment at the decorator line, take it
200+
if found := pattern.search(curr_line):
201+
reason = found.group()
202+
elif prev_line.strip().startswith("#") and (
203+
found := pattern.search(prev_line)
204+
):
205+
# Search the previous line of the decorator,
206+
# only take the comment if the line starts with a `#`
207+
reason = found.group()
198208
else:
199-
# Didn't find our `COMMENT` :)
209+
# Didn't find our `COMMENT`, so the patch isn't ours :)
200210
continue
201211

202212
reason = reason.removeprefix(COMMENT).strip(";:, ")

scripts/update_lib/tests/test_patch_spec.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,30 @@ def test_one(self):
149149
specs = patches["TestFoo"]["test_one"]
150150
self.assertEqual(len(specs), 2)
151151

152+
def test_comment_confusion(self):
153+
"""
154+
Test that we only extract our patches when CPython set one of the UT methods,
155+
that we search for
156+
"""
157+
158+
code = f"""
159+
class TestFoo(unittest.TestCase):
160+
@unittest.expectedSuccess # {COMMENT}; reason
161+
@unittest.expectedFailure
162+
def test_one(self):
163+
pass
164+
"""
165+
166+
patches = extract_patches(code)
167+
specs = patches["TestFoo"]["test_one"]
168+
self.assertEqual(len(specs), 1)
169+
170+
spec = specs[0]
171+
self.assertEqual(
172+
spec,
173+
PatchSpec(ut_method=UtMethod.ExpectedSuccess, cond=None, reason="reason"),
174+
)
175+
152176

153177
class TestApplyPatches(unittest.TestCase):
154178
"""Tests for apply_patches function."""

0 commit comments

Comments
 (0)