]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.12] gh-90015: Document that PEP-604 unions do not support forward references ...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Wed, 7 Jun 2023 18:11:48 +0000 (11:11 -0700)
committerGitHub <noreply@github.com>
Wed, 7 Jun 2023 18:11:48 +0000 (19:11 +0100)
gh-90015: Document that PEP-604 unions do not support forward references (GH-105366)
(cherry picked from commit fbdee000de47ae96fbf53ce8908e8efbb23cfba4)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Doc/library/stdtypes.rst

index fdef5314b9a4eff937fd74c6ebf8eb1b37cd8816..0caa725f75e642e7e38f4dd6d65d4cd76e446744 100644 (file)
@@ -5162,6 +5162,14 @@ enables cleaner type hinting syntax compared to :data:`typing.Union`.
       def square(number: int | float) -> int | float:
           return number ** 2
 
+   .. note::
+
+      The ``|`` operand cannot be used at runtime to define unions where one or
+      more members is a forward reference. For example, ``int | "Foo"``, where
+      ``"Foo"`` is a reference to a class not yet defined, will fail at
+      runtime. For unions which include forward references, present the
+      whole expression as a string, e.g. ``"int | Foo"``.
+
 .. describe:: union_object == other
 
    Union objects can be tested for equality with other union objects.  Details: