From b99e43efd82fcc315b54065e3d27d82322167ffb Mon Sep 17 00:00:00 2001 From: Tycho Andersen Date: Sat, 27 Apr 2024 16:32:00 -0600 Subject: [PATCH] fix type lifting with 'Union' types Now that we have real types for setfont, I tried to lift there in a test. It didn't work, because we didn't explicitly import 'Union' into the type lifter function's namespace, so we ended up with: 2024-04-27 16:31:01,582 libqtile loop.py:_handle_exception():L62 Exception in event loop: Traceback (most recent call last): File "/home/tycho/packages/qtile/libqtile/ipc.py", line 235, in _server_callback rep = self.handler(req) File "/home/tycho/packages/qtile/libqtile/command/interface.py", line 424, in call args, kwargs = lift_args(cmd, args, kwargs) File "/home/tycho/packages/qtile/libqtile/command/interface.py", line 367, in lift_args params = typing.get_type_hints(cmd, globalns=globals()) File "/usr/lib/python3.10/typing.py", line 1871, in get_type_hints value = _eval_type(value, globalns, localns) File "/usr/lib/python3.10/typing.py", line 327, in _eval_type return t._evaluate(globalns, localns, recursive_guard) File "/usr/lib/python3.10/typing.py", line 694, in _evaluate eval(self.__forward_code__, globalns, localns), File "", line 1, in NameError: name 'Union' is not defined this is the unfortunate reality that I described in the original lifting commit: we need to have all these types aligned and available in the local namespace in order to make this work. So, let's make this true for Union. Signed-off-by: Tycho Andersen --- libqtile/command/interface.py | 4 ++-- test/test_command.py | 7 +++++++ 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/libqtile/command/interface.py b/libqtile/command/interface.py index 171588c4ad..fa8cf72b76 100644 --- a/libqtile/command/interface.py +++ b/libqtile/command/interface.py @@ -28,7 +28,7 @@ import types import typing from abc import ABCMeta, abstractmethod -from typing import TYPE_CHECKING, Any, Literal, get_args, get_origin +from typing import TYPE_CHECKING, Any, Literal, Union, get_args, get_origin from libqtile import ipc from libqtile.command.base import CommandError, CommandException, CommandObject, SelectError @@ -309,7 +309,7 @@ def lift_args(cmd, args, kwargs): def lift_arg(typ, arg): # for stuff like int | None, allow either - if get_origin(typ) in [types.UnionType, typing.Union]: + if get_origin(typ) in [types.UnionType, Union]: for t in get_args(typ): if t == types.NoneType: # special case None? I don't know what this looks like diff --git a/test/test_command.py b/test/test_command.py index 9cd9fee3a8..fc6da38428 100644 --- a/test/test_command.py +++ b/test/test_command.py @@ -66,6 +66,7 @@ class CallConfig(Config): bottom=libqtile.bar.Bar( [ libqtile.widget.GroupBox(), + libqtile.widget.TextBox(), ], 20, ), @@ -108,6 +109,12 @@ def test_param_hoisting(manager): cmd_client.call("hide_show_bar", position="top", lifted=True) + # 'zomg' is not a valid font size + with pytest.raises(IPCError): + cmd_client.navigate("widget", "textbox").call("set_font", fontsize="zomg", lifted=True) + + cmd_client.navigate("widget", "textbox").call("set_font", fontsize=12, lifted=True) + class FakeCommandObject(CommandObject): @staticmethod