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

Unioned types should be hashable: not documented? #112281

Closed
faph opened this issue Nov 20, 2023 · 2 comments
Closed

Unioned types should be hashable: not documented? #112281

faph opened this issue Nov 20, 2023 · 2 comments
Assignees
Labels
topic-typing type-bug An unexpected behavior, bug, or error

Comments

@faph
Copy link

faph commented Nov 20, 2023

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

@sobolevn
Copy link
Member

sobolevn commented Nov 20, 2023

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! 👍

@sobolevn sobolevn added type-bug An unexpected behavior, bug, or error and removed docs Documentation in the Doc dir labels Nov 20, 2023
@sobolevn sobolevn self-assigned this Nov 20, 2023
@sobolevn
Copy link
Member

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

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

sobolevn added a commit to sobolevn/cpython that referenced this issue Nov 20, 2023
AlexWaygood added a commit that referenced this issue Mar 1, 2024
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
miss-islington pushed a commit to miss-islington/cpython that referenced this issue Mar 1, 2024
…ythonGH-112283)

(cherry picked from commit a7549b0)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
AlexWaygood added a commit that referenced this issue Mar 1, 2024
…H-112283) (#116213)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
sobolevn added a commit to sobolevn/cpython that referenced this issue Mar 3, 2024
…adata (pythonGH-112283)

(cherry picked from commit a7549b0)

Co-authored-by: Nikita Sobolev <mail@sobolevn.me>
Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
AlexWaygood added a commit that referenced this issue Mar 3, 2024
…H-112283) (#116288)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
woodruffw pushed a commit to woodruffw-forks/cpython that referenced this issue Mar 4, 2024
…ython#112283)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
adorilson pushed a commit to adorilson/cpython that referenced this issue Mar 25, 2024
…ython#112283)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
diegorusso pushed a commit to diegorusso/cpython that referenced this issue Apr 17, 2024
…ython#112283)

Co-authored-by: Alex Waygood <Alex.Waygood@Gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
topic-typing type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants