Skip to content

Commit

Permalink
[i3] Use generic self type variable to avoid type check errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ttrei committed Nov 30, 2022
1 parent 14a1c52 commit 2f73079
Show file tree
Hide file tree
Showing 2 changed files with 193 additions and 27 deletions.
182 changes: 173 additions & 9 deletions i3/i3init/typings.patch
Original file line number Diff line number Diff line change
@@ -1,16 +1,30 @@
diff -ruN typings/i3ipc/aio/__init__.pyi typings.patched/i3ipc/aio/__init__.pyi
--- typings/i3ipc/aio/__init__.pyi 2022-11-30 21:30:26.454203922 +0200
+++ typings.patched/i3ipc/aio/__init__.pyi 2022-11-30 21:30:25.458199758 +0200
--- typings/i3ipc/aio/__init__.pyi 2022-11-30 22:29:39.738894713 +0200
+++ typings.patched/i3ipc/aio/__init__.pyi 2022-11-30 22:29:38.730890954 +0200
@@ -3,3 +3,5 @@
"""

from .connection import Con, Connection
+
+__all__ = ["Con", "Connection"]
diff -ruN typings/i3ipc/con.pyi typings.patched/i3ipc/con.pyi
--- typings/i3ipc/con.pyi 2022-11-30 21:30:26.474204005 +0200
+++ typings.patched/i3ipc/con.pyi 2022-11-30 21:30:25.458199758 +0200
@@ -86,6 +86,11 @@
--- typings/i3ipc/con.pyi 2022-11-30 22:29:39.766894818 +0200
+++ typings.patched/i3ipc/con.pyi 2022-11-30 22:29:38.730890954 +0200
@@ -2,10 +2,12 @@
This type stub file was generated by pyright.
"""

-from typing import List, Optional
+from typing import List, Optional, TypeVar

from . import replies

+TCon = TypeVar("TCon", bound="Con")
+
class Con:
"""A container of a window and child containers gotten from :func:`i3ipc.Connection.get_tree()` or events.

@@ -86,6 +88,11 @@
:vartype ipc_data: dict
"""

Expand All @@ -22,9 +36,159 @@ diff -ruN typings/i3ipc/con.pyi typings.patched/i3ipc/con.pyi
def __init__(self, data, parent, conn) -> None: ...
def __iter__(self): # -> Generator[Unknown, None, None]:
"""Iterate through the descendents of this node (breadth-first tree traversal)"""
@@ -97,14 +104,14 @@
:rtype: bool
"""
...
- def root(self) -> Con:
+ def root(self: TCon) -> TCon:
"""Gets the root container.

:returns: The root container.
:rtype: :class:`Con`
"""
...
- def descendants(self) -> List[Con]:
+ def descendants(self: TCon) -> List[TCon]:
"""Gets a list of all child containers for the container in
breadth-first order.

@@ -112,7 +119,7 @@
:rtype: list(:class:`Con`)
"""
...
- def descendents(self) -> List[Con]:
+ def descendents(self: TCon) -> List[TCon]:
"""Gets a list of all child containers for the container in
breadth-first order.

@@ -123,7 +130,7 @@
:rtype: list(:class:`Con`)
"""
...
- def leaves(self) -> List[Con]:
+ def leaves(self: TCon) -> List[TCon]:
"""Gets a list of leaf child containers for this container in
breadth-first order. Leaf containers normally contain application
windows.
@@ -152,14 +159,14 @@
:rtype: list(:class:`CommandReply <i3ipc.CommandReply>`)
"""
...
- def workspaces(self) -> List[Con]:
+ def workspaces(self: TCon) -> List[TCon]:
"""Gets a list of workspace containers for this tree.

:returns: A list of workspace containers.
:rtype: list(:class:`Con`)
"""
...
- def find_focused(self) -> Optional[Con]:
+ def find_focused(self: TCon) -> Optional[TCon]:
"""Finds the focused container under this container if it exists.

:returns: The focused container if it exists.
@@ -167,7 +174,7 @@
under this container
"""
...
- def find_by_id(self, id: int) -> Optional[Con]:
+ def find_by_id(self: TCon, id: int) -> Optional[TCon]:
"""Finds a container with the given container id under this node.

:returns: The container with this container id if it exists.
@@ -175,14 +182,14 @@
this container id.
"""
...
- def find_by_pid(self, pid: int) -> List[Con]:
+ def find_by_pid(self: TCon, pid: int) -> List[TCon]:
"""Finds all the containers under this node with this pid.

:returns: A list of containers with this pid.
:rtype: list(:class:`Con`)
"""
...
- def find_by_window(self, window: int) -> Optional[Con]:
+ def find_by_window(self: TCon, window: int) -> Optional[TCon]:
"""Finds a container with the given window id under this node.

:returns: The container with this window id if it exists.
@@ -190,7 +197,7 @@
this window id.
"""
...
- def find_by_role(self, pattern: str) -> List[Con]:
+ def find_by_role(self: TCon, pattern: str) -> List[TCon]:
"""Finds all the containers under this node with a window role that
matches the given regex pattern.

@@ -199,7 +206,7 @@
:rtype: list(:class:`Con`)
"""
...
- def find_named(self, pattern: str) -> List[Con]:
+ def find_named(self: TCon, pattern: str) -> List[TCon]:
"""Finds all the containers under this node with a name that
matches the given regex pattern.

@@ -208,7 +215,7 @@
:rtype: list(:class:`Con`)
"""
...
- def find_titled(self, pattern: str) -> List[Con]:
+ def find_titled(self: TCon, pattern: str) -> List[TCon]:
"""Finds all the containers under this node with a window title that
matches the given regex pattern.

@@ -217,7 +224,7 @@
:rtype: list(:class:`Con`)
"""
...
- def find_classed(self, pattern: str) -> List[Con]:
+ def find_classed(self: TCon, pattern: str) -> List[TCon]:
"""Finds all the containers under this node with a window class,
or app_id that matches the given regex pattern.

@@ -226,7 +233,7 @@
:rtype: list(:class:`Con`)
"""
...
- def find_instanced(self, pattern: str) -> List[Con]:
+ def find_instanced(self: TCon, pattern: str) -> List[TCon]:
"""Finds all the containers under this node with a window instance that
matches the given regex pattern.

@@ -235,7 +242,7 @@
:rtype: list(:class:`Con`)
"""
...
- def find_marked(self, pattern: str = ...) -> List[Con]:
+ def find_marked(self: TCon, pattern: str = ...) -> List[TCon]:
"""Finds all the containers under this node with a mark that
matches the given regex pattern.

@@ -244,7 +251,7 @@
:rtype: list(:class:`Con`)
"""
...
- def find_fullscreen(self) -> List[Con]:
+ def find_fullscreen(self: TCon) -> List[TCon]:
"""Finds all the containers under this node that are in fullscreen
mode.

@@ -252,7 +259,7 @@
:rtype: list(:class:`Con`)
"""
...
- def workspace(self) -> Optional[Con]:
+ def workspace(self: TCon) -> Optional[TCon]:
"""Finds the workspace container for this node if this container is at
or below the workspace level.

diff -ruN typings/i3ipc/events.pyi typings.patched/i3ipc/events.pyi
--- typings/i3ipc/events.pyi 2022-11-30 21:30:26.478204021 +0200
+++ typings.patched/i3ipc/events.pyi 2022-11-30 21:30:25.458199758 +0200
--- typings/i3ipc/events.pyi 2022-11-30 22:29:39.762894800 +0200
+++ typings.patched/i3ipc/events.pyi 2022-11-30 22:29:38.730890954 +0200
@@ -4,6 +4,7 @@

from enum import Enum
Expand All @@ -42,8 +206,8 @@ diff -ruN typings/i3ipc/events.pyi typings.patched/i3ipc/events.pyi

class BarconfigUpdateEvent(IpcBaseEvent, BarConfigReply):
diff -ruN typings/i3ipc/__init__.pyi typings.patched/i3ipc/__init__.pyi
--- typings/i3ipc/__init__.pyi 2022-11-30 21:30:26.462203956 +0200
+++ typings.patched/i3ipc/__init__.pyi 2022-11-30 21:30:25.458199758 +0200
--- typings/i3ipc/__init__.pyi 2022-11-30 22:29:39.742894727 +0200
+++ typings.patched/i3ipc/__init__.pyi 2022-11-30 22:29:38.730890954 +0200
@@ -2,16 +2,6 @@
This type stub file was generated by pyright.
"""
Expand Down
38 changes: 20 additions & 18 deletions i3/i3init/typings/i3ipc/con.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,12 @@
This type stub file was generated by pyright.
"""

from typing import List, Optional
from typing import List, Optional, TypeVar

from . import replies

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

class Con:
"""A container of a window and child containers gotten from :func:`i3ipc.Connection.get_tree()` or events.
Expand Down Expand Up @@ -102,22 +104,22 @@ class Con:
:rtype: bool
"""
...
def root(self) -> Con:
def root(self: TCon) -> TCon:
"""Gets the root container.
:returns: The root container.
:rtype: :class:`Con`
"""
...
def descendants(self) -> List[Con]:
def descendants(self: TCon) -> List[TCon]:
"""Gets a list of all child containers for the container in
breadth-first order.
:returns: A list of descendants.
:rtype: list(:class:`Con`)
"""
...
def descendents(self) -> List[Con]:
def descendents(self: TCon) -> List[TCon]:
"""Gets a list of all child containers for the container in
breadth-first order.
Expand All @@ -128,7 +130,7 @@ class Con:
:rtype: list(:class:`Con`)
"""
...
def leaves(self) -> List[Con]:
def leaves(self: TCon) -> List[TCon]:
"""Gets a list of leaf child containers for this container in
breadth-first order. Leaf containers normally contain application
windows.
Expand Down Expand Up @@ -157,45 +159,45 @@ class Con:
:rtype: list(:class:`CommandReply <i3ipc.CommandReply>`)
"""
...
def workspaces(self) -> List[Con]:
def workspaces(self: TCon) -> List[TCon]:
"""Gets a list of workspace containers for this tree.
:returns: A list of workspace containers.
:rtype: list(:class:`Con`)
"""
...
def find_focused(self) -> Optional[Con]:
def find_focused(self: TCon) -> Optional[TCon]:
"""Finds the focused container under this container if it exists.
:returns: The focused container if it exists.
:rtype: :class:`Con` or :class:`None` if the focused container is not
under this container
"""
...
def find_by_id(self, id: int) -> Optional[Con]:
def find_by_id(self: TCon, id: int) -> Optional[TCon]:
"""Finds a container with the given container id under this node.
:returns: The container with this container id if it exists.
:rtype: :class:`Con` or :class:`None` if there is no container with
this container id.
"""
...
def find_by_pid(self, pid: int) -> List[Con]:
def find_by_pid(self: TCon, pid: int) -> List[TCon]:
"""Finds all the containers under this node with this pid.
:returns: A list of containers with this pid.
:rtype: list(:class:`Con`)
"""
...
def find_by_window(self, window: int) -> Optional[Con]:
def find_by_window(self: TCon, window: int) -> Optional[TCon]:
"""Finds a container with the given window id under this node.
:returns: The container with this window id if it exists.
:rtype: :class:`Con` or :class:`None` if there is no container with
this window id.
"""
...
def find_by_role(self, pattern: str) -> List[Con]:
def find_by_role(self: TCon, pattern: str) -> List[TCon]:
"""Finds all the containers under this node with a window role that
matches the given regex pattern.
Expand All @@ -204,7 +206,7 @@ class Con:
:rtype: list(:class:`Con`)
"""
...
def find_named(self, pattern: str) -> List[Con]:
def find_named(self: TCon, pattern: str) -> List[TCon]:
"""Finds all the containers under this node with a name that
matches the given regex pattern.
Expand All @@ -213,7 +215,7 @@ class Con:
:rtype: list(:class:`Con`)
"""
...
def find_titled(self, pattern: str) -> List[Con]:
def find_titled(self: TCon, pattern: str) -> List[TCon]:
"""Finds all the containers under this node with a window title that
matches the given regex pattern.
Expand All @@ -222,7 +224,7 @@ class Con:
:rtype: list(:class:`Con`)
"""
...
def find_classed(self, pattern: str) -> List[Con]:
def find_classed(self: TCon, pattern: str) -> List[TCon]:
"""Finds all the containers under this node with a window class,
or app_id that matches the given regex pattern.
Expand All @@ -231,7 +233,7 @@ class Con:
:rtype: list(:class:`Con`)
"""
...
def find_instanced(self, pattern: str) -> List[Con]:
def find_instanced(self: TCon, pattern: str) -> List[TCon]:
"""Finds all the containers under this node with a window instance that
matches the given regex pattern.
Expand All @@ -240,7 +242,7 @@ class Con:
:rtype: list(:class:`Con`)
"""
...
def find_marked(self, pattern: str = ...) -> List[Con]:
def find_marked(self: TCon, pattern: str = ...) -> List[TCon]:
"""Finds all the containers under this node with a mark that
matches the given regex pattern.
Expand All @@ -249,15 +251,15 @@ class Con:
:rtype: list(:class:`Con`)
"""
...
def find_fullscreen(self) -> List[Con]:
def find_fullscreen(self: TCon) -> List[TCon]:
"""Finds all the containers under this node that are in fullscreen
mode.
:returns: A list of fullscreen containers.
:rtype: list(:class:`Con`)
"""
...
def workspace(self) -> Optional[Con]:
def workspace(self: TCon) -> Optional[TCon]:
"""Finds the workspace container for this node if this container is at
or below the workspace level.
Expand Down

0 comments on commit 2f73079

Please sign in to comment.