Skip to content

Union with TypeVar does not work as intended #88798

Closed

Description

@serhiy-storchaka
BPO 44632
Nosy @gvanrossum, @serhiy-storchaka, @miss-islington, @Fidget-Spinner
PRs
  • bpo-44632: Fix support of TypeVar in the union type #27139
  • [3.10] bpo-44632: Fix support of TypeVar in the union type (GH-27139) #27143
  • 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-15.07:26:44.915>
    created_at = <Date 2021-07-14.05:33:24.289>
    labels = ['interpreter-core', 'type-bug', '3.10', '3.11']
    title = 'Union with TypeVar does not work as intended'
    updated_at = <Date 2021-07-15.07:26:44.915>
    user = 'https://github.com/serhiy-storchaka'

    bugs.python.org fields:

    activity = <Date 2021-07-15.07:26:44.915>
    actor = 'serhiy.storchaka'
    assignee = 'none'
    closed = True
    closed_date = <Date 2021-07-15.07:26:44.915>
    closer = 'serhiy.storchaka'
    components = ['Interpreter Core']
    creation = <Date 2021-07-14.05:33:24.289>
    creator = 'serhiy.storchaka'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 44632
    keywords = ['patch']
    message_count = 6.0
    messages = ['397466', '397482', '397486', '397487', '397497', '397526']
    nosy_count = 4.0
    nosy_names = ['gvanrossum', 'serhiy.storchaka', 'miss-islington', 'kj']
    pr_nums = ['27139', '27143']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue44632'
    versions = ['Python 3.10', 'Python 3.11']

    Activity

    1. serhiy-storchaka commented on Jul 14, 2021

      @serhiy-storchaka
      MemberAuthor
      >>> import typing
      >>> int | typing.T
      int | ~T
      >>> typing.T | int
      typing.Union[~T, int]
      >>> T2 = TypeVar('T2')
      >>> int | T2
      typing.Union[int, ~T2]
      >>> T2 | int
      typing.Union[~T2, int]

      There is a support of TypeVar in type.__or__, but it does not work with user defined TypeVars, only with internal TypeVars defined in the typing module.

    2. added
      3.11only security fixes
      interpreter-core(Objects, Python, Grammar, and Parser dirs)
      type-bugAn unexpected behavior, bug, or error
      on Jul 14, 2021
    3. gvanrossum commented on Jul 14, 2021

      @gvanrossum
      Member

      The code for recognizing TypeVars must be wrong. Is it also wrong in e.g. list[T]?

    4. Fidget-Spinner commented on Jul 14, 2021

      @Fidget-Spinner
      Member

      Oh this is a fun one :).

      The code for recognizing TypeVars must be wrong. Is it also wrong in e.g. list[T]?

      list[T] is correct. I was pretty puzzled since I thought their code is nearly the same, but then I noticed a subtle difference -- GenericAlias checks for type(o).__module__ == "typing", while Union checks for o.__module__ == "typing", and o.__module__ is wherever the user defines it.

    5. serhiy-storchaka commented on Jul 14, 2021

      @serhiy-storchaka
      MemberAuthor

      Good question. It works correctly in list[T]. There is a tiny difference between implementations of the check in genericaliasobject.c and unionobject.c.

    6. serhiy-storchaka commented on Jul 14, 2021

      @serhiy-storchaka
      MemberAuthor

      New changeset a158b20 by Serhiy Storchaka in branch 'main':
      bpo-44632: Fix support of TypeVar in the union type (GH-27139)
      a158b20

    7. serhiy-storchaka commented on Jul 15, 2021

      @serhiy-storchaka
      MemberAuthor

      New changeset cc1a47c by Miss Islington (bot) in branch '3.10':
      bpo-44632: Fix support of TypeVar in the union type (GH-27139) (GH-27143)
      cc1a47c

    8. 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)type-bugAn unexpected behavior, bug, or error

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions