Fix comment confusion bug in update_lib (#7802)

* Use `unittest.expectedSuccess` to mark failing tests

* Fix comment find logic

* Add test for it
This commit is contained in:
Shahar Naveh authored and GitHub committed 2026-05-10 00:27:51 +09:00
1 parent 5724485682
commit a35079967a
3 files changed
+44 -10

No files matched your search

+16 -6
View File
@@ -190,13 +190,23 @@ class PatchEntry(typing.NamedTuple):
if ut_method.has_cond():
cond = ast.unparse(dec_node.args[0])
else:
# Search first on decorator line, then in the line before
for line in lines[dec_node.lineno - 1 : dec_node.lineno - 3 : -1]:
if found := re.search(rf"{COMMENT}.?(.*)", line):
reason = found.group()
break
pattern = re.compile(rf"{COMMENT}.?(.*)")
dec_lineno = dec_node.lineno
curr_line = lines[dec_lineno - 1]
prev_line = lines[dec_lineno - 2]
# If we see our comment at the decorator line, take it
if found := pattern.search(curr_line):
reason = found.group()
elif prev_line.strip().startswith("#") and (
found := pattern.search(prev_line)
):
# Search the previous line of the decorator,
# only take the comment if the line starts with a `#`
reason = found.group()
else:
# Didn't find our `COMMENT` :)
# Didn't find our `COMMENT`, so the patch isn't ours :)
continue
reason = reason.removeprefix(COMMENT).strip(";:, ")
@@ -149,6 +149,30 @@ class TestFoo(unittest.TestCase):
specs = patches["TestFoo"]["test_one"]
self.assertEqual(len(specs), 2)
def test_comment_confusion(self):
"""
Test that we only extract our patches when CPython set one of the UT methods,
that we search for
"""
code = f"""
class TestFoo(unittest.TestCase):
@unittest.expectedSuccess # {COMMENT}; reason
@unittest.expectedFailure
def test_one(self):
pass
"""
patches = extract_patches(code)
specs = patches["TestFoo"]["test_one"]
self.assertEqual(len(specs), 1)
spec = specs[0]
self.assertEqual(
spec,
PatchSpec(ut_method=UtMethod.ExpectedSuccess, cond=None, reason="reason"),
)
class TestApplyPatches(unittest.TestCase):
"""Tests for apply_patches function."""