Skip to content

Preserve natural order of args in the union type #88818

Description

@serhiy-storchaka
BPO 44652
Nosy @gvanrossum, @serhiy-storchaka, @miss-islington, @Fidget-Spinner
PRs
  • bpo-44652: Preserve natural order of args in the union type. #27185
  • [3.10] bpo-44652: Preserve natural order of args in the union type. (GH-27185) #27190
  • 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-07-16.13:43:52.697>
    created_at = <Date 2021-07-16.11:07:49.662>
    labels = ['interpreter-core', 'type-bug', 'library', '3.10', '3.11']
    title = 'Preserve natural order of args in the union type'
    updated_at = <Date 2021-07-16.13:43:52.697>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2021-07-16.13:43:52.697>
    actor = 'kj'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-07-16.13:43:52.697>
    closer = 'kj'
    components = ['Interpreter Core', 'Library (Lib)']
    creation = <Date 2021-07-16.11:07:49.662>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 44652
    keywords = ['patch']
    message_count = 3.0
    messages = ['397612', '397618', '397628']
    nosy_count = 4.0
    nosy_names = ['gvanrossum', 'serhiy.storchaka', 'miss-islington', 'kj']
    pr_nums = ['27185', '27190']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue44652'
    versions = ['Python 3.10', 'Python 3.11']

    Activity

    1. serhiy-storchaka commented on Jul 16, 2021

      @serhiy-storchaka
      MemberAuthor

      There are two issues related to the order of __args__ in typing.Union and the union type.

      1. Indexing typing.Union preserves the order of arguments, but it is not always true when use the | operator.
      >>> A = typing.NewType('A', str)
      >>> typing.Union[typing.List[int], A]
      typing.Union[typing.List[int], A]
      >>> typing.Union[A, typing.List[int]]
      typing.Union[A, typing.List[int]]
      >>> typing.List[int] | A
      typing.Union[typing.List[int], A]
      >>> A | typing.List[int]
      typing.Union[typing.List[int], A]

      The cause is errors in __ror__ implementations.

      1. There is a difference between deduplication algorithms for typing.Union and the union type.
      >>> typing.Union[int, str, int]
      typing.Union[int, str]
      >>> int | str | int
      str | int

      It is not particularly important, because the order of __args__ mostly affects only representation. But it is better to be consistent, and it is easy to fix these tiny issues.

      Note that it does not make the order of __args__ deterministic in all cases. Due to using caching for typing.Union it can be not always preserved if we merger typing.Union objects which differs only by order of arguments.

      >>> import typing
      >>> typing.Union[typing.Union[int, str], list]
      typing.Union[int, str, list]
      >>> typing.Union[typing.Union[str, int], list]
      typing.Union[int, str, list]
    2. added
      3.11only security fixes
      interpreter-core(Objects, Python, Grammar, and Parser dirs)
      stdlibStandard Library Python modules in the Lib/ directory
      type-bugAn unexpected behavior, bug, or error
      on Jul 16, 2021
    3. serhiy-storchaka commented on Jul 16, 2021

      @serhiy-storchaka
      MemberAuthor

      New changeset 0cd2d51 by Serhiy Storchaka in branch 'main':
      bpo-44652: Preserve natural order of args in the union type. (GH-27185)
      0cd2d51

    4. miss-islington commented on Jul 16, 2021

      @miss-islington
      Contributor

      New changeset 80844d1 by Serhiy Storchaka in branch '3.10':
      [3.10] bpo-44652: Preserve natural order of args in the union type. (GH-27185) (GH-27190)
      80844d1

    5. 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.10 (EOL)end of life3.11only security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions