GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Unioned types should be hashable: not documented? · Issue #112281 · python/cpython

Unioned types should be hashable: not documented? #112281

New issue
Copy link
New issue
Copy link
Closed
Closed
Unioned types should be hashable: not documented?#112281
Copy link
Assignees
sobolevn
Labels
topic-typingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@faph
faph
opened on Nov 20, 2023
Issue body actions

Documentation

It seems that typing.Union[X, Y] as well as X | Y type hints require X and Y to be hashable.

This is not documented as far as I can see.

Considering this example:

@dataclass
class ValueRange:
    lo: int
    hi: int

T1 = Annotated[int, ValueRange(-10, 5)]

This fails when unioned:

>>> Annotated[int, ValueRange(-10, 5)] | None
(...)
  File "/usr/lib64/python3.11/typing.py", line 1375, in __or__
    return Union[self, right]
           ~~~~~^^^^^^^^^^^^^
  File "/usr/lib64/python3.11/typing.py", line 358, in inner
    return func(*args, **kwds)
           ^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.11/typing.py", line 481, in __getitem__
    return self._getitem(self, parameters)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.11/typing.py", line 695, in Union
    parameters = _remove_dups_flatten(parameters)
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.11/typing.py", line 326, in _remove_dups_flatten
    return tuple(_deduplicate(params))
                 ^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib64/python3.11/typing.py", line 301, in _deduplicate
    all_params = set(params)
                 ^^^^^^^^^^^
  File "/usr/lib64/python3.11/typing.py", line 2151, in __hash__
    return hash((self.__origin__, self.__metadata__))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
TypeError: unhashable type: 'ValueRange'

Should this be documented say here: https://docs.python.org/3/library/typing.html?highlight=typing#typing.Annotated ?

And examples here should use frozen dataclasses: https://docs.python.org/3/library/typing.html?highlight=typing#typing.Annotated ?

Linked PRs

  • gh-112281: Allow Union with unhashable Annotated metadata #112283
  • [3.12] gh-112281: Allow Union with unhashable Annotated metadata (GH-112283) #116213
  • [3.11] gh-112281: Allow Union with unhashable Annotated metadata (GH-112283) #116288
Reacted by Daniel Gellert

Activity

  1. faph
    added
    docsDocumentation in the Doc dirDocumentation in the Doc dir
    on Nov 20, 2023
  2. Eclips4
    added
    topic-typing
    on Nov 20, 2023
  3. sobolevn commented on Nov 20, 2023

    @sobolevn
    sobolevn
    on Nov 20, 2023
    Member
    More actions

    PEPs https://peps.python.org/pep-0593/#specification and https://peps.python.org/pep-0604/ do not say anything about requirement of items to be hashable. I think that this is a bug: unhashable items must be allowed.

    I think that we need to rework our _deduplicate function. I have an idea on how to do it :)

    Thanks for the report! 👍

  4. sobolevn
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    and removed
    docsDocumentation in the Doc dirDocumentation in the Doc dir
    on Nov 20, 2023
  5. sobolevn
    self-assigned this
    on Nov 20, 2023
  6. sobolevn commented on Nov 20, 2023

    @sobolevn
    sobolevn
    on Nov 20, 2023
    Member
    More actions

    This is more complex than I initially thought, because _UnionGenericAlias also uses set to compare results. So, we need a slow way of doing it as well. See

    cpython/Lib/typing.py

    Line 1536 in ce1096f

    return set(self.__args__) == set(other.__args__)

  7. sobolevn
    added a commit that references this issue on Nov 20, 2023

    pythongh-112281: Allow `Union` with unhashable `Annotated` metadata

    c13aa09
  8. AlexWaygood
    added a commit that references this issue on Mar 1, 2024

    gh-112281: Allow `Union` with unhashable `Annotated` metadata (#112283)

    a7549b0
  9. miss-islington
    added a commit that references this issue on Mar 1, 2024

    pythongh-112281: Allow `Union` with unhashable `Annotated` metadata (p…

    21cad08
  10. AlexWaygood
    added a commit that references this issue on Mar 1, 2024

    [3.12] gh-112281: Allow `Union` with unhashable `Annotated` metadata (G…

    90f75e1
  11. sobolevn
    added a commit that references this issue on Mar 3, 2024

    [3.11] pythongh-112281: Allow `Union` with unhashable `Annotated` met…

    5612315
  12. AlexWaygood
    added a commit that references this issue on Mar 3, 2024

    [3.11] gh-112281: Allow `Union` with unhashable `Annotated` metadata (G…

    8bfbeeb
  13. AlexWaygood
    closed this as completedon Mar 3, 2024
  14. woodruffw
    added a commit that references this issue on Mar 4, 2024

    pythongh-112281: Allow `Union` with unhashable `Annotated` metadata (p…

    326fad4
  15. adorilson
    added a commit that references this issue on Mar 25, 2024

    pythongh-112281: Allow `Union` with unhashable `Annotated` metadata (p…

    6b0c1c2
  16. diegorusso
    added a commit that references this issue on Apr 17, 2024

    pythongh-112281: Allow `Union` with unhashable `Annotated` metadata (p…

    2b44ae1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @sobolevn
    sobolevn

Labels

topic-typingtype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

    You are visitor 01402520 — GeezerVison does not store the pages you look up.
    Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.