On Fri, Sep 27, 2024 at 8:57 PM Vincent Fazio <vfazio@xxxxxxxxxxx> wrote: > > Since `Chip._chip` can be `None`, it's necessary to inform type checkers > of the state of the object to silence the union-attr errors. > > Type checkers may not be able to infer that an object is not `None` from > an earlier call (such as `_check_closed`). > > 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_closed` 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. > > Signed-off-by: Vincent Fazio <vfazio@xxxxxxxxxxx> > --- > bindings/python/gpiod/chip.py | 11 ++++++++++- > 1 file changed, 10 insertions(+), 1 deletion(-) > > diff --git a/bindings/python/gpiod/chip.py b/bindings/python/gpiod/chip.py > index 4aa5677f94caf8c5d863aa6d75915a5b650de137..fe7bcfe082d6e9f6220093d3fc45ff232b5d0d17 100644 > --- a/bindings/python/gpiod/chip.py > +++ b/bindings/python/gpiod/chip.py > @@ -7,7 +7,7 @@ from collections import Counter > from datetime import timedelta > from errno import ENOENT > from types import TracebackType > -from typing import Optional, Union > +from typing import Optional, Union, cast > > from . import _ext > from ._internal import poll_fd > @@ -97,6 +97,7 @@ class Chip: > longer be used after this method is called. > """ > self._check_closed() > + self._chip = cast(_ext.Chip, self._chip) > self._chip.close() > self._chip = None > Ok, so I don't really understand what is happening here. We're going to re-assign _chip in every function? What happens to cast() if _chip IS None? Bart