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@xxxxxxxxxxx> --- bindings/python/gpiod/chip.py | 10 +++------- bindings/python/gpiod/chip_info.py | 4 +--- bindings/python/gpiod/edge_event.py | 2 +- bindings/python/gpiod/info_event.py | 4 +--- bindings/python/gpiod/line_info.py | 2 +- bindings/python/gpiod/line_request.py | 4 +--- bindings/python/gpiod/line_settings.py | 4 ++-- 7 files changed, 10 insertions(+), 20 deletions(-) diff --git a/bindings/python/gpiod/chip.py b/bindings/python/gpiod/chip.py index 482b98b..30201b5 100644 --- a/bindings/python/gpiod/chip.py +++ b/bindings/python/gpiod/chip.py @@ -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: diff --git a/bindings/python/gpiod/chip_info.py b/bindings/python/gpiod/chip_info.py index eb585d6..3306af2 100644 --- a/bindings/python/gpiod/chip_info.py +++ b/bindings/python/gpiod/chip_info.py @@ -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}>' diff --git a/bindings/python/gpiod/edge_event.py b/bindings/python/gpiod/edge_event.py index 0d401d8..7f5cd4d 100644 --- a/bindings/python/gpiod/edge_event.py +++ b/bindings/python/gpiod/edge_event.py @@ -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, diff --git a/bindings/python/gpiod/info_event.py b/bindings/python/gpiod/info_event.py index d9e9564..4a86697 100644 --- a/bindings/python/gpiod/info_event.py +++ b/bindings/python/gpiod/info_event.py @@ -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}>" diff --git a/bindings/python/gpiod/line_info.py b/bindings/python/gpiod/line_info.py index 5ea9568..1aca142 100644 --- a/bindings/python/gpiod/line_info.py +++ b/bindings/python/gpiod/line_info.py @@ -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, diff --git a/bindings/python/gpiod/line_request.py b/bindings/python/gpiod/line_request.py index 7327274..9471442 100644 --- a/bindings/python/gpiod/line_request.py +++ b/bindings/python/gpiod/line_request.py @@ -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: diff --git a/bindings/python/gpiod/line_settings.py b/bindings/python/gpiod/line_settings.py index 6c6518d..2aca71c 100644 --- a/bindings/python/gpiod/line_settings.py +++ b/bindings/python/gpiod/line_settings.py @@ -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, -- 2.34.1