bindings: python: selectively use f-strings
authorVincent Fazio <vfazio@xes-inc.com>
Thu, 14 Nov 2024 14:51:07 +0000 (08:51 -0600)
committerBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Tue, 19 Nov 2024 14:21:01 +0000 (15:21 +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-15-vfazio@xes-inc.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
bindings/python/gpiod/chip.py
bindings/python/gpiod/chip_info.py
bindings/python/gpiod/edge_event.py
bindings/python/gpiod/info_event.py
bindings/python/gpiod/line_info.py
bindings/python/gpiod/line_request.py
bindings/python/gpiod/line_settings.py

index 482b98b8c9df2a9c597d50977f176d31ea7642e0..30201b52ac3648fd60d9e87fcc7a4f311efdaa6b 100644 (file)
@@ -282,9 +282,7 @@ class Chip:
         ).items():
             if count != 1:
                 raise ValueError(
-                    "line must be configured exactly once - offset {} repeats".format(
-                        offset
-                    )
+                    f"line must be configured exactly once - offset {offset} repeats"
                 )
 
         # If we have global output values - map line names to offsets
@@ -353,7 +351,7 @@ class Chip:
         if not self._chip:
             return "<Chip CLOSED>"
 
-        return 'gpiod.Chip("{}")'.format(self.path)
+        return f'gpiod.Chip("{self.path}")'
 
     def __str__(self) -> str:
         """
@@ -362,9 +360,7 @@ class Chip:
         if not self._chip:
             return "<Chip CLOSED>"
 
-        return '<Chip path="{}" fd={} info={}>'.format(
-            self.path, self.fd, self.get_info()
-        )
+        return f'<Chip path="{self.path}" fd={self.fd} info={self.get_info()}>'
 
     @property
     def path(self) -> str:
index eb585d6d7efa26492163dc0a731ba263d0232099..3306af222260938e3e97c48427e7e8775787e457 100644 (file)
@@ -18,6 +18,4 @@ class ChipInfo:
     num_lines: int
 
     def __str__(self) -> str:
-        return '<ChipInfo name="{}" label="{}" num_lines={}>'.format(
-            self.name, self.label, self.num_lines
-        )
+        return f'<ChipInfo name="{self.name}" label="{self.label}" num_lines={self.num_lines}>'
index 0d401d896a79dc5204e7ea86f202e37acbd24758..7f5cd4d345785ceb549e2888629898818a4ab293 100644 (file)
@@ -40,7 +40,7 @@ class EdgeEvent:
         object.__setattr__(self, "line_seqno", line_seqno)
 
     def __str__(self) -> str:
-        return "<EdgeEvent type={} timestamp_ns={} line_offset={} global_seqno={} line_seqno={}>".format(
+        return "<EdgeEvent type={} timestamp_ns={} line_offset={} global_seqno={} line_seqno={}>".format(  # noqa: UP032
             self.event_type,
             self.timestamp_ns,
             self.line_offset,
index d9e9564e21338cd8d1e28c567fa2ed2ac29ceb00..4a866977cfe3cb6648bd105ee03767d74df1ad6e 100644 (file)
@@ -31,6 +31,4 @@ class InfoEvent:
         object.__setattr__(self, "line_info", line_info)
 
     def __str__(self) -> str:
-        return "<InfoEvent type={} timestamp_ns={} line_info={}>".format(
-            self.event_type, self.timestamp_ns, self.line_info
-        )
+        return f"<InfoEvent type={self.event_type} timestamp_ns={self.timestamp_ns} line_info={self.line_info}>"
index 5ea9568fd5c963c77f53c56ef748ac1e80872ed9..1aca142387992d2d836fb2b5672f0b45249691af 100644 (file)
@@ -59,7 +59,7 @@ class LineInfo:
         )
 
     def __str__(self) -> str:
-        return '<LineInfo offset={} name="{}" used={} consumer="{}" direction={} active_low={} bias={} drive={} edge_detection={} event_clock={} debounced={} debounce_period={}>'.format(
+        return '<LineInfo offset={} name="{}" used={} consumer="{}" direction={} active_low={} bias={} drive={} edge_detection={} event_clock={} debounced={} debounce_period={}>'.format(  # noqa: UP032
             self.offset,
             self.name,
             self.used,
index 73272748345f225d2990232eb0480275871a3059..94714424e70938116c25d360d3419c0a8d8d2117 100644 (file)
@@ -231,9 +231,7 @@ class LineRequest:
         if not self._req:
             return "<LineRequest RELEASED>"
 
-        return '<LineRequest chip="{}" num_lines={} offsets={} fd={}>'.format(
-            self.chip_name, self.num_lines, self.offsets, self.fd
-        )
+        return f'<LineRequest chip="{self.chip_name}" num_lines={self.num_lines} offsets={self.offsets} fd={self.fd}>'
 
     @property
     def chip_name(self) -> str:
index 6c6518dbc958423393790a39d69e94802eda8547..2aca71c9c5cd02d1add663f5d430dcf0153706b9 100644 (file)
@@ -28,7 +28,7 @@ class LineSettings:
     # __repr__ generated by @dataclass uses repr for enum members resulting in
     # an unusable representation as those are of the form: <NAME: $value>
     def __repr__(self) -> str:
-        return "gpiod.LineSettings(direction=gpiod.line.{}, edge_detection=gpiod.line.{}, bias=gpiod.line.{}, drive=gpiod.line.{}, active_low={}, debounce_period={}, event_clock=gpiod.line.{}, output_value=gpiod.line.{})".format(
+        return "gpiod.LineSettings(direction=gpiod.line.{}, edge_detection=gpiod.line.{}, bias=gpiod.line.{}, drive=gpiod.line.{}, active_low={}, debounce_period={}, event_clock=gpiod.line.{}, output_value=gpiod.line.{})".format(  # noqa: UP032
             str(self.direction),
             str(self.edge_detection),
             str(self.bias),
@@ -40,7 +40,7 @@ class LineSettings:
         )
 
     def __str__(self) -> str:
-        return "<LineSettings direction={} edge_detection={} bias={} drive={} active_low={} debounce_period={} event_clock={} output_value={}>".format(
+        return "<LineSettings direction={} edge_detection={} bias={} drive={} active_low={} debounce_period={} event_clock={} output_value={}>".format(  # noqa: UP032
             self.direction,
             self.edge_detection,
             self.bias,