bindings: python: tests: add type hints to internal members
authorVincent Fazio <vfazio@xes-inc.com>
Thu, 14 Nov 2024 14:51:13 +0000 (08:51 -0600)
committerBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Tue, 19 Nov 2024 14:23:58 +0000 (15:23 +0100)
Add type hints for internal members of the test cases so type checkers
can ensure the code properly constrains to these types and accounts for
scenarios where the values are `None`.

In most cases, the type checker is allowed to assume the inferred type
upon assignment.

An example is any test case that creates a `Chip` as part of its `setUp`
method. Many of these tests subsequently set the reference to `None` in
`tearDown` to free up resources to be collected by the GC.

If the member was "properly" typed to be `Optional`, all references in
the unit tests would need to be guarded by an assert or a cast or
ignored by the type checker. This is noisy and doesn't add value since
for the life of the test it should always be a valid reference.

Instead, allow `tearDown` to violate the inferred type and inform the
type checker to ignore the assignment via directive.

If the tests are ever changed to set the member to something other than
the inferred type outside of `tearDown`, explicit type hints and proper
checks should be added.

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

index 9c8f87579469e684ed3b6a5dbcef35e0856127ba..218f238c699c6bc5b76305324f3ccd0116fb7b15 100644 (file)
@@ -75,7 +75,7 @@ class ChipProperties(TestCase):
 
     def tearDown(self) -> None:
         self.chip.close()
-        self.sim = None
+        self.sim = None  # type: ignore[assignment]
 
     def test_get_chip_path(self) -> None:
         self.assertEqual(self.sim.dev_path, self.chip.path)
@@ -200,7 +200,7 @@ class StringRepresentation(TestCase):
 
     def tearDown(self) -> None:
         self.chip.close()
-        self.sim = None
+        self.sim = None  # type: ignore[assignment]
 
     def test_repr(self) -> None:
         self.assertEqual(repr(self.chip), 'gpiod.Chip("{}")'.format(self.sim.dev_path))
@@ -224,7 +224,7 @@ class StringRepresentationClosed(TestCase):
         self.chip = gpiod.Chip(self.sim.dev_path)
 
     def tearDown(self) -> None:
-        self.sim = None
+        self.sim = None  # type: ignore[assignment]
 
     def test_repr_closed(self) -> None:
         self.chip.close()
index acb0da9d1f302186a2a6bca3e2f5a46abd3ebc51..aabfbeeb41de5c1303ddb8db452aca6cd4ef00f9 100644 (file)
@@ -15,10 +15,10 @@ class ChipInfoProperties(TestCase):
         self.info = self.chip.get_info()
 
     def tearDown(self) -> None:
-        self.info = None
+        self.info = None  # type: ignore[assignment]
         self.chip.close()
-        self.chip = None
-        self.sim = None
+        self.chip = None  # type: ignore[assignment]
+        self.sim = None  # type: ignore[assignment]
 
     def test_chip_info_name(self) -> None:
         self.assertEqual(self.info.name, self.sim.name)
index f80d6a52680c48405b935fca8e47e2a2817a2fac..d7766ec6c470b0453a3e9c729ebff72ffcdf05cf 100644 (file)
@@ -5,6 +5,7 @@ import time
 from datetime import timedelta
 from functools import partial
 from threading import Thread
+from typing import Optional
 from unittest import TestCase
 
 import gpiod
@@ -54,13 +55,13 @@ class EdgeEventInvalidConfig(TestCase):
 class WaitingForEdgeEvents(TestCase):
     def setUp(self) -> None:
         self.sim = gpiosim.Chip(num_lines=8)
-        self.thread = None
+        self.thread: Optional[Thread] = None
 
     def tearDown(self) -> None:
         if self.thread:
             self.thread.join()
             del self.thread
-        self.sim = None
+        self.sim = None  # type: ignore[assignment]
 
     def trigger_falling_and_rising_edge(self, offset: int) -> None:
         time.sleep(0.05)
index 7e12b8e33f1a425423a1b2f30dd5af9a331906e9..100564702e96336b2ea6641ce3de29c1f02e3c00 100644 (file)
@@ -7,6 +7,7 @@ import threading
 import time
 from dataclasses import FrozenInstanceError
 from functools import partial
+from typing import Optional
 from unittest import TestCase
 
 import gpiod
@@ -51,7 +52,7 @@ class WatchingInfoEventWorks(TestCase):
     def setUp(self) -> None:
         self.sim = gpiosim.Chip(num_lines=8, line_names={4: "foobar"})
         self.chip = gpiod.Chip(self.sim.dev_path)
-        self.thread = None
+        self.thread: Optional[threading.Thread] = None
 
     def tearDown(self) -> None:
         if self.thread:
@@ -59,8 +60,8 @@ class WatchingInfoEventWorks(TestCase):
             self.thread = None
 
         self.chip.close()
-        self.chip = None
-        self.sim = None
+        self.chip = None  # type: ignore[assignment]
+        self.sim = None  # type: ignore[assignment]
 
     def test_watch_line_info_returns_line_info(self) -> None:
         info = self.chip.watch_line_info(7)
@@ -138,8 +139,8 @@ class UnwatchingLineInfo(TestCase):
 
     def tearDown(self) -> None:
         self.chip.close()
-        self.chip = None
-        self.sim = None
+        self.chip = None  # type: ignore[assignment]
+        self.sim = None  # type: ignore[assignment]
 
     def test_unwatch_line_info(self) -> None:
         self.chip.watch_line_info(0)
index 9828349810eed0e3fa755f8557ec314d3e86ed7a..7bc244dfea2c58db15ac5a80b021d9b9d1af618c 100644 (file)
@@ -22,8 +22,8 @@ class GetLineInfo(TestCase):
 
     def tearDown(self) -> None:
         self.chip.close()
-        self.chip = None
-        self.sim = None
+        self.chip = None  # type: ignore[assignment]
+        self.sim = None  # type: ignore[assignment]
 
     def test_get_line_info_by_offset(self) -> None:
         self.chip.get_line_info(0)