Skip to content

It is possible to create a 1-type union type #88802

Closed

Description

@serhiy-storchaka
BPO 44636
Nosy @gvanrossum, @serhiy-storchaka, @Fidget-Spinner
PRs
  • bpo-44636: Collapse union of equal types #27178
  • [3.10] bpo-44636: Collapse union of equal types (GH-27178) #27181
  • Dependencies
  • bpo-44632: Union with TypeVar does not work as intended
  • 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.11:49:32.820>
    created_at = <Date 2021-07-14.11:04:42.002>
    labels = ['interpreter-core', 'type-bug', '3.10', '3.11']
    title = 'It is possible to create a 1-type union type'
    updated_at = <Date 2021-07-16.11:49:32.820>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2021-07-16.11:49:32.820>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-07-16.11:49:32.820>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core']
    creation = <Date 2021-07-14.11:04:42.002>
    creator = 'serhiy.storchaka'
    dependencies = ['44632']
    files = []
    hgrepos = []
    issue_num = 44636
    keywords = ['patch']
    message_count = 7.0
    messages = ['397476', '397527', '397559', '397564', '397577', '397608', '397615']
    nosy_count = 3.0
    nosy_names = ['gvanrossum', 'serhiy.storchaka', 'kj']
    pr_nums = ['27178', '27181']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue44636'
    versions = ['Python 3.10', 'Python 3.11']

    Activity

    1. serhiy-storchaka commented on Jul 14, 2021

      @serhiy-storchaka
      MemberAuthor

      typing.Union always collapsed to a simple type if it contains a single type.

      >>> import typing
      >>> typing.Union[int, typing.T][int]
      <class 'int'>

      But the builtin union type can contain a single type:

      >>> (int | typing.T)[int]
      int
      >>> type((int | typing.T)[int])
      <class 'types.Union'>
    2. added
      3.10 (EOL)
      3.11
      interpreter-core
      type-bug
      on Jul 14, 2021
    3. serhiy-storchaka commented on Jul 15, 2021

      @serhiy-storchaka
      MemberAuthor

      Simple example (without indexing):

      >>> import typing
      >>> typing.Union[int, int]
      <class 'int'>
      >>> int | int
      int
    4. gvanrossum commented on Jul 15, 2021

      @gvanrossum
      Member

      I feel that keeping the singleton is more consistent. This normalization seems to me an example of the typing module doing too much. Singleton tuples are different from scalar values too.

    5. serhiy-storchaka commented on Jul 15, 2021

      @serhiy-storchaka
      MemberAuthor

      It is a difference with typing.Union which can cause confusion. If the union type is like a tuple and we leave a 1-type union, why do we bother with deduplication? Why int | str | int is collapsed into int | str?

      Also it complicates the comparison implementation and produces surprising exceptions:

      >>> int | str == {}
      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
      TypeError: unhashable type: 'dict'

      Also it breaks one of fundamental properties -- equal objects should have equal hashes.

      >>> (int | int) == int
      True
      >>> hash(int | int) == hash(int)
      False
    6. gvanrossum commented on Jul 15, 2021

      @gvanrossum
      Member

      Hm, you are right. Make it so, Mr. Spock! :-)

    7. serhiy-storchaka commented on Jul 16, 2021

      @serhiy-storchaka
      MemberAuthor

      New changeset d9f9232 by Serhiy Storchaka in branch 'main':
      bpo-44636: Collapse union of equal types (GH-27178)
      d9f9232

    8. serhiy-storchaka commented on Jul 16, 2021

      @serhiy-storchaka
      MemberAuthor

      New changeset c3007ab by Serhiy Storchaka in branch '3.10':
      [3.10] bpo-44636: Collapse union of equal types (GH-27178) (GH-27181)
      c3007ab

    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.10 (EOL)3.11interpreter-coretype-bug

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions