This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: Optional callable raises TypeError
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.9
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: Ofekmeister, kj
Priority: normal Keywords:

Created on 2021-01-19 08:11 by Ofekmeister, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (4)
msg385246 - (view) Author: Ofek Lev (Ofekmeister) * Date: 2021-01-19 08:11
https://docs.python.org/3.9/library/typing.html#callable

```
Python 3.9.1 (default, Jan 12 2021, 16:45:25)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from typing import Optional
>>> from collections.abc import Callable
>>>
>>> Hasher = Optional[Callable[[bytes], bytes]]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.9/typing.py", line 262, in inner
    return func(*args, **kwds)
  File "/usr/local/lib/python3.9/typing.py", line 339, in __getitem__
    return self._getitem(self, parameters)
  File "/usr/local/lib/python3.9/typing.py", line 463, in Optional
    return Union[arg, type(None)]
  File "/usr/local/lib/python3.9/typing.py", line 262, in inner
    return func(*args, **kwds)
  File "/usr/local/lib/python3.9/typing.py", line 339, in __getitem__
    return self._getitem(self, parameters)
  File "/usr/local/lib/python3.9/typing.py", line 451, in Union
    parameters = _remove_dups_flatten(parameters)
  File "/usr/local/lib/python3.9/typing.py", line 231, in _remove_dups_flatten
    return tuple(_deduplicate(params))
  File "/usr/local/lib/python3.9/typing.py", line 205, in _deduplicate
    all_params = set(params)
TypeError: unhashable type: 'list'
>>>
>>> from typing import Tuple
>>> Hasher = Optional[Callable[Tuple[bytes], bytes]]
>>>
```

Tuple type for arguments makes it work
msg385247 - (view) Author: Ofek Lev (Ofekmeister) * Date: 2021-01-19 08:25
I'm using the deprecated typing.Callable instead now and that works
msg385262 - (view) Author: Ken Jin (kj) * (Python committer) Date: 2021-01-19 14:20
Hello, this issue is a byproduct of issue42195. It has already been fixed on Python 3.10, and on Python 3.9.2 (which isn't out yet). You can see the what's new for it here https://docs.python.org/3/whatsnew/3.9.html#notable-changes-in-python-3-9-2.

The expected release date for Python 3.9.2 is Monday, 2021-02-15 according to PEP 596 https://www.python.org/dev/peps/pep-0596/.

For now, I guess you'll have to use the old typing.Callable, then update it in newer versions of Python.

On Python 3.10a4:
>>> from typing import Optional
>>> from collections.abc import Callable
>>> Optional[Callable[[bytes], bytes]]
typing.Optional[collections.abc.Callable[[bytes], bytes]]
msg385264 - (view) Author: Ofek Lev (Ofekmeister) * Date: 2021-01-19 14:48
Ah I see, thanks!
History
Date User Action Args
2022-04-11 14:59:40adminsetgithub: 87131
2021-01-19 14:48:11Ofekmeistersetstatus: open -> closed
resolution: fixed
messages: + msg385264

stage: resolved
2021-01-19 14:20:04kjsetnosy: + kj
messages: + msg385262
2021-01-19 08:25:07Ofekmeistersetmessages: + msg385247
2021-01-19 08:11:26Ofekmeistercreate