From a0f3ca2a833c00cc9c266316a155617b478e4ce4 Mon Sep 17 00:00:00 2001 From: fdev31 Date: Sun, 30 Jul 2023 19:12:21 +0200 Subject: [PATCH] more linting --- pyprland/command.py | 8 +++++-- pyprland/plugins/expose.py | 14 ++++++------ pyprland/plugins/lost_windows.py | 6 +++--- pyprland/plugins/monitors.py | 24 +++++++++++---------- pyprland/plugins/scratchpads.py | 12 ++++++----- pyprland/plugins/shift_monitors.py | 7 ++++-- pyprland/plugins/toggle_dpms.py | 4 ++-- pyprland/plugins/workspaces_follow_focus.py | 9 ++++++-- 8 files changed, 50 insertions(+), 34 deletions(-) diff --git a/pyprland/command.py b/pyprland/command.py index 3bf24ff..b70e63f 100755 --- a/pyprland/command.py +++ b/pyprland/command.py @@ -1,6 +1,7 @@ #!/bin/env python " Pyprland - an Hyprland companion app " import asyncio +from typing import cast import json import sys import os @@ -23,7 +24,7 @@ class Pyprland: event_reader: asyncio.StreamReader stopped = False name = "builtin" - config: dict[str, dict] = None + config: None | dict[str, dict] = None def __init__(self): self.plugins: dict[str, Plugin] = {} @@ -33,6 +34,7 @@ class Pyprland: """Loads the configuration if `init` is true, also initializes the plugins""" + assert isinstance(self.config, dict) try: with open(os.path.expanduser(CONFIG_FILE), encoding="utf-8") as f: self.config = json.loads(f.read()) @@ -42,7 +44,9 @@ class Pyprland: ) raise PyprError() from e - for name in self.config["pyprland"]["plugins"]: + assert self.config + + for name in cast(dict, self.config["pyprland"]["plugins"]): if name not in self.plugins: modname = name if "." in name else f"pyprland.plugins.{name}" try: diff --git a/pyprland/plugins/expose.py b/pyprland/plugins/expose.py index e68c7db..32213a5 100644 --- a/pyprland/plugins/expose.py +++ b/pyprland/plugins/expose.py @@ -1,22 +1,22 @@ """ expose Brings every client window to screen for selection toggle_minimized allows having an "expose" like selection of minimized windows """ -from typing import Any +from typing import Any, cast from .interface import Plugin from ..ipc import hyprctlJSON, hyprctl class Extension(Plugin): # pylint: disable=missing-class-docstring - exposed = False + exposed: list[dict] = [] async def run_toggle_minimized(self, special_workspace="minimized"): """[name] Toggles switching the focused window to the special workspace "name" (default: minimized)""" - aw: dict[str, Any] = await hyprctlJSON("activewindow") + aw = cast(dict, await hyprctlJSON("activewindow")) wid = aw["workspace"]["id"] assert isinstance(wid, int) if wid < 1: # special workspace: unminimize - wrk = await hyprctlJSON("activeworkspace") + wrk = cast(dict, await hyprctlJSON("activeworkspace")) await hyprctl(f"togglespecialworkspace {special_workspace}") await hyprctl(f"movetoworkspacesilent {wrk['id']},address:{aw['address']}") await hyprctl(f"focuswindow address:{aw['address']}") @@ -36,7 +36,7 @@ class Extension(Plugin): # pylint: disable=missing-class-docstring """Expose every client on the active workspace. If expose is active restores everything and move to the focused window""" if self.exposed: - aw: dict[str, Any] = await hyprctlJSON("activewindow") + aw: dict[str, Any] = cast(dict, await hyprctlJSON("activewindow")) focused_addr = aw["address"] for client in self.exposed_clients: await hyprctl( @@ -44,9 +44,9 @@ class Extension(Plugin): # pylint: disable=missing-class-docstring ) await hyprctl("togglespecialworkspace exposed") await hyprctl(f"focuswindow address:{focused_addr}") - self.exposed = False + self.exposed = [] else: - self.exposed = await hyprctlJSON("clients") + self.exposed = cast(list, await hyprctlJSON("clients")) for client in self.exposed_clients: await hyprctl( f"movetoworkspacesilent special:exposed,address:{client['address']}" diff --git a/pyprland/plugins/lost_windows.py b/pyprland/plugins/lost_windows.py index 34cbb5d..3b7c133 100644 --- a/pyprland/plugins/lost_windows.py +++ b/pyprland/plugins/lost_windows.py @@ -1,5 +1,5 @@ " Moves unreachable client windows to the currently focused workspace" -from typing import Any +from typing import Any, cast from .interface import Plugin from ..ipc import hyprctlJSON, hyprctl @@ -23,8 +23,8 @@ def contains(monitor, window): class Extension(Plugin): # pylint: disable=missing-class-docstring async def run_attract_lost(self): """Brings lost floating windows to the current workspace""" - monitors: list[dict[str, Any]] = await hyprctlJSON("monitors") - windows = await hyprctlJSON("clients") + monitors = cast(list, await hyprctlJSON("monitors")) + windows = cast(list, await hyprctlJSON("clients")) lost = [ win for win in windows diff --git a/pyprland/plugins/monitors.py b/pyprland/plugins/monitors.py index 7c51de3..da4821d 100644 --- a/pyprland/plugins/monitors.py +++ b/pyprland/plugins/monitors.py @@ -1,6 +1,6 @@ " The monitors plugin " import subprocess -from typing import Any +from typing import Any, cast from .interface import Plugin from ..ipc import hyprctlJSON @@ -40,7 +40,7 @@ def configure_monitors(monitors, screenid: str, pos_x: int, pos_y: int) -> None: class Extension(Plugin): # pylint: disable=missing-class-docstring async def load_config(self, config) -> None: await super().load_config(config) - monitors = await hyprctlJSON("monitors") + monitors = cast(list[dict], await hyprctlJSON("monitors")) for monitor in monitors: await self.event_monitoradded( monitor["name"], no_default=True, monitors=monitors @@ -53,7 +53,7 @@ class Extension(Plugin): # pylint: disable=missing-class-docstring monitor_name = monitor_name.strip() if not monitors: - monitors = await hyprctlJSON("monitors") + monitors = cast(list, await hyprctlJSON("monitors")) assert monitors @@ -85,18 +85,20 @@ class Extension(Plugin): # pylint: disable=missing-class-docstring ref = mon_by_name[other_mon_description] if ref: place = placement.lower() + x: int = 0 + y: int = 0 if place == "topof": - x: int = ref["x"] - y: int = ref["y"] - newmon["height"] + x = ref["x"] + y = ref["y"] - newmon["height"] elif place == "bottomof": - x: int = ref["x"] - y: int = ref["y"] + ref["height"] + x = ref["x"] + y = ref["y"] + ref["height"] elif place == "leftof": - x: int = ref["x"] - newmon["width"] - y: int = ref["y"] + x = ref["x"] - newmon["width"] + y = ref["y"] else: # rightof - x: int = ref["x"] + ref["width"] - y: int = ref["y"] + x = ref["x"] + ref["width"] + y = ref["y"] configure_monitors(monitors, monitor_name, x, y) return True diff --git a/pyprland/plugins/scratchpads.py b/pyprland/plugins/scratchpads.py index 80c0fd5..e4da67f 100644 --- a/pyprland/plugins/scratchpads.py +++ b/pyprland/plugins/scratchpads.py @@ -2,7 +2,7 @@ import os import asyncio import subprocess -from typing import Any +from typing import Any, cast from ..ipc import ( hyprctl, @@ -158,10 +158,10 @@ class Extension(Plugin): # pylint: disable=missing-class-docstring *(die_in_piece(scratch) for scratch in self.scratches.values()) ) - async def load_config(self, config) -> None: + async def load_config(self, config: dict[str, Any]) -> None: "config loader" - config: dict[str, dict[str, Any]] = config["scratchpads"] - scratches = {k: Scratch(k, v) for k, v in config.items()} + my_config: dict[str, dict[str, Any]] = config["scratchpads"] + scratches = {k: Scratch(k, v) for k, v in my_config.items()} new_scratches = set() @@ -347,7 +347,9 @@ class Extension(Plugin): # pylint: disable=missing-class-docstring uid = uid.strip() item = self.scratches.get(uid) - self.focused_window_tracking[uid] = await hyprctlJSON("activewindow") + self.focused_window_tracking[uid] = cast( + dict[str, Any], await hyprctlJSON("activewindow") + ) if not item: self.log.warning("%s is not configured", uid) diff --git a/pyprland/plugins/shift_monitors.py b/pyprland/plugins/shift_monitors.py index 6006857..29990da 100644 --- a/pyprland/plugins/shift_monitors.py +++ b/pyprland/plugins/shift_monitors.py @@ -1,4 +1,5 @@ " shift workspaces across monitors " +from typing import cast from .interface import Plugin from ..ipc import hyprctlJSON, hyprctl @@ -8,7 +9,9 @@ class Extension(Plugin): # pylint: disable=missing-class-docstring monitors: list[str] = [] async def init(self): - self.monitors = [mon["name"] for mon in await hyprctlJSON("monitors")] + self.monitors: list[str] = [ + mon["name"] for mon in cast(list[dict], await hyprctlJSON("monitors")) + ] async def run_shift_monitors(self, arg: str): """Swaps monitors' workspaces in the given direction""" @@ -16,7 +19,7 @@ class Extension(Plugin): # pylint: disable=missing-class-docstring if direction > 0: mon_list = self.monitors[:-1] else: - mon_list = reversed(self.monitors[1:]) + mon_list = list(reversed(self.monitors[1:])) for i, mon in enumerate(mon_list): await hyprctl(f"swapactiveworkspaces {mon} {self.monitors[i+direction]}") diff --git a/pyprland/plugins/toggle_dpms.py b/pyprland/plugins/toggle_dpms.py index 0212ed5..86c6074 100644 --- a/pyprland/plugins/toggle_dpms.py +++ b/pyprland/plugins/toggle_dpms.py @@ -1,5 +1,5 @@ " Toggle monitors on or off " -from typing import Any +from typing import Any, cast from .interface import Plugin from ..ipc import hyprctlJSON, hyprctl @@ -8,7 +8,7 @@ from ..ipc import hyprctlJSON, hyprctl class Extension(Plugin): # pylint: disable=missing-class-docstring async def run_toggle_dpms(self): """toggles dpms on/off for every monitor""" - monitors: list[dict[str, Any]] = await hyprctlJSON("monitors") + monitors = cast(list[dict[str, Any]], await hyprctlJSON("monitors")) powered_off = any(m["dpmsStatus"] for m in monitors) if not powered_off: await hyprctl("dpms on") diff --git a/pyprland/plugins/workspaces_follow_focus.py b/pyprland/plugins/workspaces_follow_focus.py index 2b3ebe0..29e2872 100644 --- a/pyprland/plugins/workspaces_follow_focus.py +++ b/pyprland/plugins/workspaces_follow_focus.py @@ -1,4 +1,5 @@ """ Force workspaces to follow the focus / mouse """ +from typing import cast from .interface import Plugin from ..ipc import hyprctlJSON, hyprctl @@ -19,10 +20,14 @@ class Extension(Plugin): # pylint: disable=missing-class-docstring # move every free workspace to the currently focused desktop busy_workspaces = set( mon["activeWorkspace"]["id"] - for mon in await hyprctlJSON("monitors") + for mon in cast(list[dict], await hyprctlJSON("monitors")) if mon["name"] != monitor_id ) - workspaces = [w["id"] for w in await hyprctlJSON("workspaces") if w["id"] > 0] + workspaces = [ + w["id"] + for w in cast(list[dict], await hyprctlJSON("workspaces")) + if w["id"] > 0 + ] batch: list[str | list[str]] = [] for n in workspaces: