Skip to content

Use Generic to set precise type for InputDevice.path #241

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions src/evdev/device.py
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -9,6 +9,8 @@
except ImportError:
from .eventio import EvdevError, EventIO

_AnyStr = TypeVar("_AnyStr", str, bytes)


class AbsInfo(NamedTuple):
"""Absolute axis information.
Expand Down Expand Up @@ -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
---------
Expand All @@ -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:
Expand Down