Skip to content

Inconsistent TypeError messages with callables #136327

Open
@Tapeline

Description

@Tapeline

Bug report

Bug description:

Consider this example:

>>> class A:
...     def __str__(self): return "some random text"
...     def __call__(self, a): ...

>>> A()()
Traceback (most recent call last):
  File "<python-input-16>", line 1, in <module>
    A()()
    ~~~^^
TypeError: A.__call__() missing 1 required positional argument: 'a'

>>> A()(*None)
Traceback (most recent call last):
  File "<python-input-17>", line 1, in <module>
    A()(*None)
    ~~~^^^^^^^
TypeError: some random text argument after * must be an iterable, not NoneType

The first one gets the callable's __qualname__. The second, on the other hand, does not, but should do so.

Another example, mentioned in #135975 (issuecomment link):

>>> exit(*None)
Traceback (most recent call last):
  File "<python-input-0>", line 1, in <module>
    exit(*None)
    ~~~~^^^^^^^
TypeError: Use exit() or Ctrl-Z plus Return to exit argument after * must be an iterable, not NoneType

Which just gets the exit's __repr__.

If this is recognized as unwanted behaviour, I'm ready to make a PR.

CPython versions tested on:

3.13

Operating systems tested on:

Windows

Edit: ** also does not work properly:

# the code from the first example
>>> A()(**1)
Traceback (most recent call last):
  File "<python-input-26>", line 1, in <module>
    A()(**1)
    ~~~^^^^^
TypeError: some random text argument after ** must be a mapping, not int

Linked PRs

Metadata

Metadata

Labels

interpreter-core(Objects, Python, Grammar, and Parser dirs)type-bugAn unexpected behavior, bug, or error

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions