Skip to content

getsource returns incomplete code when lambda passed to decorator #221

@mmckerns

Description

@mmckerns

In the code below, the results for fun_2 are incomplete:

class Decorator(object):
    def __init__(self, item):
        self.item = item
        self.func = None
    def __call__(self, func):
        self.func = func
        return self

@Decorator(42)
def fun_1():
    pass

@Decorator(lambda x: 42)
def fun_2():
    pass

@Decorator(fun_2.item)
def fun_3():
    pass

import inspect
print("Inspect results")
print(inspect.getsource(fun_1.func))
print(inspect.getsource(fun_2.func))
print(inspect.getsource(fun_3.func))

import dill
print("Dill results")
print(dill.source.getsource(fun_1.func))
print(dill.source.getsource(fun_2.func))
print(dill.source.getsource(fun_3.func))

The expected results are:

@Decorator(lambda x: 42)
def fun_2():
    pass

but what is obtained is

@Decorator(lambda x: 42)

Both dill and inspect fail with the same results.

Note this issue was reported on StackOverflow: http://stackoverflow.com/questions/43063658

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions