Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Let's use our type annotations to resolve how to lift strings into whatever their right type is. For now, this just works on "normal" base+hand handled compound types. I have implemented stuff for Union and Literal, since we have those kinds of type annotations. However, I have not implemented e.g. lists as a compound type, since as near as I can tell we do not have any exposed commands that take lists (although it would not be hard, I just didn't want to add a bunch of unused code; see the discussion on the issues cited below for some sample code if you're looking at this in the future). I put normal in quotes, because of e.g. the behavior with bool. It's a bit ugly, but... best to only have to write it once. Maybe we should use eval() for some of these instead? But that's even uglier. Anyway. For supporting "custom" types (i.e. ones defined in libqtile somewhere that are more complex than just a constructor that accepts a string), we could do some kind of poking around in the type for a `from_str`, e.g.: >>> from __future__ import annotations >>> class C: ... def from_str(s: str) -> C: ... pass ... >>> class D: ... def foo(c: C): pass ... >>> typing.get_type_hints(D().foo)["c"] <class '__main__.C'> >>> hasattr(typing.get_type_hints(D().foo)["c"], "from_str") True again, though, we don't have any of these, so no need to define this convention right now. Finally, one annoying part of all this is that all of the type annotations that anyone uses in the qtile code base need to be imported in interface.py, hence the new noqa import. Otherwise, you end up with, 2024-03-25 19:35:19,812 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 357, in call 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 'ColorType' is not defined this is annoying, but IMO a small price to pay for only having to write this code once. If we end up with circular dependencies or such, we can split this code out into its own module, but for now it seemed to work fine. Fixes qtile#2433 Fixes qtile#2435 Fixes qtile#4737 Signed-off-by: Tycho Andersen <[email protected]>
- Loading branch information