Skip to content

Commit 196d56f

Browse files
committed
fix-unions-for-return-annotations-too
1 parent 2769bb5 commit 196d56f

2 files changed

Lines changed: 32 additions & 4 deletions

File tree

IPython/core/guarded_eval.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -793,9 +793,12 @@ def eval_node(node: Union[ast.AST, None], context: EvaluationContext):
793793

794794
if is_property:
795795
if return_type is not None:
796-
context.transient_locals[node.name] = _resolve_annotation(
797-
return_type, context
798-
)
796+
if is_type_annotation(return_type):
797+
context.transient_locals[node.name] = _resolve_annotation(
798+
return_type, context
799+
)
800+
else:
801+
context.transient_locals[node.name] = return_type
799802
else:
800803
return_value = _infer_return_value(node, func_context)
801804
context.transient_locals[node.name] = return_value
@@ -806,7 +809,10 @@ def dummy_function(*args, **kwargs):
806809
pass
807810

808811
if return_type is not None:
809-
dummy_function.__annotations__["return"] = return_type
812+
if is_type_annotation(return_type):
813+
dummy_function.__annotations__["return"] = return_type
814+
else:
815+
dummy_function.__inferred_return__ = return_type
810816
else:
811817
inferred_return = _infer_return_value(node, func_context)
812818
if inferred_return is not None:

tests/test_completer.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2536,6 +2536,7 @@ def _(expected):
25362536
),
25372537
["capitalize"],
25382538
],
2539+
# Test union types
25392540
[
25402541
"\n".join(
25412542
[
@@ -2545,6 +2546,27 @@ def _(expected):
25452546
),
25462547
["bit_length", "capitalize"],
25472548
],
2549+
[
2550+
"\n".join(
2551+
[
2552+
"def func() -> int | str: pass",
2553+
"func().",
2554+
]
2555+
),
2556+
["bit_length", "capitalize"],
2557+
],
2558+
[
2559+
"\n".join(
2560+
[
2561+
"class T:",
2562+
" @property",
2563+
" def p(self) -> int | str: pass",
2564+
"t = T()",
2565+
"t.p.",
2566+
]
2567+
),
2568+
["bit_length", "capitalize"],
2569+
],
25482570
],
25492571
)
25502572
def test_undefined_variables(use_jedi, evaluation, code, insert_text):

0 commit comments

Comments
 (0)