Fix ruff PLW2901 warnings (for-loop variable overwritten) in python/mozbuild
Categories
(Developer Infrastructure :: Lint and Formatting, task)
Tracking
(firefox158 fixed)
| Tracking | Status | |
|---|---|---|
| firefox158 | --- | fixed |
People
(Reporter: Sylvestre, Assigned: chegedan699, Mentored)
References
(Blocks 1 open bug)
Details
(Keywords: good-first-bug, Whiteboard: [lang=python])
Attachments
(1 external file)
Filing as a good first bug to learn workflows.
ruff reports 3 PLW2901 warnings ("for loop variable overwritten by assignment target") in python/mozbuild. In each case the loop body immediately reassigns the loop variable to a normalized form of itself, which makes the original value unrecoverable and confuses readers about what the variable holds at any point.
The fix is to assign to a new, clearly named local instead of clobbering the loop variable, then use that new name in the rest of the loop body.
The three sites:
-
python/mozbuild/mozbuild/backend/common.py#493 --
for s in source_filenames:thens = mozpath.relpath(s, output_directory)
https://searchfox.org/firefox-main/rev/211abcf08683d4089ebc30897460213f560ed59f/python/mozbuild/mozbuild/backend/common.py#493 -
python/mozbuild/mozbuild/frontend/emitter.py#2012 --
for mpath in mpmanifest.manifests():thenmpath = mozpath.normpath(mpath)
https://searchfox.org/firefox-main/rev/211abcf08683d4089ebc30897460213f560ed59f/python/mozbuild/mozbuild/frontend/emitter.py#2012 -
python/mozbuild/mozbuild/vendor/vendor_manifest.py#520 --
for file in self.convert_patterns_to_paths(...):thenfile = mozpath.normsep(file)
https://searchfox.org/firefox-main/rev/211abcf08683d4089ebc30897460213f560ed59f/python/mozbuild/mozbuild/vendor/vendor_manifest.py#520
In the third one, the loop variable is also named file, which shadows the file builtin. Renaming it to something like path fixes that at the same time.
Take care in each case to update every later use of the variable inside the loop body, not just the assignment. In the vendor_manifest.py case in particular, the normalized value is what the if ... not in to_keep test and the mozfile.remove call must keep using, so the behaviour stays the same.
To verify the fix:
./mach lint -W -l ruff python/mozbuild/mozbuild/backend/common.py python/mozbuild/mozbuild/frontend/emitter.py python/mozbuild/mozbuild/vendor/vendor_manifest.py
./mach python-test python/mozbuild/mozbuild/test
The three PLW2901 warnings should be gone and the tests should still pass. Note that these files also carry UP031 warnings, which are being handled in separate bugs -- please leave those alone here so the patches do not conflict.
Tutorial to contribute:
https://firefox-source-docs.mozilla.org/contributing/contribution_quickref.html
https://firefox-source-docs.mozilla.org/contributing/stack_quickref.html
Please don't ask for the bug to be assigned. It will be automatically assigned to the first patch.
| Assignee | ||
Comment 1•1 month ago
|
||
Updated•1 month ago
|
Comment 3•1 month ago
|
||
| bugherder | ||
Description
•