Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Unioned types should be hashable: not documented? #112281

Closed
Assignees
sobolevn
Labels
@faph

Description

@faph

Documentation

It seems thattyping.Union[X, Y] as well asX | Y type hints requireX andY to be hashable.

This is not documented as far as I can see.

Considering this example:

@dataclassclassValueRange:lo:inthi:intT1=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 usefrozen dataclasses:https://docs.python.org/3/library/typing.html?highlight=typing#typing.Annotated ?

Linked PRs

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions


    [8]ページ先頭

    ©2009-2025 Movatter.jp