Skip to content

ForwardRef does not support | operator #89652

Description

@partiallyderived
BPO 45489
Nosy @gvanrossum, @corona10, @Fidget-Spinner, @partiallyderived
PRs
  • bpo-45489: Update ForwardRef to support | operator. #28991
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2021-10-16.15:43:32.751>
    created_at = <Date 2021-10-15.16:37:31.685>
    labels = ['type-bug', '3.11']
    title = 'ForwardRef does not support | operator'
    updated_at = <Date 2021-10-16.15:43:32.750>
    user = 'https://github.com/partiallyderived'

    bugs.python.org fields:

    activity = <Date 2021-10-16.15:43:32.750>
    actor = 'kj'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-10-16.15:43:32.751>
    closer = 'kj'
    components = []
    creation = <Date 2021-10-15.16:37:31.685>
    creator = 'bobbeyreese'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 45489
    keywords = ['patch']
    message_count = 8.0
    messages = ['404037', '404078', '404084', '404085', '404086', '404087', '404090', '404091']
    nosy_count = 4.0
    nosy_names = ['gvanrossum', 'corona10', 'kj', 'bobbeyreese']
    pr_nums = ['28991']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue45489'
    versions = ['Python 3.11']

    Activity

    1. partiallyderived commented on Oct 15, 2021

      partiallyderivedmannequin
      MannequinAuthor

      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
      >>>
    2. corona10 commented on Oct 16, 2021

      @corona10
      Member

      --- 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.

    3. gvanrossum commented on Oct 16, 2021

      @gvanrossum
      Member

      I suppose this is useful for programs that use runtime annotations (e.g. pydantic?). I don’t mind adding this.

    4. Fidget-Spinner commented on Oct 16, 2021

      @Fidget-Spinner
      Member

      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')
    5. corona10 commented on Oct 16, 2021

      @corona10
      Member

      New changeset 15ad52f by Dong-hee Na in branch 'main':
      bpo-45489: Update ForwardRef to support | operator. (GH-28991)
      15ad52f

    6. corona10 commented on Oct 16, 2021

      @corona10
      Member

      @guido

      Do we need to create a backport for 3.10?
      Ken and I are waiting for your comment :)
      #28991 (comment)

    7. gvanrossum commented on Oct 16, 2021

      @gvanrossum
      Member

      No. :-)

    8. Fidget-Spinner commented on Oct 16, 2021

      @Fidget-Spinner
      Member

      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.

    9. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.11only security fixestype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions