Skip to content

Commit

Permalink
fix type lifting with 'Union' types
Browse files Browse the repository at this point in the history
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 "<string>", line 1, in <module>
    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 <tycho@tycho.pizza>
  • Loading branch information
tych0 committed Apr 27, 2024
1 parent 95e0a5c commit bfac6cf
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
4 changes: 2 additions & 2 deletions libqtile/command/interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions test/test_command.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ class CallConfig(Config):
bottom=libqtile.bar.Bar(
[
libqtile.widget.GroupBox(),
libqtile.widget.TextBox(),
],
20,
),
Expand Down Expand Up @@ -110,6 +111,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
Expand Down

0 comments on commit bfac6cf

Please sign in to comment.