From c9ab63f79ac40c43eb7509d45db761da2ac00964 Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Sat, 15 Mar 2025 19:59:50 +0000 Subject: [PATCH 1/2] Use Generic to set precise type for InputDevice.path --- src/evdev/device.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/evdev/device.py b/src/evdev/device.py index 878a937..38cc4c6 100644 --- a/src/evdev/device.py +++ b/src/evdev/device.py @@ -1,6 +1,6 @@ import contextlib import os -from typing import Dict, Iterator, List, Literal, NamedTuple, Tuple, Union, overload +from typing import Dict, Generic, Iterator, List, Literal, NamedTuple, Tuple, TypeVar, Union, overload from . import _input, ecodes, util @@ -9,6 +9,8 @@ except ImportError: from .eventio import EvdevError, EventIO +_AnyStr = TypeVar("_AnyStr", str, bytes) + class AbsInfo(NamedTuple): """Absolute axis information. @@ -100,14 +102,14 @@ def __str__(self) -> str: return msg.format(*self) # pylint: disable=not-an-iterable -class InputDevice(EventIO): +class InputDevice(EventIO, Generic[_AnyStr]): """ A linux input device from which input events can be read. """ __slots__ = ("path", "fd", "info", "name", "phys", "uniq", "_rawcapabilities", "version", "ff_effects_count") - def __init__(self, dev: Union[str, bytes, os.PathLike]): + def __init__(self, dev: Union[_AnyStr, os.PathLike[_AnyStr]]): """ Arguments --------- @@ -116,7 +118,7 @@ def __init__(self, dev: Union[str, bytes, os.PathLike]): """ #: Path to input device. - self.path = dev if not hasattr(dev, "__fspath__") else dev.__fspath__() + self.path: _AnyStr = dev if not hasattr(dev, "__fspath__") else dev.__fspath__() # Certain operations are possible only when the device is opened in read-write mode. try: From 536835ce8cb15059a6bee7e3706fcd176dd50c14 Mon Sep 17 00:00:00 2001 From: Sam Bull Date: Sat, 15 Mar 2025 20:04:13 +0000 Subject: [PATCH 2/2] Update src/evdev/device.py --- src/evdev/device.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/evdev/device.py b/src/evdev/device.py index 38cc4c6..a7f9b92 100644 --- a/src/evdev/device.py +++ b/src/evdev/device.py @@ -109,7 +109,7 @@ class InputDevice(EventIO, Generic[_AnyStr]): __slots__ = ("path", "fd", "info", "name", "phys", "uniq", "_rawcapabilities", "version", "ff_effects_count") - def __init__(self, dev: Union[_AnyStr, os.PathLike[_AnyStr]]): + def __init__(self, dev: Union[_AnyStr, "os.PathLike[_AnyStr]"]): """ Arguments ---------