[issue46873] inspect.getsource with some lambdas in decorators does not get the full source

Adam Hopkins report at bugs.python.org
Sun Feb 27 08:15:29 EST 2022


New submission from Adam Hopkins <adam at amhopkins.com>:

I believe the following produces an unexpected behavior:

    from inspect import getsource


    def bar(*funcs):
        def decorator(func):
            return func

        return decorator


    @bar(lambda x: bool(True), lambda x: False)
    async def foo():
        ...


    print(getsource(foo))

The output shows only the decorator declaration and none of the function:

    @bar(lambda x: bool(True), lambda x: False)


>From my investigation, it seems like this requires the following conditions to be true:
- lambdas are passed in decorator arguments
- there is more than one lambda
- at least one of the lambdas has a function call

Passing the lambdas as default function arguments seems okay:

    async def foo(bar=[lambda x: bool(True), lambda x: False]):
        ...

A single lambda seems okay:

    @bar(lambda x: bool(True))
    async def foo():
        ...

Lambdas with no function calls also seem okay:

    @bar(lambda x: not x, lambda: True)
    async def foo():
        ...

Tested this on:
- Python 3.10.2
- Python 3.9.9
- Python 3.8.11
- Python 3.7.12

----------
messages: 414149
nosy: ahopkins2
priority: normal
severity: normal
status: open
title: inspect.getsource with some lambdas in decorators does not get the full source
versions: Python 3.10, Python 3.7, Python 3.8, Python 3.9

_______________________________________
Python tracker <report at bugs.python.org>
<https://bugs.python.org/issue46873>
_______________________________________


More information about the Python-bugs-list mailing list