Skip to content

Commit 8bbc9ae

Browse files
Use Generic to set precise type for InputDevice.path (#241)
* Use Generic to set precise type for InputDevice.path * Update src/evdev/device.py
1 parent b917479 commit 8bbc9ae

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/evdev/device.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import contextlib
22
import os
3-
from typing import Dict, Iterator, List, Literal, NamedTuple, Tuple, Union, overload
3+
from typing import Dict, Generic, Iterator, List, Literal, NamedTuple, Tuple, TypeVar, Union, overload
44

55
from . import _input, ecodes, util
66

@@ -9,6 +9,8 @@
99
except ImportError:
1010
from .eventio import EvdevError, EventIO
1111

12+
_AnyStr = TypeVar("_AnyStr", str, bytes)
13+
1214

1315
class AbsInfo(NamedTuple):
1416
"""Absolute axis information.
@@ -100,14 +102,14 @@ def __str__(self) -> str:
100102
return msg.format(*self) # pylint: disable=not-an-iterable
101103

102104

103-
class InputDevice(EventIO):
105+
class InputDevice(EventIO, Generic[_AnyStr]):
104106
"""
105107
A linux input device from which input events can be read.
106108
"""
107109

108110
__slots__ = ("path", "fd", "info", "name", "phys", "uniq", "_rawcapabilities", "version", "ff_effects_count")
109111

110-
def __init__(self, dev: Union[str, bytes, os.PathLike]):
112+
def __init__(self, dev: Union[_AnyStr, "os.PathLike[_AnyStr]"]):
111113
"""
112114
Arguments
113115
---------
@@ -116,7 +118,7 @@ def __init__(self, dev: Union[str, bytes, os.PathLike]):
116118
"""
117119

118120
#: Path to input device.
119-
self.path = dev if not hasattr(dev, "__fspath__") else dev.__fspath__()
121+
self.path: _AnyStr = dev if not hasattr(dev, "__fspath__") else dev.__fspath__()
120122

121123
# Certain operations are possible only when the device is opened in read-write mode.
122124
try:

0 commit comments

Comments
 (0)