Skip to content

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

Closed as duplicate

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

  1. added
    type-bugAn unexpected behavior, bug, or error
    on Oct 28, 2025
  2. picnixz commented on Oct 28, 2025

    @picnixz
    Member

    It could be a duplicate of #139838.

  3. added
    stdlibStandard Library Python modules in the Lib/ directory
    pendingThe issue will be closed if no feedback is provided
    on Oct 28, 2025
  4. picnixz commented on Oct 28, 2025

    @picnixz
    Member
  5. serhiy-storchaka commented on Oct 29, 2025

    @serhiy-storchaka
    Member

    Yes, this is a duplicate of #139838. Although this is more realistic use case which can occur in real code.

  6. picnixz commented on Oct 29, 2025

    @picnixz
    Member

    I'm closing this one but forwards the repro

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