This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: ForwardRef does not support | operator
Type: behavior Stage: resolved
Components: Versions: Python 3.11
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: bobbeyreese, corona10, gvanrossum, kj
Priority: normal Keywords: patch

Created on 2021-10-15 16:37 by bobbeyreese, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 28991 merged corona10, 2021-10-16 14:47
Messages (8)
msg404037 - (view) Author: Bobbey Reese (bobbeyreese) Date: 2021-10-15 16:37
Not positive this is a bug, but it seems like ForwardRef should support the pipe (|) operator for indicating Unions in Python 3.10:

Python 3.10.0 (default, Oct  4 2021, 22:09:55) [GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from typing import ForwardRef
>>> ForwardRef('asdf') | ForwardRef('fdsa')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for |: 'ForwardRef' and 'ForwardRef'
>>> int | str
int | str
>>>
msg404078 - (view) Author: Dong-hee Na (corona10) * (Python committer) Date: 2021-10-16 13:43
--- a/Lib/typing.py
+++ b/Lib/typing.py
@@ -719,6 +719,12 @@ def __eq__(self, other):
     def __hash__(self):
         return hash(self.__forward_arg__)

+    def __or__(self, other):
+        return Union[self, other]
+
+    def __ror__(self, other):
+        return Union[other, self]
+
     def __repr__(self):
         return f'ForwardRef({self.__forward_arg__!r})'

This can be easily fixed, but I am waiting for guido and ken to check whether this is an intentioned operation.
msg404084 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2021-10-16 14:31
I suppose this is useful for programs that use runtime annotations (e.g. pydantic?). I don’t mind adding this.
msg404085 - (view) Author: Ken Jin (kj) * (Python committer) Date: 2021-10-16 14:48
ForwardRef is for internal use, and writing ForwardRef('fdsa') is discouraged, as the docs say:

   This class should not be instantiated by a user.

However, I don't see any cons from adding union support, so let's do it!

Note that a workaround exists for OP's example. But it may not be sufficient for complex runtime libraries:

ForwardRef('asdf | fdsa')
msg404086 - (view) Author: Dong-hee Na (corona10) * (Python committer) Date: 2021-10-16 15:13
New changeset 15ad52fbf607b6ccec44a38a8a32a5f1fad635ee by Dong-hee Na in branch 'main':
bpo-45489: Update ForwardRef to support | operator. (GH-28991)
https://github.com/python/cpython/commit/15ad52fbf607b6ccec44a38a8a32a5f1fad635ee
msg404087 - (view) Author: Dong-hee Na (corona10) * (Python committer) Date: 2021-10-16 15:15
@guido

Do we need to create a backport for 3.10?
Ken and I are waiting for your comment :)
https://github.com/python/cpython/pull/28991#issuecomment-944926933
msg404090 - (view) Author: Guido van Rossum (gvanrossum) * (Python committer) Date: 2021-10-16 15:39
No. :-)
msg404091 - (view) Author: Ken Jin (kj) * (Python committer) Date: 2021-10-16 15:43
Thanks @corona10 for the patch.

@bobbeyreese
The new behavior is available in 3.11 and up. 3.10.x won't be supporting the | operator.
History
Date User Action Args
2022-04-11 14:59:51adminsetgithub: 89652
2021-10-16 15:43:32kjsetstatus: open -> closed
versions: - Python 3.10
messages: + msg404091

resolution: fixed
stage: resolved
2021-10-16 15:39:38gvanrossumsetmessages: + msg404090
2021-10-16 15:15:16corona10setmessages: + msg404087
2021-10-16 15:13:06corona10setmessages: + msg404086
2021-10-16 14:48:33kjsetmessages: + msg404085
stage: patch review -> (no value)
2021-10-16 14:47:49corona10setkeywords: + patch
stage: patch review
pull_requests: + pull_request27275
2021-10-16 14:31:07gvanrossumsetmessages: + msg404084
2021-10-16 13:45:17corona10setversions: + Python 3.11
2021-10-16 13:43:34corona10setmessages: + msg404078
2021-10-16 13:37:12corona10setnosy: + corona10
2021-10-16 12:53:09terry.reedysetnosy: + gvanrossum, kj
2021-10-15 16:37:31bobbeyreesecreate