Summary
PLR1733 is giving a false positive for dictionary access on else clause of for.
Example:
import fnmatch
def clean_globs_with_priorities (globs_priorities: list[tuple[str, float]]) -> dict[str, float]:
result: dict[str, float] = {}
for glob, priority in globs_priorities:
for res_glob, res_priority in result.items():
# test if there is a glob that matches the current glob and
# has a higher or equal priority
if fnmatch.fnmatch(glob, res_glob) and (res_priority >= priority):
break
else:
# no glob matches, proceed to remove all globs that match the current glob and
# have a lower or equal priority
for res_glob in fnmatch.filter(result.keys(), glob):
if result[res_glob] <= priority:
result.pop(res_glob)
result[glob] = priority
return result
When running ruff check --preview --select "PLR1733" <file>.py it outputs:
error[PLR1733][*]: Unnecessary lookup of dictionary value by key
--> <file>.py:18:20
|
16 | # have a lower or equal priority
17 | for res_glob in fnmatch.filter(result.keys(), glob):
18 | if result[res_glob] <= priority:
| ^^^^^^^^^^^^^^^^
19 | result.pop(res_glob)
|
help: Use existing variable
15 | # no glob matches, proceed to remove all globs that match the current glob and
16 | # have a lower or equal priority
17 | for res_glob in fnmatch.filter(result.keys(), glob):
- if result[res_glob] <= priority:
18 + if res_priority <= priority:
19 | result.pop(res_glob)
20 |
21 | result[glob] = priority
Found 1 error.
[*] 1 fixable with the `--fix` option.
The --fix option creates a possibly unbound reference to res_priority at the else clause.
Version
ruff 0.15.12
Summary
PLR1733 is giving a false positive for dictionary access on
elseclause offor.Example:
When running
ruff check --preview --select "PLR1733" <file>.pyit outputs:The
--fixoption creates a possibly unbound reference tores_priorityat theelseclause.Version
ruff 0.15.12