python / python/cpython

Traceback Shows Too Much Info For Class Definition Exception

Open
#129,145 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

3.14 interpreter-core stdlib type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

Given the following:

class Meta(type):
    def __prepare__(cls, *args, **kwargs):
        raise Exception

class Spam(metaclass=Meta):
    a = 1
    b = 2
    c = 3
    d = 4
    e = 5
    f = 6

I get:

Traceback (most recent call last):
  File "<python-input-10>", line 1, in <module>
    class Spam(metaclass=Meta):
    ...<5 lines>...
        f = 6
  File "<python-input-9>", line 3, in __prepare__
    raise Exception

I would expect this to only show the compound statement's "header":

Traceback (most recent call last):
  File "<python-input-10>", line 1, in <module>
    class Spam(metaclass=Meta):
  File "<python-input-9>", line 3, in __prepare__
    raise Exception

CC @iritkatriel

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the traceback on the CPython main branch with the class and metaclass example in the issue. Trace how the interpreter formats the class definition's source span and look for existing traceback tests covering compound statements. Done means the traceback shows only the class header before the prepare exception.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.