Message ID | 20240927-vfazio-mypy-v1-11-91a7c2e20884@xes-inc.com |
---|---|
State | New |
Headers | show |
Series | bindings: python: conform to mypy and ruff linter recommendations | expand |
On Fri, Sep 27, 2024 at 8:57 PM Vincent Fazio <vfazio@xes-inc.com> wrote: > > The `values` argument of `_ext.Request.get_values` uses a preallocated > `list[None]` as a buffer that is populated with `Value`s by the external > module that are then returned from the function. > > Use `cast` to inform the type checker it's a `list[Value]` despite how > it's allocated. > > Also, as `lines` is typed as an `Iterable`, there is no guarantee it has > a `__len__` method. Instead, use the size of the `offsets` array to > allocate the buffer. > > Signed-off-by: Vincent Fazio <vfazio@xes-inc.com> > --- > bindings/python/gpiod/line_request.py | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/bindings/python/gpiod/line_request.py b/bindings/python/gpiod/line_request.py > index a1ca64cfd82c32be5de3fc53f6c981026911bb9c..917020b9ec7046dd8e10158f70efb555fc87eade 100644 > --- a/bindings/python/gpiod/line_request.py > +++ b/bindings/python/gpiod/line_request.py > @@ -122,7 +122,7 @@ class LineRequest: > > offsets = [self._line_to_offset(line) for line in lines] > > - buf = [None] * len(lines) > + buf = cast(list[Value], [None] * len(offsets)) > > self._req.get_values(offsets, buf) > return buf > > -- > 2.34.1 > I think this is the first time in this series where I understand what cast() really does. Looks good. Bart
diff --git a/bindings/python/gpiod/line_request.py b/bindings/python/gpiod/line_request.py index a1ca64cfd82c32be5de3fc53f6c981026911bb9c..917020b9ec7046dd8e10158f70efb555fc87eade 100644 --- a/bindings/python/gpiod/line_request.py +++ b/bindings/python/gpiod/line_request.py @@ -122,7 +122,7 @@ class LineRequest: offsets = [self._line_to_offset(line) for line in lines] - buf = [None] * len(lines) + buf = cast(list[Value], [None] * len(offsets)) self._req.get_values(offsets, buf) return buf
The `values` argument of `_ext.Request.get_values` uses a preallocated `list[None]` as a buffer that is populated with `Value`s by the external module that are then returned from the function. Use `cast` to inform the type checker it's a `list[Value]` despite how it's allocated. Also, as `lines` is typed as an `Iterable`, there is no guarantee it has a `__len__` method. Instead, use the size of the `offsets` array to allocate the buffer. Signed-off-by: Vincent Fazio <vfazio@xes-inc.com> --- bindings/python/gpiod/line_request.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)