python / python/cpython

Misbehavior of `f.__code__.co_firstlineno` for decorated functions

Open
#139,838 7 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Bug report

Bug description:

See also: #139783

Compare:

dummy = lambda f: f                 # L1
@dummy                              # L2
def f():                            # L3
    return                          # L4
print(f.__code__.co_firstlineno)    # L5  # result = 2

with

dummy = lambda f: f                 # L1
pass                                # L2
def f():                            # L3
    return                          # L4
print(f.__code__.co_firstlineno)    # L5  # result = 3

and

dummy = lambda f: f                 # L1
@\
dummy                               # L3
def f():                            # L4
    return                          # L5
print(f.__code__.co_firstlineno)    # L6  # result = 3

In the last example, the line continuation after @ is not handled correctly and the first line for f.__code__ then becomes the line at dummy. Because of that, the values returned by inspect.findsource(f) will not be consistent and inspect.getsourcelines will not use the correct source (it will only start at "dummy" and will not contain the "@" part).

@serhiy-storchaka suggested that this is an issue in the AST parser. I however don't know how we should actually fix this.

Consequences to inspect.getsource and other introspection functions:
import inspect

def outer(*args, **kwargs):
    def inner(f):
        return f
    return deco

@(
    outer(
        ...
    )
)
def f():
    return "hello"

print(inspect.getsource(f))

On Python 3.12, the output is expected:

@(
    outer(
        ...
    )
)
def func():
    return "hello"

On Python 3.13.7, the output is missing the first line @(:

    outer(
        ...
    )
)
def func():
    return "hello"

On Python 3.13.9 and main, we are missing @ and outer parentheses:

    outer(
        ...
    )
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

Reproduce the three decorator examples and the nested-decorator example, then inspect the AST parser behavior alongside inspect.findsource, inspect.getsourcelines, and inspect.getsource. Done means decorated functions report the correct first source line, including continued decorators and opening parentheses, and introspection returns the complete decorator source.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
compilers, devtools
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.