Open Bug 2072564 Opened 2 months ago

Fix ruff UP031 warnings in python/mozbuild/mozbuild/vendor/vendor_manifest.py

Categories

(Developer Infrastructure :: Lint and Formatting, task)

task

Tracking

(Not tracked)

People

(Reporter: Sylvestre, Unassigned, Mentored)

References

(Blocks 1 open bug)

Details

(Keywords: good-first-bug, Whiteboard: [lang=python])

Filing as a good first bug to learn workflows.

ruff reports 6 UP031 warnings ("Use format specifiers instead of percent format") in python/mozbuild/mozbuild/vendor/vendor_manifest.py. The task is to convert the old-style percent formatting in that file to f-strings, keeping every resulting message byte-for-byte identical.

Warning lines, at the revision linked below:
91, 98, 213, 679, 680, 778

Link to the code:
https://searchfox.org/firefox-main/rev/211abcf08683d4089ebc30897460213f560ed59f/python/mozbuild/mozbuild/vendor/vendor_manifest.py#91

To verify the fix:

./mach lint -W -l ruff python/mozbuild/mozbuild/vendor/vendor_manifest.py
./mach python-test python/mozbuild/mozbuild/test

The UP031 warnings should be gone and the tests should still pass.

A note on tooling: ruff classifies the UP031 autofix as unsafe, so ./mach lint --fix will not apply it -- it reports "0 fixed". If you have ruff installed you can let it do the mechanical work with ruff check --select UP031 --fix --unsafe-fixes python/mozbuild/mozbuild/vendor/vendor_manifest.py, but you must then review every hunk by hand. Converting them manually is equally fine.

Things to watch for while converting:

  • "%s" % x where x is a tuple: the percent form unpacks it, an f-string does not.
  • %r becomes !r, as in f"{x!r}".
  • %d, %f and friends with a width or precision become format specs, as in f"{x:5.2f}".
  • A literal %% becomes a single %.
  • Braces already present in the string literal need no escaping in an f-string, but do if you switch to .format(). Prefer f-strings.
  • Keep the line under 88 characters so ./mach lint -l black stays happy; split the string across lines if needed.

Why: percent formatting is the oldest of Python's three string formatting styles. f-strings are faster, evaluate in place, and are what the rest of the tree is moving to, so converting these keeps mozbuild consistent and lets us eventually turn UP031 into an error.

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.

You need to log in before you can comment on or make changes to this bug.