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>
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)
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))
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()
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)
from datetime import timedelta
from functools import partial
from threading import Thread
+from typing import Optional
from unittest import TestCase
import gpiod
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)
import time
from dataclasses import FrozenInstanceError
from functools import partial
+from typing import Optional
from unittest import TestCase
import gpiod
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:
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)
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)
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)