Skip to content

inspect.getsource regressions for functions decorated with multi-line @(...) decorators. #140731

Description

@yilei

Bug report

Bug description:

Here is the code snippet:

import inspect
import sys


def decorator_builder(**kwargs):
    class DecoratorChain:
        def __init__(self, **kw):
            self.kwargs = kw

        def with_option(self, value):
            self.kwargs["option"] = value
            return self

        def __call__(self, fn):
            return fn

    return DecoratorChain(**kwargs)


@(
    decorator_builder(
        version=1,
        name="example",
    ).with_option("test")
)
def function():
    return "hello"


print("Source is:")
print(inspect.getsource(function))

On Python 3.12, the output is expected:

Source is:
@(
    decorator_builder(
        version=1,
        name="example",
    ).with_option("test")
)
def function():
    return "hello"

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

Source is:
    decorator_builder(
        version=1,
        name="example",
    ).with_option("test")
)
def function():
    return "hello"

On Python 3.13.9 and current main branch, the output just contains the lines inside @(...):

Source is:
    decorator_builder(
        version=1,
        name="example",
    ).with_option("test")

I bisected the changes:

  1. inspect.getsource does not work for a class-defined code object #116987 resulted in the missing @( first line.
  2. gh-137477: Fix inspect.getblock() for generator expressions #137488 (and the [3.13] gh-137477: Fix inspect.getblock() for generator expressions (GH-137488) #137995 backport) resulted in containing only the lines inside @(...)

CPython versions tested on:

3.13, 3.14, CPython main branch

Operating systems tested on:

Linux

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    pendingThe issue will be closed if no feedback is providedstdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions