Implement unions with forward references

This commit is contained in:
Dave Halter
2026-05-01 16:40:25 +02:00
parent 01dc123ea1
commit d87a4af50f
2 changed files with 17 additions and 6 deletions
+9 -6
View File
@@ -37,12 +37,15 @@ def infer_annotation(context, annotation):
"Inferred typing index %s should lead to 1 object, not %s" % (annotation, value_set))
return value_set
inferred_value = list(value_set)[0]
if is_string(inferred_value):
result = _get_forward_reference_node(context, inferred_value.get_safe_value())
if result is not None:
return context.infer_node(result)
return value_set
strings_removed = NO_VALUES
for part in value_set:
if is_string(part):
result = _get_forward_reference_node(context, part.get_safe_value())
if result is not None:
strings_removed |= context.infer_node(result)
continue
strings_removed |= ValueSet([part])
return strings_removed
def _infer_annotation_string(context, string, index=None):
+8
View File
@@ -230,3 +230,11 @@ def use_type_with_annotation() -> type[int]: ...
#? int
use_type_with_annotation()
def union_with_forward_references(x: int | "str", y: "int" | str, z: "int | str"):
#? int() str()
x
#? int() str()
y
#? int() str()
z