This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: inspect.getsource with some lambdas in decorators does not get the full source
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.10, Python 3.9
process
Status: closed Resolution: duplicate
Dependencies: Superseder:
Assigned To: Nosy List: AlexWaygood, ahopkins, yselivanov
Priority: normal Keywords: patch

Created on 2022-02-27 13:15 by ahopkins, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 31605 closed ahopkins, 2022-02-27 20:58
Messages (4)
msg414149 - (view) Author: Adam Hopkins (ahopkins) * Date: 2022-02-27 13:15
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
msg414154 - (view) Author: Alex Waygood (AlexWaygood) * (Python triager) Date: 2022-02-27 14:00
I'm removing 3.7 and 3.8 from the "versions" field, since they're currently only accepting patches for security-related bugs. But thanks for testing on those versions as well — that's useful information!
msg414156 - (view) Author: Adam Hopkins (ahopkins) * Date: 2022-02-27 14:15
Sorry about that. I am doing some more digging to see if I can find the route of it and a proposal for a non-breaking patch. The problem seems to be in BlockFinder.tokeneater.
msg414174 - (view) Author: Adam Hopkins (ahopkins) * Date: 2022-02-28 05:55
Duplicate of https://bugs.python.org/issue38854

Sorry I didn't come across our before submitting.
History
Date User Action Args
2022-04-11 14:59:56adminsetgithub: 91029
2022-02-28 05:55:53ahopkinssetstatus: open -> closed
resolution: duplicate
messages: + msg414174

stage: patch review -> resolved
2022-02-27 20:58:48ahopkinssetkeywords: + patch
stage: patch review
pull_requests: + pull_request29728
2022-02-27 14:21:28AlexWaygoodsettype: behavior
versions: - Python 3.7, Python 3.8
2022-02-27 14:15:27ahopkinssettype: behavior -> (no value)
messages: + msg414156
versions: + Python 3.7, Python 3.8
2022-02-27 14:00:51AlexWaygoodsetversions: - Python 3.7, Python 3.8
nosy: + AlexWaygood, yselivanov

messages: + msg414154

components: + Library (Lib)
type: behavior
2022-02-27 13:15:29ahopkinscreate