* [libgpiod][PATCH] bindings: python: fix __repr__() implementations
@ 2024-02-02 13:08 Bartosz Golaszewski
2024-02-05 9:18 ` Bartosz Golaszewski
0 siblings, 1 reply; 2+ messages in thread
From: Bartosz Golaszewski @ 2024-02-02 13:08 UTC (permalink / raw)
To: Linus Walleij, Kent Gibson; +Cc: linux-gpio, Bartosz Golaszewski, Robert Thomas
From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
The __repr__() function should - if possible - return a valid Python
expression that can be used to instantiate a new object when evaluated.
LineSettings.__repr__() is missing comas between arguments. Both Chip and
LineSettings also don't prefix the returned string with 'gpiod.'. Fix
both functions and add more test cases - including actually using the
strings returned by __repr__() to create new objects and compare their
contents.
Reported-by: Robert Thomas <rob.thomas@raspberrypi.com>
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
---
bindings/python/gpiod/chip.py | 2 +-
bindings/python/gpiod/line_settings.py | 2 +-
bindings/python/tests/tests_chip.py | 5 ++++-
bindings/python/tests/tests_line_settings.py | 9 ++++++---
4 files changed, 12 insertions(+), 6 deletions(-)
diff --git a/bindings/python/gpiod/chip.py b/bindings/python/gpiod/chip.py
index b3d8e61..19c62cd 100644
--- a/bindings/python/gpiod/chip.py
+++ b/bindings/python/gpiod/chip.py
@@ -333,7 +333,7 @@ class Chip:
if not self._chip:
return "<Chip CLOSED>"
- return 'Chip("{}")'.format(self.path)
+ return 'gpiod.Chip("{}")'.format(self.path)
def __str__(self) -> str:
"""
diff --git a/bindings/python/gpiod/line_settings.py b/bindings/python/gpiod/line_settings.py
index 458fd81..41712cc 100644
--- a/bindings/python/gpiod/line_settings.py
+++ b/bindings/python/gpiod/line_settings.py
@@ -27,7 +27,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):
- return "LineSettings(direction={}, edge_detection={} bias={} drive={} active_low={} debounce_period={} event_clock={} output_value={})".format(
+ return "gpiod.LineSettings(direction={}, edge_detection={}, bias={}, drive={}, active_low={}, debounce_period={}, event_clock={}, output_value={})".format(
str(self.direction),
str(self.edge_detection),
str(self.bias),
diff --git a/bindings/python/tests/tests_chip.py b/bindings/python/tests/tests_chip.py
index 8db4cdb..bd4ae34 100644
--- a/bindings/python/tests/tests_chip.py
+++ b/bindings/python/tests/tests_chip.py
@@ -202,7 +202,10 @@ class StringRepresentation(TestCase):
self.sim = None
def test_repr(self):
- self.assertEqual(repr(self.chip), 'Chip("{}")'.format(self.sim.dev_path))
+ self.assertEqual(repr(self.chip), 'gpiod.Chip("{}")'.format(self.sim.dev_path))
+
+ cmp = eval(repr(self.chip))
+ self.assertEqual(self.chip.path, cmp.path)
def test_str(self):
info = self.chip.get_info()
diff --git a/bindings/python/tests/tests_line_settings.py b/bindings/python/tests/tests_line_settings.py
index 36dda6d..c6ca720 100644
--- a/bindings/python/tests/tests_line_settings.py
+++ b/bindings/python/tests/tests_line_settings.py
@@ -1,10 +1,10 @@
# SPDX-License-Identifier: LGPL-2.1-or-later
# SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <brgl@bgdev.pl>
+import datetime
import gpiod
from . import gpiosim
-from datetime import timedelta
from gpiod.line import Direction, Edge, Bias, Drive, Value, Clock
from unittest import TestCase
@@ -47,7 +47,7 @@ class LineSettingsAttributes(TestCase):
settings.direction = Direction.INPUT
settings.edge_detection = Edge.BOTH
settings.bias = Bias.DISABLED
- settings.debounce_period = timedelta(microseconds=3000)
+ settings.debounce_period = datetime.timedelta(microseconds=3000)
settings.event_clock = Clock.HTE
self.assertEqual(settings.direction, Direction.INPUT)
@@ -69,9 +69,12 @@ class LineSettingsStringRepresentation(TestCase):
def test_repr(self):
self.assertEqual(
repr(self.settings),
- "LineSettings(direction=Direction.OUTPUT, edge_detection=Edge.NONE bias=Bias.AS_IS drive=Drive.OPEN_SOURCE active_low=True debounce_period=datetime.timedelta(0) event_clock=Clock.MONOTONIC output_value=Value.INACTIVE)",
+ "gpiod.LineSettings(direction=Direction.OUTPUT, edge_detection=Edge.NONE, bias=Bias.AS_IS, drive=Drive.OPEN_SOURCE, active_low=True, debounce_period=datetime.timedelta(0), event_clock=Clock.MONOTONIC, output_value=Value.INACTIVE)",
)
+ cmp = eval(repr(self.settings))
+ self.assertEqual(self.settings, cmp)
+
def test_str(self):
self.assertEqual(
str(self.settings),
--
2.40.1
^ permalink raw reply related [flat|nested] 2+ messages in thread* Re: [libgpiod][PATCH] bindings: python: fix __repr__() implementations
2024-02-02 13:08 [libgpiod][PATCH] bindings: python: fix __repr__() implementations Bartosz Golaszewski
@ 2024-02-05 9:18 ` Bartosz Golaszewski
0 siblings, 0 replies; 2+ messages in thread
From: Bartosz Golaszewski @ 2024-02-05 9:18 UTC (permalink / raw)
To: Linus Walleij, Kent Gibson; +Cc: linux-gpio, Bartosz Golaszewski, Robert Thomas
On Fri, Feb 2, 2024 at 2:08 PM Bartosz Golaszewski <brgl@bgdev.pl> wrote:
>
> From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
>
> The __repr__() function should - if possible - return a valid Python
> expression that can be used to instantiate a new object when evaluated.
>
> LineSettings.__repr__() is missing comas between arguments. Both Chip and
> LineSettings also don't prefix the returned string with 'gpiod.'. Fix
> both functions and add more test cases - including actually using the
> strings returned by __repr__() to create new objects and compare their
> contents.
>
> Reported-by: Robert Thomas <rob.thomas@raspberrypi.com>
> Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
> ---
Patch applied.
Bart
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-02-05 9:18 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-02-02 13:08 [libgpiod][PATCH] bindings: python: fix __repr__() implementations Bartosz Golaszewski
2024-02-05 9:18 ` Bartosz Golaszewski
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).