Skip to content

Commit 40d1d28

Browse files
[3.11] gh-90015: Document that PEP-604 unions do not support forward references (GH-105366) (#105461)
gh-90015: Document that PEP-604 unions do not support forward references (GH-105366) (cherry picked from commit fbdee00) Co-authored-by: Alex Waygood <[email protected]>
1 parent 34f2390 commit 40d1d28

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

Doc/library/stdtypes.rst

+8
Original file line numberDiff line numberDiff line change
@@ -5109,6 +5109,14 @@ enables cleaner type hinting syntax compared to :data:`typing.Union`.
51095109
def square(number: int | float) -> int | float:
51105110
return number ** 2
51115111

5112+
.. note::
5113+
5114+
The ``|`` operand cannot be used at runtime to define unions where one or
5115+
more members is a forward reference. For example, ``int | "Foo"``, where
5116+
``"Foo"`` is a reference to a class not yet defined, will fail at
5117+
runtime. For unions which include forward references, present the
5118+
whole expression as a string, e.g. ``"int | Foo"``.
5119+
51125120
.. describe:: union_object == other
51135121

51145122
Union objects can be tested for equality with other union objects. Details:

0 commit comments

Comments
 (0)