[issue45857] PEP 604 Union syntax does not support forward references

Alex Waygood report at bugs.python.org
Mon Nov 22 11:46:08 EST 2021


Alex Waygood <Alex.Waygood at Gmail.com> added the comment:

Thanks, Ken! To clarify: I agree that changing the implementation here would probably be a bad way to go: it would be foolish to try to replicate all the functionality of the typing module as builtins. I also think the existing documentation at https://docs.python.org/3/library/stdtypes.html#union-type is actually very good, so I don't think it needs a fundamental rewrite by any means.

I do still think a sentence or two could be added to the documentation, however, clarifying how to deal with forward references, since the behaviour here is different to the older, more established syntax using typing.Union. Something like this?

"""
Note: The object on both sides of the | operand must be an object that defines the __or__ special method. As the str type does not support __or__, the expression `int | "Foo"`, where  "Foo" is a reference to a class not yet defined, will fail at runtime. To annotate forward references using union-type expressions, present the whole expression as a string, e.g. `"int | Foo"`.
"""

----------
status: pending -> open

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue45857>
_______________________________________


More information about the Python-bugs-list mailing list