bindings: python: tests: selectively use f-strings
authorVincent Fazio <vfazio@xes-inc.com>
Thu, 14 Nov 2024 14:51:15 +0000 (08:51 -0600)
committerBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Tue, 19 Nov 2024 14:23:58 +0000 (15:23 +0100)
Since their inclusion in Python 3.6, f-strings have become the preferred
way to format strings with variable values as they are generally more
readable as the value substitution is in place and doesn't have to be
parsed from the list or arguments to `.format()`.

Where it does not impact readability (when the line is <120 characters),
swap usage of `.format()` to an f-string.

For lines that are not converted, inform the linter to ignore attempts
to upgrade those instances to f-strings [0]

[0]: https://docs.astral.sh/ruff/rules/f-string/

Signed-off-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20241114145116.2123714-23-vfazio@xes-inc.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
bindings/python/tests/__init__.py
bindings/python/tests/tests_chip.py
bindings/python/tests/tests_chip_info.py
bindings/python/tests/tests_line_request.py
bindings/python/tests/tests_module.py

index 2374e8155373efbd94d5c66dcfdffa7cc23be9f6..a0f22ae717271766b4175a775cc6ec19c9b24441 100644 (file)
@@ -9,7 +9,5 @@ current_version = LooseVersion(os.uname().release.split("-")[0])
 
 if current_version < required_kernel_version:
     raise NotImplementedError(
-        "linux kernel version must be at least {} - got {}".format(
-            required_kernel_version, current_version
-        )
+        f"linux kernel version must be at least {required_kernel_version} - got {current_version}"
     )
index 9b31e306e92aab3edd080917d0cab8e894dcf352..d5a64b36313f435154c20b7c42aeafe0e45b8d53 100644 (file)
@@ -25,7 +25,7 @@ class ChipConstructor(TestCase):
             pass
 
     def test_open_chip_by_link(self) -> None:
-        link = "/tmp/gpiod-py-test-link.{}".format(os.getpid())
+        link = f"/tmp/gpiod-py-test-link.{os.getpid()}"
         sim = gpiosim.Chip()
 
         with LinkGuard(sim.dev_path, link):
@@ -94,7 +94,7 @@ class ChipProperties(TestCase):
 class ChipDevPathFromLink(TestCase):
     def test_dev_path_open_by_link(self) -> None:
         sim = gpiosim.Chip()
-        link = "/tmp/gpiod-py-test-link.{}".format(os.getpid())
+        link = f"/tmp/gpiod-py-test-link.{os.getpid()}"
 
         with LinkGuard(sim.dev_path, link):
             with gpiod.Chip(link) as chip:
@@ -203,7 +203,7 @@ class StringRepresentation(TestCase):
         self.sim = None  # type: ignore[assignment]
 
     def test_repr(self) -> None:
-        self.assertEqual(repr(self.chip), 'gpiod.Chip("{}")'.format(self.sim.dev_path))
+        self.assertEqual(repr(self.chip), f'gpiod.Chip("{self.sim.dev_path}")')
 
         cmp = eval(repr(self.chip))
         self.assertEqual(self.chip.path, cmp.path)
@@ -212,7 +212,7 @@ class StringRepresentation(TestCase):
         info = self.chip.get_info()
         self.assertEqual(
             str(self.chip),
-            '<Chip path="{}" fd={} info=<ChipInfo name="{}" label="foobar" num_lines=4>>'.format(
+            '<Chip path="{}" fd={} info=<ChipInfo name="{}" label="foobar" num_lines=4>>'.format(  # noqa: UP032
                 self.sim.dev_path, self.chip.fd, info.name
             ),
         )
index fdceda9df949778e6b6a8b3e4c6af144801b7e5d..dbb7fd0f7f6a6ad87cb82fbd8cc3a9eab557c69f 100644 (file)
@@ -49,5 +49,5 @@ class ChipInfoStringRepresentation(TestCase):
 
             self.assertEqual(
                 str(info),
-                '<ChipInfo name="{}" label="foobar" num_lines=16>'.format(sim.name),
+                f'<ChipInfo name="{sim.name}" label="foobar" num_lines=16>',
             )
index bae8815b98654145c26071c4fc40816469313192..afee644558b5e734b50a6d90dd6a0a867f466eb7 100644 (file)
@@ -635,9 +635,7 @@ class LineRequestStringRepresentation(TestCase):
             with chip.request_lines(config={(2, 6, 4, 1): None}) as req:
                 self.assertEqual(
                     str(req),
-                    '<LineRequest chip="{}" num_lines=4 offsets=[2, 6, 4, 1] fd={}>'.format(
-                        self.sim.name, req.fd
-                    ),
+                    f'<LineRequest chip="{self.sim.name}" num_lines=4 offsets=[2, 6, 4, 1] fd={req.fd}>',
                 )
 
     def test_str_released(self) -> None:
index efd49db59e6567b9bc5ee0096ccce3281ac466f3..7120c6346ba4666adb250d0880d5777dbb7666ea 100644 (file)
@@ -36,14 +36,14 @@ class IsGPIOChip(TestCase):
         self.assertTrue(gpiod.is_gpiochip_device(path=sim.dev_path))
 
     def test_is_gpiochip_link_good(self) -> None:
-        link = "/tmp/gpiod-py-test-link.{}".format(os.getpid())
+        link = f"/tmp/gpiod-py-test-link.{os.getpid()}"
         sim = gpiosim.Chip()
 
         with LinkGuard(sim.dev_path, link):
             self.assertTrue(gpiod.is_gpiochip_device(link))
 
     def test_is_gpiochip_link_bad(self) -> None:
-        link = "/tmp/gpiod-py-test-link.{}".format(os.getpid())
+        link = f"/tmp/gpiod-py-test-link.{os.getpid()}"
 
         with LinkGuard("/dev/null", link):
             self.assertFalse(gpiod.is_gpiochip_device(link))