Skip to content

Commit 95c0e4f

Browse files
authored
Add support for PEP 604 union types in inline signatures (#8449)
1 parent 1697f15 commit 95c0e4f

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

dspy/signatures/signature.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,18 @@ def resolve_name(type_name: str):
580580

581581
return base_type[arg_types]
582582

583+
if isinstance(node, ast.BinOp) and isinstance(node.op, ast.BitOr):
584+
# Handle PEP 604: int | None, str | float, etc.
585+
left = _parse_type_node(node.left, names)
586+
right = _parse_type_node(node.right, names)
587+
588+
# Optional[X] is Union[X, NoneType]
589+
if right is type(None):
590+
return typing.Optional[left]
591+
if left is type(None):
592+
return typing.Optional[right]
593+
return typing.Union[left, right]
594+
583595
if isinstance(node, ast.Tuple):
584596
return tuple(_parse_type_node(elt, names) for elt in node.elts)
585597

0 commit comments

Comments
 (0)