# ruff PLR1733 on repo: fix changes the outcome of the project's command

Evidence sheet generated by fixhunt from a repository scan. Not an issue text. A human re-verifies, checks the
duplicates below, and writes the upstream report in their own words following the project's AI policy.

- Tool: ruff 0.17.1 (fix labelled safe)
- Project: /home/sun/dev/diaghunt/demo/repo
- Command: `cd python && python3 -m unittest discover -s tests -t .` (exit 0 before, 1 after)
- Files changed by the fix: 1; reduced to 1 of 1 hunk(s) in python/toolbox/registry.py (delta debugging complete; the patch below was applied with git apply and reproduced the same difference)
- Sites reported by the rule in 1 changed file(s): 1 before, 0 after the fix
- Judge (Nemotron 3 Ultra, advisory): real, severity high. The fix incorrectly assumes the inner loop variable 'key' iterates over the outer dict's keys, but it actually iterates over 'value' (which could be a list or other iterable). Replacing 'table[key]' with 'value' substitutes the entire iterable for the specific looked-up element, changing the program's logic and causing a test failure.

## Change (reduced; applies to the project with `git apply -p1`)

```diff
--- a/python/toolbox/registry.py
+++ b/python/toolbox/registry.py
@@ -27,5 +27,5 @@
     out = {}
     for key, value in table.items():
         for key in value:
-            out[key] = table[key] if key in table else None
+            out[key] = value if key in table else None
     return out
```

## Observed

```
exit 0 → 1
stderr last line before: OK
stderr last line after: FAILED (failures=1)
```

## Output of the command (last 40 lines)

Before:

```
(no stdout)
stderr:
..........
----------------------------------------------------------------------
Ran 10 tests in 0.007s

OK
```

After:

```
(no stdout)
stderr:
  File "/w/python/tests/test_registry.py", line 15, in test_resolve_listed_names
    self.assertEqual(resolve(table), {"b": [], "c": None})
AssertionError: {'b': ['b', 'c'], 'c': None} != {'b': [], 'c': None}
- {'b': ['b', 'c'], 'c': None}
?        --------

+ {'b': [], 'c': None}

----------------------------------------------------------------------
Ran 10 tests in 0.007s

FAILED (failures=1)
```

## Why the rewrite is not equivalent (model draft, verify before use)

The original inner loop uses `key` as both the outer variable from `table.items()` and the iterator over `value`, shadowing it; thus `table[key]` refers to the current row’s dictionary entry under that iterated element. After rewriting, `value` replaces `table[key]`, but `value` is the entire list/dict of keys being iterated—so each assignment stores the whole collection instead of individual elements, altering output structure and causing test failures.

## Upstream search

Queries: "PLR1733 fix", "PLR1733 nested loop variable shadowing", "PLR1733 dictionary lookup redundancy", "PLR1733 inner loop overwrites outer key"

- related: https://github.com/astral-sh/ruff/issues/25182
- related: https://github.com/astral-sh/ruff/issues/29422
- related: https://github.com/astral-sh/ruff/issues/25150

## Reproduce

```sh
# /home/sun/dev/diaghunt/demo/repo: apply the change above (minimal.patch), then run the command
git apply -p1 minimal.patch
cd python && python3 -m unittest discover -s tests -t .
# or run the fixer itself on the changed files
ruff check --preview --isolated --select PLR1733 --fix 'python/toolbox/registry.py'
```
