bindings: python: clean up imports and exports
authorVincent Fazio <vfazio@xes-inc.com>
Thu, 14 Nov 2024 14:50:54 +0000 (08:50 -0600)
committerBartosz Golaszewski <bartosz.golaszewski@linaro.org>
Tue, 19 Nov 2024 14:21:01 +0000 (15:21 +0100)
Remove unused imports and sort the remainder following isort rules.

Update submodules to use lists for `__all__` for ease of re-exporting
public classes from within gpiod.

Place imports used only for the purposes of type checking behind a
TYPE_CHECKING guard.

Signed-off-by: Vincent Fazio <vfazio@xes-inc.com>
Link: https://lore.kernel.org/r/20241114145116.2123714-2-vfazio@xes-inc.com
Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
bindings/python/gpiod/__init__.py
bindings/python/gpiod/chip.py
bindings/python/gpiod/chip_info.py
bindings/python/gpiod/edge_event.py
bindings/python/gpiod/info_event.py
bindings/python/gpiod/internal.py
bindings/python/gpiod/line.py
bindings/python/gpiod/line_info.py
bindings/python/gpiod/line_request.py
bindings/python/gpiod/line_settings.py

index 9cbb8dfb7b7b8aff4b2f6429fd831d582058c1b6..aaa0474f79c557ec71d492cb82af459a32de9606 100644 (file)
@@ -7,19 +7,66 @@ Python bindings for libgpiod.
 This module wraps the native C API of libgpiod in a set of python classes.
 """
 
-from . import _ext
-from . import line
-from .chip import Chip
-from .chip_info import ChipInfo
-from .edge_event import EdgeEvent
-from .exception import ChipClosedError, RequestReleasedError
-from .info_event import InfoEvent
-from .line_request import LineRequest
-from .line_settings import LineSettings
+from . import (
+    _ext,
+    chip,
+    chip_info,
+    edge_event,
+    exception,
+    info_event,
+    line,
+    line_info,
+    line_request,
+    line_settings,
+    version,
+)
+from .chip import *
+from .chip_info import *
+from .edge_event import *
+from .exception import *
+from .info_event import *
+from .line_info import *
+from .line_request import *
+from .line_settings import *
 from .version import __version__
 
 api_version = _ext.api_version
 
+# public submodules
+__all__ = [
+    "chip",
+    "chip_info",
+    "edge_event",
+    "exception",
+    "info_event",
+    "line",
+    "line_info",
+    "line_request",
+    "line_settings",
+    "version",
+]
+
+# re-export public submodule exports
+# do not re-export line objects, this is not an oversight
+__all__ += (
+    chip.__all__
+    + chip_info.__all__
+    + edge_event.__all__
+    + exception.__all__
+    + info_event.__all__
+    + line_info.__all__
+    + line_request.__all__
+    + line_settings.__all__
+)
+
+# module methods/attributes
+__all__ += [
+    "__version__",
+    "api_version",
+    "is_gpiochip_device",
+    "request_lines",
+]
+
 
 def is_gpiochip_device(path: str) -> bool:
     """
index 92a73149c560fba2510f8cb24075175dcd5412be..29c30f5dffbfdf7d478204170deec03bbdc1f305 100644 (file)
@@ -1,22 +1,25 @@
 # SPDX-License-Identifier: LGPL-2.1-or-later
 # SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <brgl@bgdev.pl>
 
+from collections import Counter
+from errno import ENOENT
+from typing import TYPE_CHECKING, Optional, Union
+
 from . import _ext
-from .chip_info import ChipInfo
 from .exception import ChipClosedError
-from .info_event import InfoEvent
 from .internal import poll_fd
 from .line import Value
-from .line_info import LineInfo
-from .line_settings import LineSettings, _line_settings_to_ext
 from .line_request import LineRequest
-from collections import Counter
-from collections.abc import Iterable
-from datetime import timedelta
-from errno import ENOENT
-from typing import Union, Optional
+from .line_settings import LineSettings, _line_settings_to_ext
+
+if TYPE_CHECKING:
+    from datetime import timedelta
+
+    from .chip_info import ChipInfo
+    from .info_event import InfoEvent
+    from .line_info import LineInfo
 
-__all__ = "Chip"
+__all__ = ["Chip"]
 
 
 class Chip:
index 92b5e6f23c7117eaaa3e73ed27305116de7b0af2..884b910681abbc2069673669539d068a93f6aa72 100644 (file)
@@ -4,7 +4,7 @@
 
 from dataclasses import dataclass
 
-__all__ = "ChipInfo"
+__all__ = ["ChipInfo"]
 
 
 @dataclass(frozen=True, repr=False)
index bf258c1472abc7b0faa40ed6533c68cefcd85b6b..a8b2378f9e3a9bdfabd8dde60d5c30fc73766f4c 100644 (file)
@@ -1,11 +1,12 @@
 # SPDX-License-Identifier: LGPL-2.1-or-later
 # SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <brgl@bgdev.pl>
 
-from . import _ext
 from dataclasses import dataclass
 from enum import Enum
 
-__all__ = "EdgeEvent"
+from . import _ext
+
+__all__ = ["EdgeEvent"]
 
 
 @dataclass(frozen=True, init=False, repr=False)
index 481eae6c376bc6cb418e03be84511b0de811ff91..7b544aa6436b34613a71ee06d9b675a63ad16989 100644 (file)
@@ -1,12 +1,13 @@
 # SPDX-License-Identifier: LGPL-2.1-or-later
 # SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <brgl@bgdev.pl>
 
-from . import _ext
-from .line_info import LineInfo
 from dataclasses import dataclass
 from enum import Enum
 
-__all__ = "InfoEvent"
+from . import _ext
+from .line_info import LineInfo
+
+__all__ = ["InfoEvent"]
 
 
 @dataclass(frozen=True, init=False, repr=False)
index 2dddb65027ab6a3f8b590a65050203d3189fb8c8..d1e95e4ade3146f596643d52207b367e332e6f7e 100644 (file)
@@ -5,7 +5,7 @@ from datetime import timedelta
 from select import select
 from typing import Optional, Union
 
-__all__ = []
+__all__ = ["poll_fd"]
 
 
 def poll_fd(fd: int, timeout: Optional[Union[timedelta, float]] = None) -> bool:
index d088fb4631cc9f2bf102b6fe9a6ed5dad1eb4420..828385cbd84a95f207e808fe77022caad4056916 100644 (file)
@@ -2,9 +2,10 @@
 # SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <brgl@bgdev.pl>
 
 
-from . import _ext
 from enum import Enum
 
+from . import _ext
+
 __all__ = ["Value", "Direction", "Bias", "Drive", "Edge", "Clock"]
 
 
index c196a6aedeb48223ff5aec27b415c3ac690a84b1..46e16533802e9c8ff57a697e5b51b8b028d0c061 100644 (file)
@@ -1,12 +1,12 @@
 # SPDX-License-Identifier: LGPL-2.1-or-later
 # SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <brgl@bgdev.pl>
 
-from . import _ext
 from dataclasses import dataclass
 from datetime import timedelta
-from gpiod.line import Direction, Bias, Drive, Edge, Clock
 
-__all__ = "LineInfo"
+from .line import Bias, Clock, Direction, Drive, Edge
+
+__all__ = ["LineInfo"]
 
 
 @dataclass(frozen=True, init=False, repr=False)
index 51e600a2ee5854f11f12d41ef561ec7bcdeb5908..292fa1b5fb477fa5d3b65ac2cfdb6485c35c5bee 100644 (file)
@@ -1,17 +1,23 @@
 # SPDX-License-Identifier: LGPL-2.1-or-later
 # SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <brgl@bgdev.pl>
 
+from typing import TYPE_CHECKING, Optional, Union
+
 from . import _ext
-from .edge_event import EdgeEvent
+
 from .exception import RequestReleasedError
 from .internal import poll_fd
-from .line import Value
 from .line_settings import LineSettings, _line_settings_to_ext
-from collections.abc import Iterable
-from datetime import timedelta
-from typing import Optional, Union
 
-__all__ = "LineRequest"
+if TYPE_CHECKING:
+    from collections.abc import Iterable
+    from datetime import timedelta
+
+    from .edge_event import EdgeEvent
+    from .line import Value
+
+
+__all__ = ["LineRequest"]
 
 
 class LineRequest:
index 5e3219438c2812c449d4da84a97ebc420f2b2352..f2811b288f4e832802217e9249a71a4db0eb1a2d 100644 (file)
@@ -1,12 +1,13 @@
 # SPDX-License-Identifier: LGPL-2.1-or-later
 # SPDX-FileCopyrightText: 2022 Bartosz Golaszewski <brgl@bgdev.pl>
 
-from . import _ext
 from dataclasses import dataclass
 from datetime import timedelta
-from gpiod.line import Direction, Bias, Drive, Edge, Clock, Value
 
-__all__ = "LineSettings"
+from . import _ext
+from .line import Bias, Clock, Direction, Drive, Edge, Value
+
+__all__ = ["LineSettings"]
 
 
 @dataclass(repr=False)