Skip to content

Commit ccf5432

Browse files
committed
Fixed UNION processing
1 parent 4a263bf commit ccf5432

File tree

5 files changed

+24
-17
lines changed

5 files changed

+24
-17
lines changed

docs/source/changelog.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ Glossary
2424
Releases
2525
---------------------
2626

27+
v1.2.2
28+
================
29+
- Fixed incorrect ``Union`` processing if it was used in a ``List`` annotation.
30+
31+
32+
2733
v1.2.1
2834
================
2935
- Replaced raw usage of ``.__annotations__`` with :func:`typing.get_typehints`.

tkclasswiz/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Works with Tkinter / TTKBootstrap.
44
"""
55

6-
__version__ = "1.2.1"
6+
__version__ = "1.2.2"
77

88
from .object_frame import *
99
from .annotations import *

tkclasswiz/annotations.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def get_annotations(class_) -> dict:
9494
in Python 3.12.
9595
"""
9696
annotations = {}
97-
with suppress(AttributeError):
97+
with suppress(AttributeError, TypeError):
9898
if isclass(class_):
9999
annotations = get_type_hints(class_.__init__)
100100
elif isclass(origin_class := get_origin(class_)) and issubclass(origin_class, Generic):

tkclasswiz/object_frame/frame_base.py

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -175,16 +175,22 @@ def remove_classes(types: list):
175175
if isabstract(type_):
176176
r.remove(type_)
177177

178-
return r
178+
return tuple(r)
179179

180-
while get_origin(types_in) is Union:
181-
types_in = get_args(types_in)
180+
if get_origin(types_in) is Union:
181+
types_in = cls.convert_types(get_args(types_in))
182182

183-
if not isinstance(types_in, list):
184-
if isinstance(types_in, tuple):
185-
types_in = list(types_in)
186-
else:
187-
types_in = [types_in, ]
183+
elif issubclass_noexcept(origin := get_origin(types_in), Iterable) and types_in is not str:
184+
types_in = origin[cls.convert_types(get_args(types_in))]
185+
186+
if isinstance(types_in, tuple):
187+
new_types = []
188+
for t in types_in:
189+
new_types.extend(cls.convert_types(t))
190+
191+
types_in = new_types
192+
else:
193+
types_in = (types_in,)
188194

189195
# Also include inherited objects
190196
subtypes = []
@@ -197,7 +203,7 @@ def remove_classes(types: list):
197203
subtypes.extend(cls.convert_types(st))
198204

199205
# Remove wrapped classes (eg. wrapped by decorator) + ABC classes
200-
return remove_classes(types_in + subtypes)
206+
return remove_classes([*types_in, *subtypes])
201207

202208
def init_main_frame(self):
203209
frame_main = ttk.Frame(self)

tkclasswiz/object_frame/frame_iterable.py

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,7 @@ def __init__(
8888
ttk.Button(frame_up_down, text="Up", command=lambda: w.move_selection(-1)).pack(side="left", fill=tk.X, expand=True)
8989
ttk.Button(frame_up_down, text="Down", command=lambda: w.move_selection(1)).pack(side="left", fill=tk.X, expand=True)
9090

91-
args = get_args(self.class_)
92-
args = self.convert_types(args)
93-
if get_origin(args[0]) is Union:
94-
args = get_args(args[0])
95-
96-
for arg in args:
91+
for arg in get_args(self.class_):
9792
menu.add_command(label=self.get_cls_name(arg), command=partial(self.new_object_frame, arg, w))
9893

9994
w.pack(side="left", fill=tk.BOTH, expand=True)

0 commit comments

Comments
 (0)