bindings: python: use quoted types when casting
PATCH 1/1 bindings: python: use quoted types when casting
Linter check T006 [0] recommends using quotes around types (making them strings) when casting values to a type to avoid the overhead of runtime type evaluation.
This provides a small performance benefit at runtime [1].
Quoted 1 line
Quoted 1 line
Quoted 1 line
timeit(lambda: cast(gpiod._ext.Request, "a"), number=100000000)
6.4817840601317585
Quoted 1 line
Quoted 1 line
Quoted 1 line
timeit(lambda: cast("gpiod._ext.Request", "a"), number=100000000)
4.369633618975058
This also allows moving some imports behind type checking guards as they are not required at runtime.
[0]: https://docs.astral.sh/ruff/rules/runtime-cast-value/
[1]: https://github.com/snok/flake8-type-checking/issues/119#issuecomment-2511022212
--- bindings/python/gpiod/chip.py | 18 +++++++++--------- bindings/python/gpiod/line_request.py | 16 ++++++++-------- 2 files changed, 17 insertions(+), 17 deletions(-)
bindings/python/gpiod/chip.py +9 -9
@@ -101,7 +101,7 @@ class Chip: longer be used after this method is called. """ self._check_closed() - cast(_ext.Chip, 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 = cast(_ext.Chip, 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 cast(_ext.Chip, 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,7 @@ class Chip: def _get_line_info(self, line: Union[int, str], watch: bool) -> LineInfo: self._check_closed() - return cast(_ext.Chip, self._chip).get_line_info( + return cast("_ext.Chip", self._chip).get_line_info( self.line_offset_from_id(line), watch )
@@ -198,7 +198,7 @@ class Chip: Offset or name of the line to stop watching. """ self._check_closed() - return cast(_ext.Chip, self._chip).unwatch_line_info( + return cast("_ext.Chip", self._chip).unwatch_line_info( self.line_offset_from_id(line) )
@@ -234,7 +234,7 @@ class Chip: This function may block if there are no available events in the queue. """ self._check_closed() - return cast(_ext.Chip, self._chip).read_info_event() + return cast("_ext.Chip", self._chip).read_info_event() def request_lines( self,
@@ -328,7 +328,7 @@ class Chip: if len(global_output_values): line_cfg.set_output_values(global_output_values) - req_internal = cast(_ext.Chip, self._chip).request_lines( + req_internal = cast("_ext.Chip", self._chip).request_lines( line_cfg, consumer, event_buffer_size ) request = LineRequest(req_internal)
@@ -374,7 +374,7 @@ class Chip: Filesystem path used to open this chip. """ self._check_closed() - return cast(_ext.Chip, self._chip).path + return cast("_ext.Chip", self._chip).path @property def fd(self) -> int:
@@ -382,4 +382,4 @@ class Chip: File descriptor associated with this chip. """ self._check_closed() - return cast(_ext.Chip, self._chip).fd + return cast("_ext.Chip", self._chip).fd
bindings/python/gpiod/line_request.py +8 -8
@@ -8,7 +8,6 @@ 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:
@@ -17,6 +16,7 @@ if TYPE_CHECKING: from types import TracebackType from .edge_event import EdgeEvent + from .line import Value __all__ = ["LineRequest"]
@@ -76,7 +76,7 @@ class LineRequest: not be used after a call to this method. """ self._check_released() - cast(_ext.Request, self._req).release() + cast("_ext.Request", self._req).release() self._req = None def get_value(self, line: Union[int, str]) -> Value:
@@ -122,9 +122,9 @@ class LineRequest: offsets = [self._line_to_offset(line) for line in lines] - buf = cast(list[Value], [None] * len(offsets)) + buf = cast("list[Value]", [None] * len(offsets)) - cast(_ext.Request, 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:
@@ -151,7 +151,7 @@ class LineRequest: mapped = {self._line_to_offset(line): value for line, value in values.items()} - cast(_ext.Request, self._req).set_values(mapped) + cast("_ext.Request", self._req).set_values(mapped) def reconfigure_lines( self,
@@ -186,7 +186,7 @@ class LineRequest: settings = line_settings.get(offset) or LineSettings() line_cfg.add_line_settings([offset], _line_settings_to_ext(settings)) - cast(_ext.Request, 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
@@ -220,7 +220,7 @@ class LineRequest: """ self._check_released() - return cast(_ext.Request, self._req).read_edge_events(max_events) + return cast("_ext.Request", self._req).read_edge_events(max_events) def fileno(self) -> int: """
@@ -276,4 +276,4 @@ class LineRequest: File descriptor associated with this request. """ self._check_released() - return cast(_ext.Request, self._req).fd + return cast("_ext.Request", self._req).fd
Signature · 1 line
2.43.0
Quoted 11 lines · Vincent Fazio
Linter check T006 [0] recommends using quotes around types (making them strings) when casting values to a type to avoid the overhead of runtime type evaluation.
This provides a small performance benefit at runtime [1].
Quoted 1 line
Quoted 1 line
Quoted 1 line
timeit(lambda: cast(gpiod._ext.Request, "a"), number=100000000)6.4817840601317585
Quoted 1 line
Quoted 1 line
Quoted 1 line
timeit(lambda: cast("gpiod._ext.Request", "a"), number=100000000)4.369633618975058
Quoted 8 lines
This also allows moving some imports behind type checking guards as they are not required at runtime.
[0]: https://docs.astral.sh/ruff/rules/runtime-cast-value/
[1]: https://github.com/snok/flake8-type-checking/issues/119#issuecomment-2511022212Signed-off-by Vincent Fazio <[email protected]>---
Hi Vincent!
This doesn't apply on top of current master, could you rebase and resend, please?
Thanks!
Bartosz