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

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

Closed
serhiy-storchaka opened this issue Jul 14, 2021 · 7 comments
Closed

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

serhiy-storchaka opened this issue Jul 14, 2021 · 7 comments
Labels
3.10 only security fixes 3.11 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error

Comments

@serhiy-storchaka
Copy link
Member

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

    @serhiy-storchaka
    Copy link
    Member Author

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

    @serhiy-storchaka serhiy-storchaka added 3.10 only security fixes 3.11 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error labels Jul 14, 2021
    @serhiy-storchaka
    Copy link
    Member Author

    Simple example (without indexing):

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

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

    @serhiy-storchaka
    Copy link
    Member Author

    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

    @gvanrossum
    Copy link
    Member

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

    @serhiy-storchaka
    Copy link
    Member Author

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

    @serhiy-storchaka
    Copy link
    Member Author

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

    @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.10 only security fixes 3.11 only security fixes interpreter-core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants