Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ForwardRef does not support | operator #89652

Closed
partiallyderived mannequin opened this issue Oct 15, 2021 · 8 comments
Closed

ForwardRef does not support | operator #89652

partiallyderived mannequin opened this issue Oct 15, 2021 · 8 comments
Labels
3.11 only security fixes type-bug An unexpected behavior, bug, or error

Comments

@partiallyderived
Copy link
Mannequin

partiallyderived mannequin commented Oct 15, 2021

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']

    @partiallyderived
    Copy link
    Mannequin Author

    partiallyderived mannequin commented Oct 15, 2021

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

    @partiallyderived partiallyderived mannequin added 3.10 only security fixes type-bug An unexpected behavior, bug, or error labels Oct 15, 2021
    @corona10
    Copy link
    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.

    @corona10 corona10 added 3.11 only security fixes labels Oct 16, 2021
    @gvanrossum
    Copy link
    Member

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

    @Fidget-Spinner
    Copy link
    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')

    @corona10
    Copy link
    Member

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

    @corona10
    Copy link
    Member

    @guido

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

    @gvanrossum
    Copy link
    Member

    No. :-)

    @Fidget-Spinner
    Copy link
    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.

    @Fidget-Spinner Fidget-Spinner removed the 3.10 only security fixes label Oct 16, 2021
    @Fidget-Spinner Fidget-Spinner removed the 3.10 only security fixes label Oct 16, 2021
    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.11 only security fixes type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants