Skip to content

Commit 39feb5d

Browse files
authored
Accommodate old-style hints (#661)
* Accommodate old-style hints In particular `typing.Optional` is not an instance of `types.Union`, so add a clause to catch that case and test old- and new-style equivalency. Signed-off-by: liamhuber <liamhuber@greyhavensolutions.com> * Tell ruff I'm doing this on purpose Signed-off-by: liamhuber <liamhuber@greyhavensolutions.com> * Black Signed-off-by: liamhuber <liamhuber@greyhavensolutions.com> * Move ruff instructions Signed-off-by: liamhuber <liamhuber@greyhavensolutions.com> --------- Signed-off-by: liamhuber <liamhuber@greyhavensolutions.com>
1 parent 1efa223 commit 39feb5d

File tree

2 files changed

+36
-1
lines changed

2 files changed

+36
-1
lines changed

pyiron_workflow/type_hinting.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ def valid_value(value, type_hint, strict_callables: bool = True) -> bool:
4444

4545

4646
def type_hint_to_tuple(type_hint) -> tuple:
47-
if isinstance(type_hint, types.UnionType):
47+
if isinstance(type_hint, types.UnionType) or (
48+
hasattr(type_hint, "__origin__") and type_hint.__origin__ is typing.Union
49+
):
4850
return typing.get_args(type_hint)
4951
return (type_hint,)
5052

tests/unit/test_type_hinting.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from pyiron_workflow.type_hinting import (
77
_get_type_hints,
88
type_hint_is_as_or_more_specific_than,
9+
type_hint_to_tuple,
910
valid_value,
1011
)
1112

@@ -106,6 +107,16 @@ def test_hint_comparisons(self):
106107
typing.Annotated[int, "foo"],
107108
True,
108109
),
110+
(
111+
typing.Optional[typing.Dict[str, int]], # noqa: UP006, UP007
112+
dict[str, int] | None,
113+
True, # Old- and new-styles should be equivalent
114+
),
115+
(
116+
dict[str, int] | None,
117+
typing.Optional[typing.Dict[str, int]], # noqa: UP006, UP007
118+
True, # Thus, we expect the reverse direction to work to prove identity
119+
),
109120
]:
110121
with self.subTest(
111122
target=target, reference=reference, expected=is_more_specific
@@ -128,6 +139,28 @@ def test_get_type_hints(self):
128139
with self.subTest(hint=hint, origin=origin):
129140
self.assertEqual(_get_type_hints(hint)[0], origin)
130141

142+
def test_type_hint_to_tuple(self):
143+
for hint, tuple_ in (
144+
(dict[str, int] | None, (dict[str, int], type(None))),
145+
(
146+
typing.Dict[str, int] | None, # noqa: UP006
147+
(typing.Dict[str, int], type(None)), # noqa: UP006
148+
),
149+
(
150+
typing.Optional[dict[str, int]], # noqa: UP007
151+
(dict[str, int], type(None)),
152+
),
153+
(
154+
typing.Optional[typing.Dict[str, int]], # noqa: UP006, UP007
155+
(typing.Dict[str, int], type(None)), # noqa: UP006
156+
),
157+
):
158+
self.assertEqual(
159+
type_hint_to_tuple(hint),
160+
tuple_,
161+
msg="Old- and new-style hints should both be split into a tuple",
162+
)
163+
131164

132165
if __name__ == "__main__":
133166
unittest.main()

0 commit comments

Comments
 (0)