Is this behavior with inspect.getsource with generator expressions a bug?

We can use inspect.getsource to look at the source code of a generator object by calling the .gi_code paramter:

def my_generator():
    for x in range(5):
        yield x*2

g = my_generator()
>>>'def my_generator():\n    for x in range(5):\n        yield x*2\n'

When doing this for a generator expression returned from a function this approach gives the source of the entire function, rather than the generator exrpression itself:

def get_a_generator():
    my_generator = (x*2 for x in range(5))
    return my_generator

g = get_a_generator()
>>>'def get_a_generator():\n    my_generator = (x*2 for x in range(5))\n    return my_generator\n'

The above is presumably just a limitation, rather than a bug, though it would be nice to be able to get just the source of the expression itself somehow.

However, if you return two generators (one from a generator function and one a generator expression) from a function and use getsource on each, you get the code for the generator function for both.

def get_2_generators():
    def my_generator_1():
        for x in range(5):
            yield x*2 
    my_generator_2 = (x*3 for x in range(6))
    return my_generator_1(), my_generator_2

g1, g2 = get_2_generators()

>>>'    def my_generator_1():\n        for x in range(5):\n            yield x*2 \n'

>>>'    def my_generator_1():\n        for x in range(5):\n            yield x*2 \n'

Is this a bug? Or am I just pushing getsource beyond what is reasonable to expect?

getsource is imo a very messy tool that has never been helpful in my attempts to do meta programming.

Yes, I would consider this a bug, but it might not be reasonably fixable.

Cool, have submitted a bug report, thanks.