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: Different repr for collections.abc.Callable and typing.Callable
Type: behavior Stage: resolved
Components: Versions: Python 3.9
process
Status: closed Resolution: duplicate
Dependencies: Superseder:
Assigned To: Nosy List: Prometheus3375, kj
Priority: normal Keywords:

Created on 2020-12-30 16:00 by Prometheus3375, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (5)
msg384068 - (view) Author: Svyatoslav (Prometheus3375) * Date: 2020-12-30 16:00
Was making some typing conversions to string and noticed a different behavior of collections.abc.Callable and typing.Callable here in 3.9.1. Issues issue42195 and issue40494 can be related.

>>> import collections, typing
>>> repr(collections.abc.Callable[[int], str])
"collections.abc.Callable[[<class 'int'>], str]"
>>> repr(typing.Callable[[int], str])
'typing.Callable[[int], str]'

The variant from collections is wrong, it is not consistent with other GenericAlias reprs like
>>> repr(tuple[list[float], int, str])
'tuple[list[float], int, str]'

The problem is actually in the list usage to denote callable arguments:
>>> repr(tuple[[float], int, str])
"tuple[[<class 'float'>], int, str]"

Here is the same error. This error disallows to use typing in eval/exec statements:
>>> c = collections.abc.Callable[[int], str]
>>> d = eval(repr(c))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 1
    collections.abc.Callable[[<class 'int'>], str]
                              ^
SyntaxError: invalid syntax

Ofc there is no such problem with typing.Callable:
>>> c = typing.Callable[[int], str]
>>> d = eval(repr(c))
>>> 

Interesting, if pass a list into typing.Tuple, an exception is raised:
>>> repr(typing.Tuple[[float], int, str])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "f:\software\python3.9\lib\typing.py", line 262, in inner
    return func(*args, **kwds)
  File "f:\software\python3.9\lib\typing.py", line 896, in __getitem__
    params = tuple(_type_check(p, msg) for p in params)
  File "f:\software\python3.9\lib\typing.py", line 896, in <genexpr>
    params = tuple(_type_check(p, msg) for p in params)
  File "f:\software\python3.9\lib\typing.py", line 151, in _type_check
    raise TypeError(f"{msg} Got {arg!r:.100}.")
TypeError: Tuple[t0, t1, ...]: each t must be a type. Got [<class 'float'>].

I think it is not correct that tuple accepts lists as generics, i. e. for tuple[[float], int, str] an exception as above should be raised. Bu this is the topic for other issue (maybe even already reported).
msg384070 - (view) Author: Ken Jin (kj) * (Python committer) Date: 2020-12-30 16:39
This was addressed in issue42195 and issue40494 as you pointed out :). It has been fixed in Python 3.10. Unfortunately the backport didn't make it in time for Python 3.9.1, and will come in Python 3.9.2 instead. Sadly there's not much to do about that.

On 3.10:

>>> import collections
>>> repr(collections.abc.Callable[[int], str])
'collections.abc.Callable[[int], str]'
msg384075 - (view) Author: Svyatoslav (Prometheus3375) * Date: 2020-12-30 17:42
Ok, thanks for the answer. Did I understand correctly that the issue will be fixed in 3.9.2 as well?
msg384080 - (view) Author: Ken Jin (kj) * (Python committer) Date: 2020-12-30 17:55
You're welcome, and yes you're right! You can read the news item 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/.
msg384082 - (view) Author: Svyatoslav (Prometheus3375) * Date: 2020-12-30 18:24
Thanks a lot! I am closing this issue.
History
Date User Action Args
2022-04-11 14:59:39adminsetgithub: 86952
2020-12-30 18:24:57Prometheus3375setstatus: open -> closed
resolution: duplicate
messages: + msg384082

stage: resolved
2020-12-30 17:55:23kjsetmessages: + msg384080
2020-12-30 17:42:03Prometheus3375setmessages: + msg384075
2020-12-30 16:39:33kjsetnosy: + kj
messages: + msg384070
2020-12-30 16:00:56Prometheus3375create