i3ipc-python icon indicating copy to clipboard operation
i3ipc-python copied to clipboard

Use generic self type rather than specifying "Con"

Open Hubro opened this issue 4 years ago • 1 comments

The i3ipc.aio.connection.Con subclass overrides the command and command_children methods, but nothing else. All the find_* methods are untouched, and their type hints state: -> List['Con']. This means that even if you're in an AIO context, any type checker or language server will think the find_* methods return regular (non-AIO) containers. Any awaits on those containers will generate errors:

image

This issue is discussed in this PEP-0673: https://www.python.org/dev/peps/pep-0673/

It seems like Python 3.11 will include proper syntax for this: -> Self. However, for now, the "correct" workaround is to use a bound type variable:

TCon = TypeVar("TCon", bound="Con")

class Con:
    ...

    def find_focused(self) -> Optional[TCon]:
        ...

    def find_by_id(self, id: int) -> Optional[TCon]:
        ...

    def find_by_pid(self, id: int) -> List[TCon]:
        ...

    ...

This will cause the find_* methods on the AIO subclass to return instances of the AIO subclass rather than the parent class.

Hubro avatar Dec 22 '21 12:12 Hubro

Oh ok I think that would be a good inclusion.

acrisci avatar Dec 22 '21 18:12 acrisci