Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Missing carets when reporting an "object is not callable" error #128832

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

Closed
PierreQuentel opened this issue Jan 14, 2025 · 4 comments
Closed

Missing carets when reporting an "object is not callable" error #128832

PierreQuentel opened this issue Jan 14, 2025 · 4 comments
Labels
type-bug An unexpected behavior, bug, or error

Comments

@PierreQuentel
Copy link
Contributor

PierreQuentel commented Jan 14, 2025

Bug report

Bug description:

There is a minor inconsistency between the way "not subscriptable" and "not callable" errors are reported.

When an object is not subscriptable, the exception trace includes the carets ~ and ^ for the subscript value and slice.

>>> t = 0
>>> t[0]
Traceback (most recent call last):
  File "<python-input-1>", line 1, in <module>
    t[0]
    ~^^^
TypeError: 'int' object is not subscriptable

The carets are also displayed if the attempted subscript is assigned

>>> x = t[0]
Traceback (most recent call last):
  File "<python-input-4>", line 1, in <module>
    x = t[0]
        ~^^^
TypeError: 'int' object is not subscriptable

It is the same when an object is not callable

>>> t(0)
Traceback (most recent call last):
  File "<python-input-2>", line 1, in <module>
    t(0)
    ~^^^
TypeError: 'int' object is not callable

but when the attempted call is assigned, we lose the carets

>>> x = t(0)
Traceback (most recent call last):
  File "<python-input-5>", line 1, in <module>
    x = t(0)
TypeError: 'int' object is not callable

instead of the expected

>>> x = t(0)
Traceback (most recent call last):
  File "<python-input-5>", line 1, in <module>
    x = t(0)
        ~^^^
TypeError: 'int' object is not callable

CPython versions tested on:

3.13

Operating systems tested on:

Windows

@PierreQuentel PierreQuentel added the type-bug An unexpected behavior, bug, or error label Jan 14, 2025
@Eclips4
Copy link
Member

Eclips4 commented Jan 14, 2025

Hello!
Thanks for the report.
After taking a look at Lib/traceback.py:

cpython/Lib/traceback.py

Lines 712 to 723 in bbd3300

match statement:
case ast.Return(value=ast.Call()):
if isinstance(statement.value.func, ast.Name):
value = statement.value
case ast.Assign(value=ast.Call()):
if (
len(statement.targets) == 1 and
isinstance(statement.targets[0], ast.Name)
):
value = statement.value
if value is not None and _spawns_full_line(value):
return False

It looks like intended behavior, cc @pablogsal

@Eclips4
Copy link
Member

Eclips4 commented Jan 14, 2025

Ah, yes, this is expected. See #99180.

@Eclips4 Eclips4 closed this as not planned Won't fix, can't repro, duplicate, stale Jan 14, 2025
@PierreQuentel
Copy link
Contributor Author

Yes, this is how it works now, but it's hard to see why assigning to a failing subscript (x= t[0]) gives a different result from assigning to a failing call (x = t(0)).

@Eclips4
Copy link
Member

Eclips4 commented Jan 14, 2025

Yes, this is how it works now, but it's hard to see why assigning to a failing subscript (x= t[0]) gives a different result from assigning to a failing call (x = t(0)).

Please take a look at the #99180.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants