Explorar o código

fix(yabai): improve `yabai.py` type annotations

Joe hai 1 ano
pai
achega
e303fa0b38
Modificáronse 1 ficheiros con 25 adicións e 15 borrados
  1. 25 15
      .config/yabai/yabai.py

+ 25 - 15
.config/yabai/yabai.py

@@ -4,15 +4,16 @@ from logging import NOTSET, basicConfig, debug, info
 from os import getenv
 from os import getenv
 from subprocess import CalledProcessError, check_output
 from subprocess import CalledProcessError, check_output
 from sys import argv
 from sys import argv
-from typing import TypeAlias
+from typing import Any, TypeAlias
 
 
 HOME = getenv("HOME")
 HOME = getenv("HOME")
 XDG_CONFIG_HOME = getenv("XDG_CONFIG_HOME")
 XDG_CONFIG_HOME = getenv("XDG_CONFIG_HOME")
 TARGET_DISPLAY_WIDTH = 1680
 TARGET_DISPLAY_WIDTH = 1680
 TARGET_DISPLAY_HEIGHT = 1050
 TARGET_DISPLAY_HEIGHT = 1050
 
 
+
 class Window:
 class Window:
-    def __init__(self, data: dict):
+    def __init__(self, data: dict[str, Any]):
         self.id: int = data["id"]
         self.id: int = data["id"]
         self.app: str = data["app"]
         self.app: str = data["app"]
         self.space: str = data["space"]
         self.space: str = data["space"]
@@ -34,10 +35,10 @@ class Window:
 
 
     def __gt__(self, other: "Window"):
     def __gt__(self, other: "Window"):
         return self.id.__gt__(other.id)
         return self.id.__gt__(other.id)
- 
+
 
 
 class Space:
 class Space:
-    def __init__(self, data: dict):
+    def __init__(self, data: dict[str, Any]):
         self.id: int = data["id"]
         self.id: int = data["id"]
         self.index: int = data["index"]
         self.index: int = data["index"]
         self.label: str = data["label"]
         self.label: str = data["label"]
@@ -58,14 +59,14 @@ class Space:
 
 
 
 
 class Display:
 class Display:
-    def __init__(self, data: dict):
+    def __init__(self, data: dict[str, Any]):
         self.id: int = data["id"]
         self.id: int = data["id"]
         self.uuid: str = data["uuid"]
         self.uuid: str = data["uuid"]
         self.index: int = data["index"]
         self.index: int = data["index"]
         self.label: str = data["label"]
         self.label: str = data["label"]
         self.has_focus: bool = data["has-focus"]
         self.has_focus: bool = data["has-focus"]
         self.spaces: list[str] = data["spaces"]
         self.spaces: list[str] = data["spaces"]
-        self.frame: dict = data["frame"]
+        self.frame: dict[str, int] = data["frame"]
 
 
     def __repr__(self) -> str:
     def __repr__(self) -> str:
         return (
         return (
@@ -97,15 +98,16 @@ class Yabai:
         self._initial_window = self.get_focused_window()
         self._initial_window = self.get_focused_window()
         return self
         return self
 
 
-    def __exit__(self, exc_type, exc_value, traceback):
+    def __exit__(self, exc_type: Any, exc_value: Any, traceback: Any):
         if exc_type is not None:
         if exc_type is not None:
             debug(f"Exited with {exc_type} {exc_value}")
             debug(f"Exited with {exc_type} {exc_value}")
         self.message(["rule", "--apply"])
         self.message(["rule", "--apply"])
-        self.message(["window", "--focus", self._initial_window])
+        if self._initial_window is not None:
+            self.message(["window", "--focus", self._initial_window])
         if exc_type is None:
         if exc_type is None:
             debug(f"Executed successfully")
             debug(f"Executed successfully")
 
 
-    def execute(self, *args, **kwargs):
+    def execute(self, *args: Any, **kwargs: Any) -> Any:
         debug(f"Executing: ({args}), ({kwargs})")
         debug(f"Executing: ({args}), ({kwargs})")
         return check_output(*args, **kwargs)
         return check_output(*args, **kwargs)
 
 
@@ -113,13 +115,17 @@ class Yabai:
         return self.execute(["yabai", "-m"] + [str(arg) for arg in args]).decode("utf8")
         return self.execute(["yabai", "-m"] + [str(arg) for arg in args]).decode("utf8")
 
 
     def get_windows(self) -> set[Window]:
     def get_windows(self) -> set[Window]:
-        return { Window(window) for window in loads(self.message(["query", "--windows"])) }
+        return {
+            Window(window) for window in loads(self.message(["query", "--windows"]))
+        }
 
 
     def get_spaces(self) -> set[Space]:
     def get_spaces(self) -> set[Space]:
         return {Space(space) for space in loads(self.message(["query", "--spaces"]))}
         return {Space(space) for space in loads(self.message(["query", "--spaces"]))}
 
 
     def get_displays(self) -> set[Display]:
     def get_displays(self) -> set[Display]:
-        return { Display(display) for display in loads(self.message(["query", "--displays"])) }
+        return {
+            Display(display) for display in loads(self.message(["query", "--displays"]))
+        }
 
 
     def get_main_display(self, displays: set[Display] | None = None) -> Display:
     def get_main_display(self, displays: set[Display] | None = None) -> Display:
         return sorted(list(displays if displays != None else self.get_displays()))[-1]
         return sorted(list(displays if displays != None else self.get_displays()))[-1]
@@ -159,7 +165,7 @@ class Yabai:
         spaces = self.get_spaces()
         spaces = self.get_spaces()
         blank_spaces = {space for space in spaces if self.is_blank_space(space)}
         blank_spaces = {space for space in spaces if self.is_blank_space(space)}
         created_space_labels: set[tuple[str, bool]] = set()
         created_space_labels: set[tuple[str, bool]] = set()
-        for space_label, space_fullscreen, space_apps in self.spaces:
+        for space_label, space_fullscreen, _ in self.spaces:
             if any(space.label == space_label for space in spaces):
             if any(space.label == space_label for space in spaces):
                 continue
                 continue
             space = blank_spaces.pop()
             space = blank_spaces.pop()
@@ -178,12 +184,16 @@ class Yabai:
         main_display = self.get_main_display()
         main_display = self.get_main_display()
         for label, fullscreen in created_space_labels:
         for label, fullscreen in created_space_labels:
             self.set_space_background(label)
             self.set_space_background(label)
-        for label, fullscreen, apps in self.spaces:
+        for label, fullscreen, _ in self.spaces:
             self.set_config(
             self.set_config(
                 label,
                 label,
                 fullscreen=fullscreen,
                 fullscreen=fullscreen,
-                horizontal_padding=(main_display.frame["w"] - TARGET_DISPLAY_WIDTH) / 2,
-                vertical_padding=(main_display.frame["h"] - TARGET_DISPLAY_HEIGHT) / 2,
+                horizontal_padding=int(
+                    (main_display.frame["w"] - TARGET_DISPLAY_WIDTH) / 2
+                ),
+                vertical_padding=int(
+                    (main_display.frame["h"] - TARGET_DISPLAY_HEIGHT) / 2
+                ),
             )
             )
         if len(created_space_labels) > 0:
         if len(created_space_labels) > 0:
             self.message(["window", "--focus", initial_window])
             self.message(["window", "--focus", initial_window])