Fix the weakened assertion finder for test edits

Review the AI's codeEasy10 minAnthropic style

Your task

A test failed after a feature change, and an agent was told to "make the test pass". It did, by loosening the assertions: status_code == 200 became status_code in (200, 201, 202), and total == 42 became total is not None. Your team now runs a check over every test edit that reports assertions made weaker.

An assistant wrote weakened_assertions(before, after). It is in the editor. It has a bug. Find it and fix it.

Input and output:

  • before: the assertion lines before the edit.
  • after: the assertion lines after the edit, at the same positions.
  • Return the positions in before whose assertion got weaker, in increasing order.

Rules the tests check:

  • Score each line by how much it pins down. Look at the line with whitespace trimmed from both ends, in lowercase. The first rule that matches gives the score:
    1. The line is empty: 0. The check is gone.
    2. It contains is not none: 1.
    3. It contains in ( or in [, with the space before in: 2. A set of accepted values.
    4. It contains ==: 3.
    5. Anything else: 1.
  • Compare before[i] with after[i]. A position missing from after counts as an empty line. Extra lines in after are ignored.
  • Report position i when the score of after[i] is lower than the score of before[i]. A new expected value with the same score, like 42 becoming 50, is not reported.

Examples

weakened_assertions(before: list[str], after: list[str]) -> list[int]
Example 1
Inputweakened_assertions(["assert response.status_code == 200", "assert response.json()[\"total\"] == 42"], ["assert response.status_code in (200, 201, 202)", "assert response.json().get(\"total\") is not None"])
Output[0, 1]
Example 2
Inputweakened_assertions(["assert total == 42"], ["assert total == 50"])
Output[]
Example 3
Inputweakened_assertions(["assert a == 1", "assert b == 2"], ["assert a == 1"])
Output[1]