bindings: python: fix LineRequest union-attr type errors
authorVincent Fazio <vfazio@xes-inc.com>
Thu, 14 Nov 2024 14:51:04 +0000 (08:51 -0600)
committerBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Tue, 19 Nov 2024 14:21:01 +0000 (15:21 +0100)
commitfcb520f9fe5cc6e550646daee224c6e3d0c3bfb6
tree9bbdf6a889c5680a5826e395f748ea47c18231bf
parent01efd10160bfcf09566992522aa70319241d3db7
bindings: python: fix LineRequest union-attr type errors

Since `LineRequest._req` is typed to be `Optional` and because type
checkers may not be able to infer that an object is not `None` from an
earlier call (such as `_check_released`) it is necessary to inform type
checkers of the state of the object to silence union-attr [0] errors.

Instead of littering the code with "# type: ignore" comments, use casts
to inform type checkers that objects are not `None`.

Using `assert` is another option, however this duplicates the logic in
`_check_released` so is redundant at best and, at worst, is not a safe
replacement as `assert` can be elided in optimized Python environments
and these checks need to be runtime enforced.

[0]: https://mypy.readthedocs.io/en/stable/error_code_list.html#check-that-attribute-exists-in-each-union-item-union-attr

Signed-off-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20241114145116.2123714-12-vfazio@xes-inc.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
bindings/python/gpiod/line_request.py