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

Optional callable raises TypeError #87131

Closed
ofek mannequin opened this issue Jan 19, 2021 · 5 comments
Closed

Optional callable raises TypeError #87131

ofek mannequin opened this issue Jan 19, 2021 · 5 comments
Labels
3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@ofek
Copy link
Mannequin

ofek mannequin commented Jan 19, 2021

BPO 42965
Nosy @ofek, @Fidget-Spinner

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-01-19.14:48:11.240>
created_at = <Date 2021-01-19.08:11:26.387>
labels = ['type-bug', 'library', '3.9']
title = 'Optional callable raises TypeError'
updated_at = <Date 2021-01-19.14:48:11.239>
user = 'https://github.com/ofek'

bugs.python.org fields:

activity = <Date 2021-01-19.14:48:11.239>
actor = 'Ofekmeister'
assignee = 'none'
closed = True
closed_date = <Date 2021-01-19.14:48:11.240>
closer = 'Ofekmeister'
components = ['Library (Lib)']
creation = <Date 2021-01-19.08:11:26.387>
creator = 'Ofekmeister'
dependencies = []
files = []
hgrepos = []
issue_num = 42965
keywords = []
message_count = 4.0
messages = ['385246', '385247', '385262', '385264']
nosy_count = 2.0
nosy_names = ['Ofekmeister', 'kj']
pr_nums = []
priority = 'normal'
resolution = 'fixed'
stage = 'resolved'
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue42965'
versions = ['Python 3.9']

@ofek
Copy link
Mannequin Author

ofek mannequin commented Jan 19, 2021

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

@ofek ofek mannequin added 3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Jan 19, 2021
@ofek
Copy link
Mannequin Author

ofek mannequin commented Jan 19, 2021

I'm using the deprecated typing.Callable instead now and that works

@Fidget-Spinner
Copy link
Member

Hello, this issue is a byproduct of bpo-42195. 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]]

@ofek
Copy link
Mannequin Author

ofek mannequin commented Jan 19, 2021

Ah I see, thanks!

@ofek ofek mannequin closed this as completed Jan 19, 2021
@ofek ofek mannequin closed this as completed Jan 19, 2021
@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
@herambnemlekar
Copy link

Callable

Which deprecated version did you use for typing.Callable? I am facing the same issue with Python 3.9

The-Compiler added a commit to qutebrowser/qutebrowser that referenced this issue Oct 13, 2024
Did run with ruff pretending to use Python 3.10,
because otherwise it won't reformat those:

    ruff check --select 'UP035' --fix --config 'target-version = "py310"' --unsafe-fixes

This is because collections.abc.Callable inside Optional[...] and Union[...] is
broken with Python 3.9.0 and 3.9.1:

asottile/pyupgrade#677
astral-sh/ruff#2690
python/cpython#87131

However, pylint can detect problematic usages (of which we only have one),
so we might as well use the new thing everywhere possible for consistency.
The-Compiler added a commit to qutebrowser/qutebrowser that referenced this issue Oct 13, 2024
Did run with ruff pretending to use Python 3.10,
because otherwise it won't reformat those:

    ruff check --select 'UP035' --fix --config 'target-version = "py310"' --unsafe-fixes

This is because collections.abc.Callable inside Optional[...] and Union[...] is
broken with Python 3.9.0 and 3.9.1:

asottile/pyupgrade#677
astral-sh/ruff#2690
python/cpython#87131

However, pylint can detect problematic usages (of which we only have one),
so we might as well use the new thing everywhere possible for consistency.
The-Compiler added a commit to qutebrowser/qutebrowser that referenced this issue Oct 15, 2024
Did run with ruff pretending to use Python 3.10,
because otherwise it won't reformat those:

    ruff check --select 'UP035' --fix --config 'target-version = "py310"' --unsafe-fixes

This is because collections.abc.Callable inside Optional[...] and Union[...] is
broken with Python 3.9.0 and 3.9.1:

asottile/pyupgrade#677
astral-sh/ruff#2690
python/cpython#87131

However, pylint can detect problematic usages (of which we only have one),
so we might as well use the new thing everywhere possible for consistency.

Also see #7098
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.9 only security fixes stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants