From patchwork Thu Nov 14 14:50:56 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Fazio X-Patchwork-Id: 843626 Received: from mail.xes-mad.com (mail.xes-mad.com [162.248.234.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 38CA2179BD for ; Thu, 14 Nov 2024 14:51:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=162.248.234.2 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731595892; cv=none; b=qWNesUyfy7/Fl9hLRvoTUphCjO4uJqOgIIqObdMKLl3DKJ65APRxI+nuT9Peb5/VBD/roG1rblqXQ7qjAzZyeNeAugqX4OHtmFxFViF8d7yyrCn1Y1u2ruErTmvVTUKp2kKNQhaF9vm4m5uDcGKN8y+plWOLYJIzS79TErKrb0o= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731595892; c=relaxed/simple; bh=Ld8QqaHjkMywk8ju92DvpgVII2LpMVMsKwNc59ZFEZw=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=soBecxzf2P+ohuxt1b2wiwAF8rLpMPRVFd6AnvbyYdbsQstxvjBRKbWbUjP4lh8Lfa0XshP2cxGDySw7etrTHhnwWiWMNAHTmXnTv6l+khplgigzINGuC7GVVLo5DOf22Gq/SG4mlFUf/QHJ+Rd2NNv+u2Ya7DS4pjYcNkLw8Tk= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=xes-inc.com; spf=pass smtp.mailfrom=xes-inc.com; dkim=pass (1024-bit key) header.d=xes-inc.com header.i=@xes-inc.com header.b=eaHAZiGh; arc=none smtp.client-ip=162.248.234.2 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=xes-inc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=xes-inc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=xes-inc.com header.i=@xes-inc.com header.b="eaHAZiGh" Received: from vfazio4.xes-mad.com (vfazio4.xes-mad.com [10.52.19.201]) by mail.xes-mad.com (Postfix) with ESMTP id 6F23A20A48; Thu, 14 Nov 2024 08:51:22 -0600 (CST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=xes-inc.com; s=mail; t=1731595882; bh=Ld8QqaHjkMywk8ju92DvpgVII2LpMVMsKwNc59ZFEZw=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=eaHAZiGhWdr/VsZ6eXJ9K1Q0uSJE1gJUxuPFnY9sqODB//TEnIRmEavaq5m5S2RLS i8eJIhwcPh1102+MUYkYf69XOF4R1DFvl6c+fJ/XXINS2al2Vw0xq+7q1UCUwY2cbc b/BZDems7N9DtqLwKc6iEgx3upxb1tJOLr4vGOf4= From: Vincent Fazio To: linux-gpio@vger.kernel.org Cc: vfazio@gmail.com, Vincent Fazio Subject: [libgpiod][PATCH v2 03/23] bindings: python: loosen type requirements in public API Date: Thu, 14 Nov 2024 08:50:56 -0600 Message-Id: <20241114145116.2123714-4-vfazio@xes-inc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241114145116.2123714-1-vfazio@xes-inc.com> References: <20241114145116.2123714-1-vfazio@xes-inc.com> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Previously, `Chip.request_lines` and `LineRequest.reconfigure_lines` were typed to accept a config argument that was either an int, a str, or a tuple[int | str]. This had two downsides, namely: * The tuple was typed as having only a single element and not a variable number of elements. The examples and test suite relied on a variable length tuple. * The tuple type itself was overly restictive. The function implementations had no requirement that the value be a tuple, only that it was iterable if it was not a str or an int. Now, these functions accept an Iterable[int | str] instead of tuples to offer greater flexibility to callers. This change does not break compatibility for existing users. Closes: https://github.com/brgl/libgpiod/issues/102 Signed-off-by: Vincent Fazio --- bindings/python/gpiod/chip.py | 5 ++++- bindings/python/gpiod/line_request.py | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/bindings/python/gpiod/chip.py b/bindings/python/gpiod/chip.py index 175fcb0..1db199e 100644 --- a/bindings/python/gpiod/chip.py +++ b/bindings/python/gpiod/chip.py @@ -13,6 +13,7 @@ from .line_request import LineRequest from .line_settings import LineSettings, _line_settings_to_ext if TYPE_CHECKING: + from collections.abc import Iterable from datetime import timedelta from .chip_info import ChipInfo @@ -225,7 +226,9 @@ class Chip: def request_lines( self, - config: dict[tuple[Union[int, str]], Optional[LineSettings]], + config: dict[ + Union[Iterable[Union[int, str]], int, str], Optional[LineSettings] + ], consumer: Optional[str] = None, event_buffer_size: Optional[int] = None, output_values: Optional[dict[Union[int, str], Value]] = None, diff --git a/bindings/python/gpiod/line_request.py b/bindings/python/gpiod/line_request.py index a8e4a87..c7b32f3 100644 --- a/bindings/python/gpiod/line_request.py +++ b/bindings/python/gpiod/line_request.py @@ -148,7 +148,10 @@ class LineRequest: self._req.set_values(mapped) def reconfigure_lines( - self, config: dict[tuple[Union[int, str]], LineSettings] + self, + config: dict[ + Union[Iterable[Union[int, str]], int, str], Optional[LineSettings] + ], ) -> None: """ Reconfigure requested lines. From patchwork Thu Nov 14 14:50:58 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Fazio X-Patchwork-Id: 843624 Received: from mail.xes-mad.com (mail.xes-mad.com [162.248.234.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C678536AF5 for ; Thu, 14 Nov 2024 14:51:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=162.248.234.2 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731595893; cv=none; b=FZru+egeR/Gm5QFekubYOqVMJ6FWLpRXDWqDAEMl2FtCbX6k9SI45HmY6IoqKvRnO1PK/sKDRCcnprx5WQnO6q/Y48R2iQQqnNbXIOWJpJVf6eoyiHRo0YCzGqZrAA3qMlEULNsWjmhSTJmyc9PzeMeCnc5qosiMxfENHySaql8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731595893; c=relaxed/simple; bh=xUk2IRbdq+lHQV9yq/xb4NnV3hZjtrZmx/NDMoTL+wo=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=OHK7kbq56g88kU00H7TuHPUk2SF3ND1exhUJK7m3KIqFblRFErARniuwY+iXlT5arPh3luNcUl38evANimOovqUA1IAFORv/r8zgcLjnjR1wkH0vgxNcH3gD5jFGN77i3IaUiCuos81KyapRONB0iprktu4+Bg80ita3vFm71vQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=xes-inc.com; spf=pass smtp.mailfrom=xes-inc.com; dkim=pass (1024-bit key) header.d=xes-inc.com header.i=@xes-inc.com header.b=o7Lkw8Fp; arc=none smtp.client-ip=162.248.234.2 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=xes-inc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=xes-inc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=xes-inc.com header.i=@xes-inc.com header.b="o7Lkw8Fp" Received: from vfazio4.xes-mad.com (vfazio4.xes-mad.com [10.52.19.201]) by mail.xes-mad.com (Postfix) with ESMTP id 981B120A9C; Thu, 14 Nov 2024 08:51:22 -0600 (CST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=xes-inc.com; s=mail; t=1731595882; bh=xUk2IRbdq+lHQV9yq/xb4NnV3hZjtrZmx/NDMoTL+wo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=o7Lkw8FpYBBGSB0v4AksQ0bzeG4f2i7Y3DHZGfyIniZ7EFs/Zp5qhlHpypXIbcCRy mK3J1GJg1+aL2YV2TLJ7Euf7rhGZeRZ/AjSvOgkqF2D/VQCulFkAE1NynbNv7kwD55 M3+nys9fa7Lcxivc1WsNx2dsLNc4H+ebtNt3gO3c= From: Vincent Fazio To: linux-gpio@vger.kernel.org Cc: vfazio@gmail.com, Vincent Fazio Subject: [libgpiod][PATCH v2 05/23] bindings: python: add type stub for the _ext module Date: Thu, 14 Nov 2024 08:50:58 -0600 Message-Id: <20241114145116.2123714-6-vfazio@xes-inc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241114145116.2123714-1-vfazio@xes-inc.com> References: <20241114145116.2123714-1-vfazio@xes-inc.com> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add a type stub for the compiled external module so that types and methods used from the module are accurately type checked. Signed-off-by: Vincent Fazio --- bindings/python/gpiod/_ext.pyi | 93 ++++++++++++++++++++++++++++++++++ bindings/python/setup.py | 2 +- 2 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 bindings/python/gpiod/_ext.pyi diff --git a/bindings/python/gpiod/_ext.pyi b/bindings/python/gpiod/_ext.pyi new file mode 100644 index 0000000..1beb80d --- /dev/null +++ b/bindings/python/gpiod/_ext.pyi @@ -0,0 +1,93 @@ +# SPDX-License-Identifier: LGPL-2.1-or-later +# SPDX-FileCopyrightText: 2024 Vincent Fazio + +from typing import Optional + +from .chip_info import ChipInfo +from .edge_event import EdgeEvent +from .info_event import InfoEvent +from .line import Value +from .line_info import LineInfo + +class LineSettings: + def __init__( + self, + direction: int, + edge_detection: int, + bias: int, + drive: int, + active_low: bool, + debounce_period: int, + event_clock: int, + output_value: int, + ) -> None: ... + +class LineConfig: + def __init__(self) -> None: ... + def add_line_settings(self, offsets: list[int], settings: LineSettings) -> None: ... + def set_output_values(self, global_output_values: list[Value]) -> None: ... + +class Request: + def release(self) -> None: ... + def get_values(self, offsets: list[int], values: list[Value]) -> None: ... + def set_values(self, values: dict[int, Value]) -> None: ... + def reconfigure_lines(self, line_cfg: LineConfig) -> None: ... + def read_edge_events(self, max_events: Optional[int]) -> list[EdgeEvent]: ... + @property + def chip_name(self) -> str: ... + @property + def num_lines(self) -> int: ... + @property + def offsets(self) -> list[int]: ... + @property + def fd(self) -> int: ... + +class Chip: + def __init__(self, path: str) -> None: ... + def get_info(self) -> ChipInfo: ... + def line_offset_from_id(self, id: str) -> int: ... + def get_line_info(self, offset: int, watch: bool) -> LineInfo: ... + def request_lines( + self, + line_cfg: LineConfig, + consumer: Optional[str], + event_buffer_size: Optional[int], + ) -> Request: ... + def read_info_event(self) -> InfoEvent: ... + def close(self) -> None: ... + def unwatch_line_info(self, line: int) -> None: ... + @property + def path(self) -> str: ... + @property + def fd(self) -> int: ... + +def is_gpiochip_device(path: str) -> bool: ... + +api_version: str + +# enum constants +BIAS_AS_IS: int +BIAS_DISABLED: int +BIAS_PULL_DOWN: int +BIAS_PULL_UP: int +BIAS_UNKNOWN: int +CLOCK_HTE: int +CLOCK_MONOTONIC: int +CLOCK_REALTIME: int +DIRECTION_AS_IS: int +DIRECTION_INPUT: int +DIRECTION_OUTPUT: int +DRIVE_OPEN_DRAIN: int +DRIVE_OPEN_SOURCE: int +DRIVE_PUSH_PULL: int +EDGE_BOTH: int +EDGE_EVENT_TYPE_FALLING: int +EDGE_EVENT_TYPE_RISING: int +EDGE_FALLING: int +EDGE_NONE: int +EDGE_RISING: int +INFO_EVENT_TYPE_LINE_CONFIG_CHANGED: int +INFO_EVENT_TYPE_LINE_RELEASED: int +INFO_EVENT_TYPE_LINE_REQUESTED: int +VALUE_ACTIVE: int +VALUE_INACTIVE: int diff --git a/bindings/python/setup.py b/bindings/python/setup.py index 7ab783f..0d518af 100644 --- a/bindings/python/setup.py +++ b/bindings/python/setup.py @@ -228,7 +228,7 @@ setup( name="gpiod", url="https://git.kernel.org/pub/scm/libs/libgpiod/libgpiod.git", packages=find_packages(exclude=["tests", "tests.*"]), - package_data={"gpiod": ["py.typed"]}, + package_data={"gpiod": ["py.typed", "_ext.pyi"]}, python_requires=">=3.9.0", ext_modules=[gpiod_ext], cmdclass={"build_ext": build_ext, "sdist": sdist}, From patchwork Thu Nov 14 14:51:00 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Fazio X-Patchwork-Id: 843625 Received: from mail.xes-mad.com (mail.xes-mad.com [162.248.234.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C8F5E6F307 for ; Thu, 14 Nov 2024 14:51:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=162.248.234.2 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731595893; cv=none; b=O45mv4KAU83iAEHldMj6ZgzhhRckHm9NXbesup/n9Qm5WnAOfafLYO95ttUdqr/BRnM00WT3o7b/YsJeCXdpsjZi1snusV1RasMpEMJDmaor02YekG5aZSw4hbrUk7tjCkTc0bz5+0ubORLnpBxakCmp+ughf7g8max4ZSbCxqM= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731595893; c=relaxed/simple; bh=HS1GUKoL282nBRqSaaDTxIfibMjITKqb/KQdxvjE1Dg=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=j77TgRkzMUpolzqaZ+U4gy6LuKvLnvwDL0QqekMSdfHBmILT7i3HhGODTrN4RifYJ4crLdypTnwnMVLKDZNABhTfrC+1PkXhqw5gp1KeB1ud7sjWwFIDBNAca6pLQX/W/sMMRYOPSwNL473euHzOR0nt9IENWjggCx/hoEshUB0= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=xes-inc.com; spf=pass smtp.mailfrom=xes-inc.com; dkim=pass (1024-bit key) header.d=xes-inc.com header.i=@xes-inc.com header.b=f0wNDn+U; arc=none smtp.client-ip=162.248.234.2 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=xes-inc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=xes-inc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=xes-inc.com header.i=@xes-inc.com header.b="f0wNDn+U" Received: from vfazio4.xes-mad.com (vfazio4.xes-mad.com [10.52.19.201]) by mail.xes-mad.com (Postfix) with ESMTP id C16B220AA5; Thu, 14 Nov 2024 08:51:22 -0600 (CST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=xes-inc.com; s=mail; t=1731595882; bh=HS1GUKoL282nBRqSaaDTxIfibMjITKqb/KQdxvjE1Dg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=f0wNDn+UYW70bZi2kh3DuGzvEcio37Ac7cJo6S9cMYTrtBjmW3yZM1r5HTQjX9enE lcyqPy9JiRQ+B5hlwKt//4NzfYLZEeUXoFczxACqgc2r79RxWBbSupnFVmCJz5LtX/ UsdxY7Fjgv9gBVkNI4omx/eQ73ltoCxuOwqUPa4g= From: Vincent Fazio To: linux-gpio@vger.kernel.org Cc: vfazio@gmail.com, Vincent Fazio Subject: [libgpiod][PATCH v2 07/23] bindings: python: add type hint for the sec variable in poll_fd Date: Thu, 14 Nov 2024 08:51:00 -0600 Message-Id: <20241114145116.2123714-8-vfazio@xes-inc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241114145116.2123714-1-vfazio@xes-inc.com> References: <20241114145116.2123714-1-vfazio@xes-inc.com> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 The sec variable can be either a float or None, so type it accordingly to avoid complaints from type checkers. Signed-off-by: Vincent Fazio --- bindings/python/gpiod/_internal.py | 1 + 1 file changed, 1 insertion(+) diff --git a/bindings/python/gpiod/_internal.py b/bindings/python/gpiod/_internal.py index d1e95e4..c9b5d28 100644 --- a/bindings/python/gpiod/_internal.py +++ b/bindings/python/gpiod/_internal.py @@ -9,6 +9,7 @@ __all__ = ["poll_fd"] def poll_fd(fd: int, timeout: Optional[Union[timedelta, float]] = None) -> bool: + sec: Union[float, None] if isinstance(timeout, timedelta): sec = timeout.total_seconds() else: From patchwork Thu Nov 14 14:51:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Fazio X-Patchwork-Id: 843622 Received: from mail.xes-mad.com (mail.xes-mad.com [162.248.234.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id C68196F2F3 for ; Thu, 14 Nov 2024 14:51:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=162.248.234.2 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731595894; cv=none; b=otj2/7/t97fjlyLsnXSbrg3cIyIWdY8NIO0jb+dVRKuXPmNGMiv1f7BCIOLyMlS+hZ7gOvjGG2J1TxOxfL5wb3F0AkabBEMCWuzpr+e21huzHaEEXz4puCpmQibHKGKNYM4gjQjVpjuOTEUpRnIHgRz0TSsQPk8oSG3f+VIjoZ8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731595894; c=relaxed/simple; bh=h6aB99dT/vAmCz4535pHREsxBEfHYHJ+OD68VRVBLRs=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=KC56uLw0jgZBfRxEKUW3yXWFzGg7NW6sZ7VWjxlyRYrgZyvNYUYYViJ8KfKr1hm09riNLDWLwiGdCfgdAJi3jeEHrd6L7VjC5SfDrnJDUIyo9BCCtI/H6RLiFK98I7cmkFhvNnXcJoOAbjf25NNX5k1xWu5Lv4S5cRXkXFKbWiM= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=xes-inc.com; spf=pass smtp.mailfrom=xes-inc.com; dkim=pass (1024-bit key) header.d=xes-inc.com header.i=@xes-inc.com header.b=w0aKWPES; arc=none smtp.client-ip=162.248.234.2 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=xes-inc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=xes-inc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=xes-inc.com header.i=@xes-inc.com header.b="w0aKWPES" Received: from vfazio4.xes-mad.com (vfazio4.xes-mad.com [10.52.19.201]) by mail.xes-mad.com (Postfix) with ESMTP id D5ED520AAC; Thu, 14 Nov 2024 08:51:22 -0600 (CST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=xes-inc.com; s=mail; t=1731595882; bh=h6aB99dT/vAmCz4535pHREsxBEfHYHJ+OD68VRVBLRs=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=w0aKWPESzAuoPzJ0u7AZI4UOUlfPsb3FtZN5NaA3A2d3N0Zq4RRAPgJwK4NzqbiXr QJV/EVVF42OTCyigV0pqiL/m7LFVIJ86TIFXMrhWyFE0MDZCsj2jHr0qw7JM4IHSQb h3KCaJLdtECRlHzyPQU9FNNBawehljX/dL8MD6KY= From: Vincent Fazio To: linux-gpio@vger.kernel.org Cc: vfazio@gmail.com, Vincent Fazio Subject: [libgpiod][PATCH v2 08/23] bindings: python: add type hints for Chip's internal members Date: Thu, 14 Nov 2024 08:51:01 -0600 Message-Id: <20241114145116.2123714-9-vfazio@xes-inc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241114145116.2123714-1-vfazio@xes-inc.com> References: <20241114145116.2123714-1-vfazio@xes-inc.com> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add type hints for Chip's internal members so type checkers can ensure the code properly constrains to these types and accounts for scenarios where the values are `None`. Signed-off-by: Vincent Fazio --- bindings/python/gpiod/chip.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bindings/python/gpiod/chip.py b/bindings/python/gpiod/chip.py index bf38c7f..69ea777 100644 --- a/bindings/python/gpiod/chip.py +++ b/bindings/python/gpiod/chip.py @@ -61,8 +61,8 @@ class Chip: path: Path to the GPIO character device file. """ - self._chip = _ext.Chip(path) - self._info = None + self._chip: Union[_ext.Chip, None] = _ext.Chip(path) + self._info: Union[ChipInfo, None] = None def __bool__(self) -> bool: """ From patchwork Thu Nov 14 14:51:02 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Fazio X-Patchwork-Id: 843617 Received: from mail.xes-mad.com (mail.xes-mad.com [162.248.234.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id F021344C81 for ; Thu, 14 Nov 2024 14:51:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=162.248.234.2 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731595897; cv=none; b=mT4ddfcmkwX5+1CZIFVgbg6fDKo+CiETtLawX2L9h7HZBHINKc0J8xEXilsG/6Gg2rWi31hps4sykrqd4k1OyyBenjyDrkaEhXzoagtFlF0V6ZY8igxJZppyp/RxaIuHYwV/Prl/wXLAe3V65nbVWAUyLjq3HLDKbOPcSOzTG68= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731595897; c=relaxed/simple; bh=KhkKPxF8lak37r03c84CKGcuxD0tq/clEjF9bWFFxL4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=rsvHkd7vDCbz74bLOJrUH5dSFjjAIOoH3RXKRj0niag+DnCF4zB/s03efaM1BUU8A3x+tlVj2wFiSfy1wbdBVpUlrWiV8ei7kiYLs+6ndXqAOe281N3rhtvdJUmM8XAL+FA8pfolU2UhbOMuV+SjZxisAIWWufWHflirAjLFD54= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=xes-inc.com; spf=pass smtp.mailfrom=xes-inc.com; dkim=pass (1024-bit key) header.d=xes-inc.com header.i=@xes-inc.com header.b=jHezGXwj; arc=none smtp.client-ip=162.248.234.2 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=xes-inc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=xes-inc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=xes-inc.com header.i=@xes-inc.com header.b="jHezGXwj" Received: from vfazio4.xes-mad.com (vfazio4.xes-mad.com [10.52.19.201]) by mail.xes-mad.com (Postfix) with ESMTP id EA81220AAE; Thu, 14 Nov 2024 08:51:22 -0600 (CST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=xes-inc.com; s=mail; t=1731595882; bh=KhkKPxF8lak37r03c84CKGcuxD0tq/clEjF9bWFFxL4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=jHezGXwjHuN8tBfCe901xxFoBiKwDAtJ8sYVzZSCKxbzANY+mZ4TqNTw6bJOJBeQB jdxMyHpQ8Eh3Nc3rfTqzxc/yyjqhFAqdh79pZuNaRJ8xoyqGSGsa8Z/8Oei3CwS5H+ QxmAcceqbOISFFIbsN3b/4h3sYBgB2Da1tj/adiA= From: Vincent Fazio To: linux-gpio@vger.kernel.org Cc: vfazio@gmail.com, Vincent Fazio Subject: [libgpiod][PATCH v2 09/23] bindings: python: fix Chip union-attr type errors Date: Thu, 14 Nov 2024 08:51:02 -0600 Message-Id: <20241114145116.2123714-10-vfazio@xes-inc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241114145116.2123714-1-vfazio@xes-inc.com> References: <20241114145116.2123714-1-vfazio@xes-inc.com> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Since `Chip._chip` 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_closed`) 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_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. [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 --- bindings/python/gpiod/chip.py | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/bindings/python/gpiod/chip.py b/bindings/python/gpiod/chip.py index 69ea777..482b98b 100644 --- a/bindings/python/gpiod/chip.py +++ b/bindings/python/gpiod/chip.py @@ -5,7 +5,7 @@ from __future__ import annotations from collections import Counter from errno import ENOENT -from typing import TYPE_CHECKING, Optional, Union +from typing import TYPE_CHECKING, Optional, Union, cast from . import _ext from ._internal import poll_fd @@ -101,7 +101,7 @@ class Chip: longer be used after this method is called. """ self._check_closed() - self._chip.close() + cast(_ext.Chip, self._chip).close() self._chip = None def get_info(self) -> ChipInfo: @@ -114,7 +114,7 @@ class Chip: self._check_closed() if not self._info: - self._info = self._chip.get_info() + self._info = cast(_ext.Chip, self._chip).get_info() return self._info @@ -139,7 +139,7 @@ class Chip: if not isinstance(id, int): try: - return self._chip.line_offset_from_id(id) + return cast(_ext.Chip, self._chip).line_offset_from_id(id) except OSError as ex: if ex.errno == ENOENT: try: @@ -158,7 +158,9 @@ class Chip: def _get_line_info(self, line: Union[int, str], watch: bool) -> LineInfo: self._check_closed() - return self._chip.get_line_info(self.line_offset_from_id(line), watch) + return cast(_ext.Chip, self._chip).get_line_info( + self.line_offset_from_id(line), watch + ) def get_line_info(self, line: Union[int, str]) -> LineInfo: """ @@ -196,7 +198,9 @@ class Chip: Offset or name of the line to stop watching. """ self._check_closed() - return self._chip.unwatch_line_info(self.line_offset_from_id(line)) + return cast(_ext.Chip, self._chip).unwatch_line_info( + self.line_offset_from_id(line) + ) def wait_info_event( self, timeout: Optional[Union[timedelta, float]] = None @@ -230,7 +234,7 @@ class Chip: This function may block if there are no available events in the queue. """ self._check_closed() - return self._chip.read_info_event() + return cast(_ext.Chip, self._chip).read_info_event() def request_lines( self, @@ -326,7 +330,9 @@ class Chip: if len(global_output_values): line_cfg.set_output_values(global_output_values) - req_internal = self._chip.request_lines(line_cfg, consumer, event_buffer_size) + req_internal = cast(_ext.Chip, self._chip).request_lines( + line_cfg, consumer, event_buffer_size + ) request = LineRequest(req_internal) request._chip_name = req_internal.chip_name @@ -366,7 +372,7 @@ class Chip: Filesystem path used to open this chip. """ self._check_closed() - return self._chip.path + return cast(_ext.Chip, self._chip).path @property def fd(self) -> int: @@ -374,4 +380,4 @@ class Chip: File descriptor associated with this chip. """ self._check_closed() - return self._chip.fd + return cast(_ext.Chip, self._chip).fd From patchwork Thu Nov 14 14:51:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Fazio X-Patchwork-Id: 843621 Received: from mail.xes-mad.com (mail.xes-mad.com [162.248.234.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1F0454D9FE for ; Thu, 14 Nov 2024 14:51:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=162.248.234.2 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731595895; cv=none; b=dR0lwy3/VVBG4y8fclYyn3Nisu82uiMoxgk8u9fpfbKbu4uPq+RoFMTi3vWAWc34upeTb3321+vlxjHIQ73o+2YGYbc9a8NrcInoy3Y/91l9UtGfi3fEXY5JDCSVZy9kk/dIBCVV56plNnX67euDfCpgTZRr/EJIM4tB2m/75j4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731595895; c=relaxed/simple; bh=xePOxLQmu+k04jd8EiUx7g+K1U4mxeZ1DY2jQpuYac4=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Hg3kL+POg+aoFTO/hJQ3TA8PyS9elgnOJmq2kx1j5HuqHgoUMbd2Ifp3L70VB00yilq1ThOZ4KqrP/i9oeArRykrHNZysLUw9LfW0CSVpc46mUqnQEdOExc4VcNaVe7gN+JZxWVMO+3YS7jdC8rrX2ItTvc1W2HtJeKtxpZeIAs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=xes-inc.com; spf=pass smtp.mailfrom=xes-inc.com; dkim=pass (1024-bit key) header.d=xes-inc.com header.i=@xes-inc.com header.b=bHBvF/5X; arc=none smtp.client-ip=162.248.234.2 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=xes-inc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=xes-inc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=xes-inc.com header.i=@xes-inc.com header.b="bHBvF/5X" Received: from vfazio4.xes-mad.com (vfazio4.xes-mad.com [10.52.19.201]) by mail.xes-mad.com (Postfix) with ESMTP id 1F1ED20AB2; Thu, 14 Nov 2024 08:51:23 -0600 (CST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=xes-inc.com; s=mail; t=1731595883; bh=xePOxLQmu+k04jd8EiUx7g+K1U4mxeZ1DY2jQpuYac4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=bHBvF/5X7wKKUy5ZjsMz4nMYUjQ6bjUB9g0t3BSSg3iFfgxgpSNqtHyRMZBI4I5Eg p8YX1kzBn0Plm/pawjatZ2ssZsMWlATqodKAnVqyZcVqWgR53cWZ2WZzGSowpGgKwz O47qx8NHZcZ5W479k7ZUwHm9zSP8QA1u4XotfAgg= From: Vincent Fazio To: linux-gpio@vger.kernel.org Cc: vfazio@gmail.com, Vincent Fazio Subject: [libgpiod][PATCH v2 11/23] bindings: python: fix LineRequest union-attr type errors Date: Thu, 14 Nov 2024 08:51:04 -0600 Message-Id: <20241114145116.2123714-12-vfazio@xes-inc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241114145116.2123714-1-vfazio@xes-inc.com> References: <20241114145116.2123714-1-vfazio@xes-inc.com> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 --- bindings/python/gpiod/line_request.py | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/bindings/python/gpiod/line_request.py b/bindings/python/gpiod/line_request.py index f8bbf64..81f2517 100644 --- a/bindings/python/gpiod/line_request.py +++ b/bindings/python/gpiod/line_request.py @@ -3,7 +3,7 @@ from __future__ import annotations -from typing import TYPE_CHECKING, Optional, Union +from typing import TYPE_CHECKING, Optional, Union, cast from . import _ext from ._internal import poll_fd @@ -78,7 +78,7 @@ class LineRequest: not be used after a call to this method. """ self._check_released() - self._req.release() + cast(_ext.Request, self._req).release() self._req = None def get_value(self, line: Union[int, str]) -> Value: @@ -128,7 +128,7 @@ class LineRequest: buf = [None] * len(lines) - self._req.get_values(offsets, buf) + cast(_ext.Request, self._req).get_values(offsets, buf) return buf def set_value(self, line: Union[int, str], value: Value) -> None: @@ -158,7 +158,7 @@ class LineRequest: for line in values } - self._req.set_values(mapped) + cast(_ext.Request, self._req).set_values(mapped) def reconfigure_lines( self, @@ -193,7 +193,7 @@ class LineRequest: settings = line_settings.get(offset) or LineSettings() line_cfg.add_line_settings([offset], _line_settings_to_ext(settings)) - self._req.reconfigure_lines(line_cfg) + cast(_ext.Request, self._req).reconfigure_lines(line_cfg) def wait_edge_events( self, timeout: Optional[Union[timedelta, float]] = None @@ -227,7 +227,7 @@ class LineRequest: """ self._check_released() - return self._req.read_edge_events(max_events) + return cast(_ext.Request, self._req).read_edge_events(max_events) def __str__(self) -> str: """ @@ -279,4 +279,4 @@ class LineRequest: File descriptor associated with this request. """ self._check_released() - return self._req.fd + return cast(_ext.Request, self._req).fd From patchwork Thu Nov 14 14:51:06 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Fazio X-Patchwork-Id: 843623 Received: from mail.xes-mad.com (mail.xes-mad.com [162.248.234.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1EF8A179BD for ; Thu, 14 Nov 2024 14:51:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=162.248.234.2 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731595894; cv=none; b=G8S7JJ5crPg9Iv0znEZVqaUw6yMKoxi87zL0X7orvHTxqtkdhD6OnoNzHcYYHJZiRvmfhBhOzPMrqkO4EkiAse+UOL+Nl7pUpDADpQ34jYmCRDBmAlRxaRfCL36qV6ALHIJrjFWj//PZjR2D/5mxZx4x+TNYn+WZjA47o8+Oye4= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731595894; c=relaxed/simple; bh=PoaQF24GHlIxevYL09mpbGzqdeEWvku20jXh44DvccE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=qYb49dhhE115LlafYwadPXeCCnXsb1BOGI560iOzcc7i1IKGXptGM+6xIDGD64KOKcwfUqiV7qMengXneiEGBxmW3+EwmqU+/nofBj55cFme52wXk3l99iOmTP8pzs26SIWO4RQtdvLpOMy50KGaW1U+K2HK4uM+/P8vH5TIhKA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=xes-inc.com; spf=pass smtp.mailfrom=xes-inc.com; dkim=pass (1024-bit key) header.d=xes-inc.com header.i=@xes-inc.com header.b=HSs26u5j; arc=none smtp.client-ip=162.248.234.2 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=xes-inc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=xes-inc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=xes-inc.com header.i=@xes-inc.com header.b="HSs26u5j" Received: from vfazio4.xes-mad.com (vfazio4.xes-mad.com [10.52.19.201]) by mail.xes-mad.com (Postfix) with ESMTP id 480E720AB5; Thu, 14 Nov 2024 08:51:23 -0600 (CST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=xes-inc.com; s=mail; t=1731595883; bh=PoaQF24GHlIxevYL09mpbGzqdeEWvku20jXh44DvccE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=HSs26u5jNF/wT17KLqUGwrm8JCGR6PP33SAKoeEFdMl+5Cbrfob0RZwBr8ilOpGtD KUA1vnQljdqWdZmB2W0ezrP1QqdJO4o4qTZUlEw86RZkZd7WUGr5Mus4OBsh8q9CVr AM/NtxBxN175HAIje0FLCtSjxGjF0hgBH6bhQOQU= From: Vincent Fazio To: linux-gpio@vger.kernel.org Cc: vfazio@gmail.com, Vincent Fazio Subject: [libgpiod][PATCH v2 13/23] bindings: python: cast return value of LineRequest.get_values Date: Thu, 14 Nov 2024 08:51:06 -0600 Message-Id: <20241114145116.2123714-14-vfazio@xes-inc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241114145116.2123714-1-vfazio@xes-inc.com> References: <20241114145116.2123714-1-vfazio@xes-inc.com> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 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 --- bindings/python/gpiod/line_request.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/bindings/python/gpiod/line_request.py b/bindings/python/gpiod/line_request.py index 3f181c9..7327274 100644 --- a/bindings/python/gpiod/line_request.py +++ b/bindings/python/gpiod/line_request.py @@ -8,6 +8,7 @@ from typing import TYPE_CHECKING, Optional, Union, cast from . import _ext from ._internal import poll_fd from .exception import RequestReleasedError +from .line import Value from .line_settings import LineSettings, _line_settings_to_ext if TYPE_CHECKING: @@ -16,7 +17,6 @@ if TYPE_CHECKING: from types import TracebackType from .edge_event import EdgeEvent - from .line import Value __all__ = ["LineRequest"] @@ -124,7 +124,7 @@ class LineRequest: offsets = [self._line_to_offset(line) for line in lines] - buf = [None] * len(lines) + buf = cast(list[Value], [None] * len(offsets)) cast(_ext.Request, self._req).get_values(offsets, buf) return buf From patchwork Thu Nov 14 14:51:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Fazio X-Patchwork-Id: 843616 Received: from mail.xes-mad.com (mail.xes-mad.com [162.248.234.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 0A99436AF5 for ; Thu, 14 Nov 2024 14:51:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=162.248.234.2 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731595898; cv=none; b=GmuvUtlW3aIu0ipkg9/woyk5BsbOT8pIk1fEhZGo+Fe8VFPu13kzFnqmce97Xk5ZZ6lRo9qBPzt96J3DwChQhRpyUGwXS+2tl42vFevAVQkpsy71RBGbwTN6z1QW+iLpFGiFw6TMoNZwnDBYqFs4qIQdNLQAWpXBLFX35B/2zwo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731595898; c=relaxed/simple; bh=vpgqjTRjvH2hDTv+KIRi22Hv4ChI0YVrwWYMdkJw4JQ=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=KR2kVVULnsPivajmCRVb8oV7Zu5jBxkLkp2OeEONkKJdlIx+Gap9LpvXN7W+AL0xXBOl/GiMEU44mK8Ugi/ZdFEzxlGyj7r3RKNHa4e+mjVnJNitPH4vt4e95jZqxKdEYTXeEuco20PGW55G0u/IyT17FgZxCSC5MfwALK5WbDs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=xes-inc.com; spf=pass smtp.mailfrom=xes-inc.com; dkim=pass (1024-bit key) header.d=xes-inc.com header.i=@xes-inc.com header.b=itZuNDWO; arc=none smtp.client-ip=162.248.234.2 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=xes-inc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=xes-inc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=xes-inc.com header.i=@xes-inc.com header.b="itZuNDWO" Received: from vfazio4.xes-mad.com (vfazio4.xes-mad.com [10.52.19.201]) by mail.xes-mad.com (Postfix) with ESMTP id 85D3020AB8; Thu, 14 Nov 2024 08:51:23 -0600 (CST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=xes-inc.com; s=mail; t=1731595883; bh=vpgqjTRjvH2hDTv+KIRi22Hv4ChI0YVrwWYMdkJw4JQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=itZuNDWOhP8+SGxZnSNgagTcMjMyOt/9JdTMcDd1kMjFfPCSHNO3L0zYgj9LDqigw zR65HpacjswNZ0DaYBebtgsTC+Agd/9FlhocmS/Q8k8ACzj+NdGdyfYOodmD2FD7Iw FFccKe1fOB0WeKyQpPuIlKRWaru5Q1uvcbAGmjsE= From: Vincent Fazio To: linux-gpio@vger.kernel.org Cc: vfazio@gmail.com, Vincent Fazio Subject: [libgpiod][PATCH v2 16/23] bindings: python: tests: clean up imports and exports Date: Thu, 14 Nov 2024 08:51:09 -0600 Message-Id: <20241114145116.2123714-17-vfazio@xes-inc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241114145116.2123714-1-vfazio@xes-inc.com> References: <20241114145116.2123714-1-vfazio@xes-inc.com> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Remove unused imports and sort the remainder following isort rules. Update the submodules to use `__all__` to advertise available imports. Remove an unnecessary blank line in the VersionString class to abide by the ruff formatter's rules (compatible with black's formatting). Signed-off-by: Vincent Fazio --- bindings/python/tests/__init__.py | 2 -- bindings/python/tests/__main__.py | 5 ++--- bindings/python/tests/gpiosim/__init__.py | 2 ++ bindings/python/tests/gpiosim/chip.py | 3 ++- bindings/python/tests/procname/__init__.py | 2 ++ bindings/python/tests/tests_chip.py | 5 +++-- bindings/python/tests/tests_chip_info.py | 3 ++- bindings/python/tests/tests_edge_event.py | 9 +++++---- bindings/python/tests/tests_info_event.py | 10 +++++----- bindings/python/tests/tests_line.py | 3 ++- bindings/python/tests/tests_line_info.py | 12 ++++++------ bindings/python/tests/tests_line_request.py | 6 +++--- bindings/python/tests/tests_line_settings.py | 7 +++---- bindings/python/tests/tests_module.py | 7 +++---- 14 files changed, 40 insertions(+), 36 deletions(-) diff --git a/bindings/python/tests/__init__.py b/bindings/python/tests/__init__.py index 02f4e8d..2374e81 100644 --- a/bindings/python/tests/__init__.py +++ b/bindings/python/tests/__init__.py @@ -2,8 +2,6 @@ # SPDX-FileCopyrightText: 2022 Bartosz Golaszewski import os -import unittest - from distutils.version import LooseVersion required_kernel_version = LooseVersion("5.19.0") diff --git a/bindings/python/tests/__main__.py b/bindings/python/tests/__main__.py index ea4143f..8b4260d 100644 --- a/bindings/python/tests/__main__.py +++ b/bindings/python/tests/__main__.py @@ -4,17 +4,16 @@ import unittest +from . import procname from .tests_chip import * from .tests_chip_info import * from .tests_edge_event import * from .tests_info_event import * from .tests_line import * from .tests_line_info import * +from .tests_line_request import * from .tests_line_settings import * from .tests_module import * -from .tests_line_request import * - -from . import procname procname.set_process_name("python-gpiod") diff --git a/bindings/python/tests/gpiosim/__init__.py b/bindings/python/tests/gpiosim/__init__.py index f65e413..64a1551 100644 --- a/bindings/python/tests/gpiosim/__init__.py +++ b/bindings/python/tests/gpiosim/__init__.py @@ -2,3 +2,5 @@ # SPDX-FileCopyrightText: 2022 Bartosz Golaszewski from .chip import Chip + +__all__ = ["Chip"] diff --git a/bindings/python/tests/gpiosim/chip.py b/bindings/python/tests/gpiosim/chip.py index 6af883e..691bfe1 100644 --- a/bindings/python/tests/gpiosim/chip.py +++ b/bindings/python/tests/gpiosim/chip.py @@ -1,10 +1,11 @@ # SPDX-License-Identifier: GPL-2.0-or-later # SPDX-FileCopyrightText: 2022 Bartosz Golaszewski -from . import _ext from enum import Enum from typing import Optional +from . import _ext + class Chip: """ diff --git a/bindings/python/tests/procname/__init__.py b/bindings/python/tests/procname/__init__.py index af6abdd..436ff40 100644 --- a/bindings/python/tests/procname/__init__.py +++ b/bindings/python/tests/procname/__init__.py @@ -2,3 +2,5 @@ # SPDX-FileCopyrightText: 2022 Bartosz Golaszewski from ._ext import set_process_name + +__all__ = ["set_process_name"] diff --git a/bindings/python/tests/tests_chip.py b/bindings/python/tests/tests_chip.py index bd4ae34..9110beb 100644 --- a/bindings/python/tests/tests_chip.py +++ b/bindings/python/tests/tests_chip.py @@ -2,12 +2,13 @@ # SPDX-FileCopyrightText: 2022 Bartosz Golaszewski import errno -import gpiod import os +from unittest import TestCase + +import gpiod from . import gpiosim from .helpers import LinkGuard -from unittest import TestCase class ChipConstructor(TestCase): diff --git a/bindings/python/tests/tests_chip_info.py b/bindings/python/tests/tests_chip_info.py index d392ec3..9474f38 100644 --- a/bindings/python/tests/tests_chip_info.py +++ b/bindings/python/tests/tests_chip_info.py @@ -1,10 +1,11 @@ # SPDX-License-Identifier: GPL-2.0-or-later # SPDX-FileCopyrightText: 2022 Bartosz Golaszewski +from unittest import TestCase + import gpiod from . import gpiosim -from unittest import TestCase class ChipInfoProperties(TestCase): diff --git a/bindings/python/tests/tests_edge_event.py b/bindings/python/tests/tests_edge_event.py index 68ab17e..7e7fada 100644 --- a/bindings/python/tests/tests_edge_event.py +++ b/bindings/python/tests/tests_edge_event.py @@ -1,16 +1,17 @@ # SPDX-License-Identifier: GPL-2.0-or-later # SPDX-FileCopyrightText: 2022 Bartosz Golaszewski -import gpiod import time - -from . import gpiosim from datetime import timedelta from functools import partial -from gpiod.line import Direction, Edge from threading import Thread from unittest import TestCase +import gpiod +from gpiod.line import Direction, Edge + +from . import gpiosim + EventType = gpiod.EdgeEvent.Type Pull = gpiosim.Chip.Pull diff --git a/bindings/python/tests/tests_info_event.py b/bindings/python/tests/tests_info_event.py index a226e52..bbdbc0f 100644 --- a/bindings/python/tests/tests_info_event.py +++ b/bindings/python/tests/tests_info_event.py @@ -3,17 +3,17 @@ import datetime import errno -import gpiod import threading import time -import unittest - -from . import gpiosim from dataclasses import FrozenInstanceError from functools import partial -from gpiod.line import Direction from unittest import TestCase +import gpiod +from gpiod.line import Direction + +from . import gpiosim + EventType = gpiod.InfoEvent.Type diff --git a/bindings/python/tests/tests_line.py b/bindings/python/tests/tests_line.py index 70aa09b..2182567 100644 --- a/bindings/python/tests/tests_line.py +++ b/bindings/python/tests/tests_line.py @@ -1,9 +1,10 @@ # SPDX-License-Identifier: LGPL-2.1-or-later # SPDX-FileCopyrightText: 2024 Kent Gibson -from gpiod.line import Value from unittest import TestCase +from gpiod.line import Value + class LineValue(TestCase): def test_cast_bool(self): diff --git a/bindings/python/tests/tests_line_info.py b/bindings/python/tests/tests_line_info.py index 2779e7a..79281a8 100644 --- a/bindings/python/tests/tests_line_info.py +++ b/bindings/python/tests/tests_line_info.py @@ -1,17 +1,17 @@ # SPDX-License-Identifier: GPL-2.0-or-later # SPDX-FileCopyrightText: 2022 Bartosz Golaszewski -import errno +from unittest import TestCase + import gpiod -import unittest +from gpiod.line import Bias, Clock, Direction, Drive from . import gpiosim -from gpiod.line import Direction, Bias, Drive, Clock HogDir = gpiosim.Chip.Direction -class GetLineInfo(unittest.TestCase): +class GetLineInfo(TestCase): def setUp(self): self.sim = gpiosim.Chip( num_lines=4, @@ -49,7 +49,7 @@ class GetLineInfo(unittest.TestCase): self.chip.get_line_info() -class LinePropertiesCanBeRead(unittest.TestCase): +class LinePropertiesCanBeRead(TestCase): def test_basic_properties(self): sim = gpiosim.Chip( num_lines=8, @@ -86,7 +86,7 @@ class LinePropertiesCanBeRead(unittest.TestCase): self.assertEqual(info6.debounce_period.total_seconds(), 0.0) -class LineInfoStringRepresentation(unittest.TestCase): +class LineInfoStringRepresentation(TestCase): def test_line_info_str(self): sim = gpiosim.Chip( line_names={0: "foo"}, hogs={0: ("hogger", HogDir.OUTPUT_HIGH)} diff --git a/bindings/python/tests/tests_line_request.py b/bindings/python/tests/tests_line_request.py index 285c9f1..c3e86c5 100644 --- a/bindings/python/tests/tests_line_request.py +++ b/bindings/python/tests/tests_line_request.py @@ -1,12 +1,12 @@ # SPDX-License-Identifier: GPL-2.0-or-later # SPDX-FileCopyrightText: 2022 Bartosz Golaszewski -import errno +from unittest import TestCase + import gpiod +from gpiod.line import Clock, Direction, Drive, Edge, Value from . import gpiosim -from gpiod.line import Clock, Direction, Drive, Edge, Value -from unittest import TestCase Pull = gpiosim.Chip.Pull SimVal = gpiosim.Chip.Value diff --git a/bindings/python/tests/tests_line_settings.py b/bindings/python/tests/tests_line_settings.py index 83be3d9..832ac8a 100644 --- a/bindings/python/tests/tests_line_settings.py +++ b/bindings/python/tests/tests_line_settings.py @@ -2,12 +2,11 @@ # SPDX-FileCopyrightText: 2022 Bartosz Golaszewski import datetime -import gpiod - -from . import gpiosim -from gpiod.line import Direction, Edge, Bias, Drive, Value, Clock from unittest import TestCase +import gpiod +from gpiod.line import Bias, Clock, Direction, Drive, Edge, Value + class LineSettingsConstructor(TestCase): def test_default_values(self): diff --git a/bindings/python/tests/tests_module.py b/bindings/python/tests/tests_module.py index c6f07a6..f46729f 100644 --- a/bindings/python/tests/tests_module.py +++ b/bindings/python/tests/tests_module.py @@ -1,13 +1,13 @@ # SPDX-License-Identifier: GPL-2.0-or-later # SPDX-FileCopyrightText: 2022 Bartosz Golaszewski -import gpiod import os -import unittest +from unittest import TestCase + +import gpiod from . import gpiosim from .helpers import LinkGuard -from unittest import TestCase class IsGPIOChip(TestCase): @@ -50,7 +50,6 @@ class IsGPIOChip(TestCase): class VersionString(TestCase): - VERSION_PATTERN = "^\\d+\\.\\d+(\\.\\d+|\\-devel|\\-rc\\d+)?$" def test_api_version_string(self): From patchwork Thu Nov 14 14:51:11 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Fazio X-Patchwork-Id: 843620 Received: from mail.xes-mad.com (mail.xes-mad.com [162.248.234.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3D1AD84E18 for ; Thu, 14 Nov 2024 14:51:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=162.248.234.2 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731595895; cv=none; b=N9Sqjbj0vhlnxNXCl/SzoB427hN4mW7GTEzcLtYdsUuLET+7kYhzl43iKqk5A168me8z5wEPsLLCKcaCH6HZUxpok+1aY3mD1zpoxWjnBY5UGoRdm2X2gWEbMWz7mlwd6vgR74gmDa+yVRAMJLvPX9uK5I58x+DWRMUY4lGdZmQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731595895; c=relaxed/simple; bh=r3TPDi30k3o1UDzICcKWj0WXToZFNPhUaaGaQgjpzis=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=eDjWqgXOZ48/1QspvQoOoBLLJPBTKngJHh+/tyhIlEo8iBQOS82Mf0jBBkQVtOSyYfORuD+RGLcYC1axBmg7gpx07mWkikPLm0xP9EcAC6csxTP+a8Bq8kui7YslPPBRhDQ9idG9k3795Sp1qgcqSTOfIIHnwx5UOQQaTcQAuBY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=xes-inc.com; spf=pass smtp.mailfrom=xes-inc.com; dkim=pass (1024-bit key) header.d=xes-inc.com header.i=@xes-inc.com header.b=CtPMye0Z; arc=none smtp.client-ip=162.248.234.2 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=xes-inc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=xes-inc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=xes-inc.com header.i=@xes-inc.com header.b="CtPMye0Z" Received: from vfazio4.xes-mad.com (vfazio4.xes-mad.com [10.52.19.201]) by mail.xes-mad.com (Postfix) with ESMTP id AFD8E20ABA; Thu, 14 Nov 2024 08:51:23 -0600 (CST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=xes-inc.com; s=mail; t=1731595883; bh=r3TPDi30k3o1UDzICcKWj0WXToZFNPhUaaGaQgjpzis=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=CtPMye0Z7Pcnj1X8Asp/ot1LDJdKbgtK2iYMzjMqUDPMGjsMZkZjH+YxehyAAqpTW K2yeQQsEQEFxpSWTX5Ln5VEuGyVBdKMvL6yKOciM5zwhyb90E5Ogtm+JLyntqoDUk3 Lgo++omf1aHhzK33QibZbhkFppujUMrF1C6JXphY= From: Vincent Fazio To: linux-gpio@vger.kernel.org Cc: vfazio@gmail.com, Vincent Fazio Subject: [libgpiod][PATCH v2 18/23] bindings: python: tests: add type stubs for external modules Date: Thu, 14 Nov 2024 08:51:11 -0600 Message-Id: <20241114145116.2123714-19-vfazio@xes-inc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241114145116.2123714-1-vfazio@xes-inc.com> References: <20241114145116.2123714-1-vfazio@xes-inc.com> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Add type stubs for the compiled external modules so that types and methods used from the modules are accurately type checked. Signed-off-by: Vincent Fazio --- bindings/python/tests/gpiosim/_ext.pyi | 21 +++++++++++++++++++++ bindings/python/tests/procname/_ext.pyi | 1 + 2 files changed, 22 insertions(+) create mode 100644 bindings/python/tests/gpiosim/_ext.pyi create mode 100644 bindings/python/tests/procname/_ext.pyi diff --git a/bindings/python/tests/gpiosim/_ext.pyi b/bindings/python/tests/gpiosim/_ext.pyi new file mode 100644 index 0000000..69d4b63 --- /dev/null +++ b/bindings/python/tests/gpiosim/_ext.pyi @@ -0,0 +1,21 @@ +class Chip: + def __init__(self) -> None: ... + def set_label(self, label: str) -> None: ... + def set_num_lines(self, num_lines: int) -> None: ... + def set_line_name(self, offset: int, name: str) -> None: ... + def set_hog(self, offset: int, name: str, direction: int) -> None: ... + def enable(self) -> None: ... + def get_value(set, offset: int) -> int: ... + def set_pull(set, offset: int, pull: int) -> None: ... + @property + def dev_path(self) -> str: ... + @property + def name(self) -> str: ... + +PULL_DOWN: int +PULL_UP: int +VALUE_INACTIVE: int +VALUE_ACTIVE: int +DIRECTION_INPUT: int +DIRECTION_OUTPUT_HIGH: int +DIRECTION_OUTPUT_LOW: int diff --git a/bindings/python/tests/procname/_ext.pyi b/bindings/python/tests/procname/_ext.pyi new file mode 100644 index 0000000..fdcd8ac --- /dev/null +++ b/bindings/python/tests/procname/_ext.pyi @@ -0,0 +1 @@ +def set_process_name(name: str) -> None: ... From patchwork Thu Nov 14 14:51:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Fazio X-Patchwork-Id: 843619 Received: from mail.xes-mad.com (mail.xes-mad.com [162.248.234.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id D1B1212FF70 for ; Thu, 14 Nov 2024 14:51:34 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=162.248.234.2 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731595896; cv=none; b=TjsSyFBhvxaMXTTJfiwjpXRFSmPaoXG7QxlNr+60RBlb4F3mAZNo6hcTgphlqcp/34RoxKhoAWPc3tkYLN6XeqVXBt8ePtDnovdImNxgiZ5KqfOwVq9Ai7Ug9dDnO8pLn3331OK97A+2AUT2T3CPlIkqrSOBnc+NTZLlRcemsCQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731595896; c=relaxed/simple; bh=PZ2PXOyR3EdAWFgjqGbH0qHNu/MYDSd0UrdD1rj8pac=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=CQKG4k6ZZ88Z+ZYAUokdCG+TetqMpiJ0jyDPebXpFqPd65Apyv0B9ew6Kwyk+6HDmmqXPGSomLGXW9jABLhFk9uvL4Am626SUz5hSZKlhBrRL/yB5ACkZ88UXfgEAcPL8D04XbpVMzKW/w0zqvTSi8sZ6EnGIK80KbBk2DTC9Aw= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=xes-inc.com; spf=pass smtp.mailfrom=xes-inc.com; dkim=pass (1024-bit key) header.d=xes-inc.com header.i=@xes-inc.com header.b=Of9jKf7a; arc=none smtp.client-ip=162.248.234.2 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=xes-inc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=xes-inc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=xes-inc.com header.i=@xes-inc.com header.b="Of9jKf7a" Received: from vfazio4.xes-mad.com (vfazio4.xes-mad.com [10.52.19.201]) by mail.xes-mad.com (Postfix) with ESMTP id 119E520ABF; Thu, 14 Nov 2024 08:51:24 -0600 (CST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=xes-inc.com; s=mail; t=1731595884; bh=PZ2PXOyR3EdAWFgjqGbH0qHNu/MYDSd0UrdD1rj8pac=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=Of9jKf7a5AGJrEJwqQCUU6TBa5L/FBdgFgOK+y/Lg8NtVDXrY4QCQH2XAxWHeG03a gQgvnhobTxFL9npAYbPgWNiEoZwHdY0nzs3YslPoIz34mb5nne5xyYb/VnkGA8z1fG EmxgeiiYBfHhwBajrvYQM3RTFdS3sWvBzzXOa2D0= From: Vincent Fazio To: linux-gpio@vger.kernel.org Cc: vfazio@gmail.com, Vincent Fazio Subject: [libgpiod][PATCH v2 22/23] bindings: python: tests: selectively use f-strings Date: Thu, 14 Nov 2024 08:51:15 -0600 Message-Id: <20241114145116.2123714-23-vfazio@xes-inc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241114145116.2123714-1-vfazio@xes-inc.com> References: <20241114145116.2123714-1-vfazio@xes-inc.com> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Since their inclusion in Python 3.6, f-strings have become the preferred way to format strings with variable values as they are generally more readable as the value substitution is in place and doesn't have to be parsed from the list or arguments to `.format()`. Where it does not impact readability (when the line is <120 characters), swap usage of `.format()` to an f-string. For lines that are not converted, inform the linter to ignore attempts to upgrade those instances to f-strings [0] [0]: https://docs.astral.sh/ruff/rules/f-string/ Signed-off-by: Vincent Fazio --- bindings/python/tests/__init__.py | 4 +--- bindings/python/tests/tests_chip.py | 8 ++++---- bindings/python/tests/tests_chip_info.py | 2 +- bindings/python/tests/tests_line_request.py | 4 +--- bindings/python/tests/tests_module.py | 4 ++-- 5 files changed, 9 insertions(+), 13 deletions(-) diff --git a/bindings/python/tests/__init__.py b/bindings/python/tests/__init__.py index 2374e81..a0f22ae 100644 --- a/bindings/python/tests/__init__.py +++ b/bindings/python/tests/__init__.py @@ -9,7 +9,5 @@ current_version = LooseVersion(os.uname().release.split("-")[0]) if current_version < required_kernel_version: raise NotImplementedError( - "linux kernel version must be at least {} - got {}".format( - required_kernel_version, current_version - ) + f"linux kernel version must be at least {required_kernel_version} - got {current_version}" ) diff --git a/bindings/python/tests/tests_chip.py b/bindings/python/tests/tests_chip.py index 9b31e30..d5a64b3 100644 --- a/bindings/python/tests/tests_chip.py +++ b/bindings/python/tests/tests_chip.py @@ -25,7 +25,7 @@ class ChipConstructor(TestCase): pass def test_open_chip_by_link(self) -> None: - link = "/tmp/gpiod-py-test-link.{}".format(os.getpid()) + link = f"/tmp/gpiod-py-test-link.{os.getpid()}" sim = gpiosim.Chip() with LinkGuard(sim.dev_path, link): @@ -94,7 +94,7 @@ class ChipProperties(TestCase): class ChipDevPathFromLink(TestCase): def test_dev_path_open_by_link(self) -> None: sim = gpiosim.Chip() - link = "/tmp/gpiod-py-test-link.{}".format(os.getpid()) + link = f"/tmp/gpiod-py-test-link.{os.getpid()}" with LinkGuard(sim.dev_path, link): with gpiod.Chip(link) as chip: @@ -203,7 +203,7 @@ class StringRepresentation(TestCase): self.sim = None # type: ignore[assignment] def test_repr(self) -> None: - self.assertEqual(repr(self.chip), 'gpiod.Chip("{}")'.format(self.sim.dev_path)) + self.assertEqual(repr(self.chip), f'gpiod.Chip("{self.sim.dev_path}")') cmp = eval(repr(self.chip)) self.assertEqual(self.chip.path, cmp.path) @@ -212,7 +212,7 @@ class StringRepresentation(TestCase): info = self.chip.get_info() self.assertEqual( str(self.chip), - '>'.format( + '>'.format( # noqa: UP032 self.sim.dev_path, self.chip.fd, info.name ), ) diff --git a/bindings/python/tests/tests_chip_info.py b/bindings/python/tests/tests_chip_info.py index fdceda9..dbb7fd0 100644 --- a/bindings/python/tests/tests_chip_info.py +++ b/bindings/python/tests/tests_chip_info.py @@ -49,5 +49,5 @@ class ChipInfoStringRepresentation(TestCase): self.assertEqual( str(info), - ''.format(sim.name), + f'', ) diff --git a/bindings/python/tests/tests_line_request.py b/bindings/python/tests/tests_line_request.py index bae8815..afee644 100644 --- a/bindings/python/tests/tests_line_request.py +++ b/bindings/python/tests/tests_line_request.py @@ -635,9 +635,7 @@ class LineRequestStringRepresentation(TestCase): with chip.request_lines(config={(2, 6, 4, 1): None}) as req: self.assertEqual( str(req), - ''.format( - self.sim.name, req.fd - ), + f'', ) def test_str_released(self) -> None: diff --git a/bindings/python/tests/tests_module.py b/bindings/python/tests/tests_module.py index efd49db..7120c63 100644 --- a/bindings/python/tests/tests_module.py +++ b/bindings/python/tests/tests_module.py @@ -36,14 +36,14 @@ class IsGPIOChip(TestCase): self.assertTrue(gpiod.is_gpiochip_device(path=sim.dev_path)) def test_is_gpiochip_link_good(self) -> None: - link = "/tmp/gpiod-py-test-link.{}".format(os.getpid()) + link = f"/tmp/gpiod-py-test-link.{os.getpid()}" sim = gpiosim.Chip() with LinkGuard(sim.dev_path, link): self.assertTrue(gpiod.is_gpiochip_device(link)) def test_is_gpiochip_link_bad(self) -> None: - link = "/tmp/gpiod-py-test-link.{}".format(os.getpid()) + link = f"/tmp/gpiod-py-test-link.{os.getpid()}" with LinkGuard("/dev/null", link): self.assertFalse(gpiod.is_gpiochip_device(link)) From patchwork Thu Nov 14 14:51:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Vincent Fazio X-Patchwork-Id: 843618 Received: from mail.xes-mad.com (mail.xes-mad.com [162.248.234.2]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5C1705336E for ; Thu, 14 Nov 2024 14:51:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=162.248.234.2 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731595896; cv=none; b=LLCNTf7OJpH/lLldtGCqtjFqsP9YoFA2mIXyGifYtEYVWhqJZDIGvAK4hw7QBi5YbjRGyd5vDOvdn2bXB4wBLUj2QnH2bP2ObsC3Ygb8bCrhLi2eq8U/lL7wDoh9S5lrXHWA8K/H+g1OdBs0rqjYn0MIk0j/1WTrWTlThQs+Qs8= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1731595896; c=relaxed/simple; bh=3buoQxfsBeu/2LtnS6PhbYb3vk35HwEBDXbocvanbXE=; h=From:To:Cc:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=BDhkUCk9jnXbDOfrNSjXhewJ1FEt45LV/9nujOSPiKXW8aS7ROq+nCeUiKbJy/PFAnjx4lCYV6lqZuc0RWVPPunIjL/0gg4d5HfU4G7pvoezJGxBJP0MMsvNwk1bS5x3LbI6V2iEgoydU29w+QWGoppn1mjZX6BThrdfkbqRlGY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=xes-inc.com; spf=pass smtp.mailfrom=xes-inc.com; dkim=pass (1024-bit key) header.d=xes-inc.com header.i=@xes-inc.com header.b=tHgznKxK; arc=none smtp.client-ip=162.248.234.2 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=xes-inc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=xes-inc.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=xes-inc.com header.i=@xes-inc.com header.b="tHgznKxK" Received: from vfazio4.xes-mad.com (vfazio4.xes-mad.com [10.52.19.201]) by mail.xes-mad.com (Postfix) with ESMTP id 2628E20AC0; Thu, 14 Nov 2024 08:51:24 -0600 (CST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=xes-inc.com; s=mail; t=1731595884; bh=3buoQxfsBeu/2LtnS6PhbYb3vk35HwEBDXbocvanbXE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=tHgznKxKKpTMJUtLE+JhLw2lb55Aa+SurYKeRHG2cZxdtf4JQQ3OH3MwyMBZ2jhZi I/Orz30JG4Cd7rcniZ9qnIKMpUFQABKFzbYist8ZRTj5D+/0iYTWJUeU3oW/M0Mjir hvA3XkCY9qPKPh2Cc3NFwfxSkzjQHCWLom4fUbr8= From: Vincent Fazio To: linux-gpio@vger.kernel.org Cc: vfazio@gmail.com, Vincent Fazio Subject: [libgpiod][PATCH v2 23/23] bindings: python: configure and document dev dependencies Date: Thu, 14 Nov 2024 08:51:16 -0600 Message-Id: <20241114145116.2123714-24-vfazio@xes-inc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20241114145116.2123714-1-vfazio@xes-inc.com> References: <20241114145116.2123714-1-vfazio@xes-inc.com> Precedence: bulk X-Mailing-List: linux-gpio@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Mypy [0] is a popular static type checker that validates attribute and variable use and ensures function arguments adhere to type annotations. Ruff [1] is a popular Rust-based Python linter and code formatter. It has support for a large set of linting rules [2] and largely complies with the Black format [3]. Add documentation to README.md for how to run the tools. [0]: https://mypy.readthedocs.io/en/stable/ [1]: https://docs.astral.sh/ruff/ [2]: https://docs.astral.sh/ruff/rules/ [3]: https://docs.astral.sh/ruff/formatter/#black-compatibility Signed-off-by: Vincent Fazio --- bindings/python/README.md | 17 ++++++++++++++++ bindings/python/pyproject.toml | 36 ++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+) diff --git a/bindings/python/README.md b/bindings/python/README.md index cb5cee6..89c824c 100644 --- a/bindings/python/README.md +++ b/bindings/python/README.md @@ -112,3 +112,20 @@ make python-tests-run from the `libgpiod/bindings/python` directory as root (necessary to be able to create the **gpio-sims** used for testing). + +## Linting/Formatting + +When making changes, ensure type checks and linting still pass: + +``` +python3 -m venv venv +. venv/bin/activate +pip install mypy ruff +mypy; ruff format; ruff check +``` + +Ideally the gpiod library will continue to pass strict checks: + +``` +mypy --strict +``` \ No newline at end of file diff --git a/bindings/python/pyproject.toml b/bindings/python/pyproject.toml index f6bf43c..d6f5f9b 100644 --- a/bindings/python/pyproject.toml +++ b/bindings/python/pyproject.toml @@ -3,3 +3,39 @@ [build-system] requires = ["setuptools", "wheel", "packaging"] + +[tool.mypy] +python_version = "3.9" +files = [ + "gpiod/", + "tests/", +] + +[[tool.mypy.overrides]] +module = "gpiod.line.*" +strict_equality = false # Ignore Enum comparison-overlap: https://github.com/python/mypy/issues/17317 + +[tool.ruff] +target-version = "py39" +include = [ + "gpiod/**/*.py", + "gpiod/**/*.pyi", + "tests/**/*.py", + "tests/**/*.pyi", +] + +[tool.ruff.lint] +select = ["B", "E", "F", "I", "TCH", "UP"] +ignore=[ + # Ignore chained exception warnings for now: https://docs.astral.sh/ruff/rules/raise-without-from-inside-except/ + "B904", + # Never enforce line length violations. Let the formatter handle it: https://docs.astral.sh/ruff/formatter/#conflicting-lint-rules + "E501", + # Ignore new Union (|) syntax until we require 3.10+ + "UP007", +] + +[tool.ruff.lint.per-file-ignores] +"gpiod/__init__.py" = ["F403", "F405"] # ignore warnings about star imports +"tests/__main__.py" = ["F403"] +"tests/**.py" = ["F841"] # ignore warnings about unused variables \ No newline at end of file