From b09f500f78f1f6e36f91574a9bc3e8161655f972 Mon Sep 17 00:00:00 2001 From: Kalmat Date: Wed, 1 Jul 2026 10:23:22 +0200 Subject: [PATCH 1/2] Type handle variable --- src/pywinbox/_pywinbox_linux.py | 17 +++++++++++------ src/pywinbox/_pywinbox_macos.py | 13 +++++++++---- src/pywinbox/_pywinbox_win.py | 17 +++++++++++------ 3 files changed, 31 insertions(+), 16 deletions(-) diff --git a/src/pywinbox/_pywinbox_linux.py b/src/pywinbox/_pywinbox_linux.py index c97180d..f902b97 100644 --- a/src/pywinbox/_pywinbox_linux.py +++ b/src/pywinbox/_pywinbox_linux.py @@ -4,6 +4,8 @@ import sys import os +from ._main import Box + try: from typing import TypeAlias except Exception: @@ -12,12 +14,15 @@ from typing_extensions import TypeAlias from typing import Union -from Xlib.xobject.drawable import Window as XWindow - -from ._main import Box -from ewmhlib import EwmhWindow - -assert sys.platform == "linux" +try: + from Xlib.xobject.drawable import Window as XWindow + from ewmhlib import EwmhWindow +except Exception: + # This raises the OS exception + assert sys.platform == "linux" + # This raises Xlib/ewmhlib not installed exception (when OS is correct) + from Xlib.xobject.drawable import Window as XWindow + from ewmhlib import EwmhWindow _HandleTypeIn: TypeAlias = Union[int, XWindow, None] diff --git a/src/pywinbox/_pywinbox_macos.py b/src/pywinbox/_pywinbox_macos.py index a32c2ba..38aee5e 100644 --- a/src/pywinbox/_pywinbox_macos.py +++ b/src/pywinbox/_pywinbox_macos.py @@ -6,6 +6,8 @@ import subprocess import sys +from ._main import Box + try: from typing import TypeAlias except Exception: @@ -14,10 +16,13 @@ from typing_extensions import TypeAlias from typing import NamedTuple, cast, Union -from ._main import Box -import AppKit - -assert sys.platform == "darwin" +try: + import AppKit +except Exception: + # This raises the OS exception + assert sys.platform == "darwin" + # This raises AppKit not installed exception (when OS is correct) + import AppKit class _macOSNSHandle(NamedTuple): diff --git a/src/pywinbox/_pywinbox_win.py b/src/pywinbox/_pywinbox_win.py index d1c713b..8af275c 100644 --- a/src/pywinbox/_pywinbox_win.py +++ b/src/pywinbox/_pywinbox_win.py @@ -3,6 +3,10 @@ import sys +import ctypes + +from ._main import Box + try: from typing import TypeAlias except Exception: @@ -11,12 +15,13 @@ from typing_extensions import TypeAlias from typing import Union -import ctypes -import win32gui - -from ._main import Box - -assert sys.platform == "win32" +try: + import win32gui +except Exception: + # This raises the OS exception + assert sys.platform == "win32" + # This raises win32gui not installed exception (when OS is correct) + import win32gui # Thanks to poipoiPIO (https://github.com/poipoiPIO) for his HELP!!! From 2d1c2ffb68b6c86891bed0740839a2fe788b8e0b Mon Sep 17 00:00:00 2001 From: Kalmat Date: Thu, 2 Jul 2026 15:22:01 +0200 Subject: [PATCH 2/2] Type handle variable --- src/pywinbox/_pywinbox_linux.py | 15 ++++++--------- src/pywinbox/_pywinbox_macos.py | 14 ++++++-------- src/pywinbox/_pywinbox_win.py | 11 ++++------- 3 files changed, 16 insertions(+), 24 deletions(-) diff --git a/src/pywinbox/_pywinbox_linux.py b/src/pywinbox/_pywinbox_linux.py index f902b97..9622a43 100644 --- a/src/pywinbox/_pywinbox_linux.py +++ b/src/pywinbox/_pywinbox_linux.py @@ -2,11 +2,15 @@ from __future__ import annotations import sys +if sys.platform != "linux": + raise OSError(f"Cannot import {__name__} on {sys.platform}") + import os from ._main import Box try: + # TypeAlias does not exist in typing for Python3.9 from typing import TypeAlias except Exception: from typing import TYPE_CHECKING @@ -14,15 +18,8 @@ from typing_extensions import TypeAlias from typing import Union -try: - from Xlib.xobject.drawable import Window as XWindow - from ewmhlib import EwmhWindow -except Exception: - # This raises the OS exception - assert sys.platform == "linux" - # This raises Xlib/ewmhlib not installed exception (when OS is correct) - from Xlib.xobject.drawable import Window as XWindow - from ewmhlib import EwmhWindow +from Xlib.xobject.drawable import Window as XWindow +from ewmhlib import EwmhWindow _HandleTypeIn: TypeAlias = Union[int, XWindow, None] diff --git a/src/pywinbox/_pywinbox_macos.py b/src/pywinbox/_pywinbox_macos.py index 38aee5e..403267c 100644 --- a/src/pywinbox/_pywinbox_macos.py +++ b/src/pywinbox/_pywinbox_macos.py @@ -3,12 +3,16 @@ # mypy: disable_error_code = no-any-return from __future__ import annotations -import subprocess import sys +if sys.platform != "darwin": + raise OSError(f"Cannot import {__name__} on {sys.platform}") + +import subprocess from ._main import Box try: + # TypeAlias does not exist in typing for Python3.9 from typing import TypeAlias except Exception: from typing import TYPE_CHECKING @@ -16,13 +20,7 @@ from typing_extensions import TypeAlias from typing import NamedTuple, cast, Union -try: - import AppKit -except Exception: - # This raises the OS exception - assert sys.platform == "darwin" - # This raises AppKit not installed exception (when OS is correct) - import AppKit +import AppKit class _macOSNSHandle(NamedTuple): diff --git a/src/pywinbox/_pywinbox_win.py b/src/pywinbox/_pywinbox_win.py index 8af275c..4e40678 100644 --- a/src/pywinbox/_pywinbox_win.py +++ b/src/pywinbox/_pywinbox_win.py @@ -2,12 +2,15 @@ from __future__ import annotations import sys +if sys.platform != "win32": + raise OSError(f"Cannot import {__name__} on {sys.platform}") import ctypes from ._main import Box try: + # TypeAlias does not exist in typing for Python3.9 from typing import TypeAlias except Exception: from typing import TYPE_CHECKING @@ -15,13 +18,7 @@ from typing_extensions import TypeAlias from typing import Union -try: - import win32gui -except Exception: - # This raises the OS exception - assert sys.platform == "win32" - # This raises win32gui not installed exception (when OS is correct) - import win32gui +import win32gui # Thanks to poipoiPIO (https://github.com/poipoiPIO) for his HELP!!!