python / python/cpython

Weird `inspect.getsource` behavior with generators

Open
#121,331 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

stdlib type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

Bug report

Bug description:

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()

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

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

Further, presumably not a bug but a current limitation, in general using getsource on generator-expression objects returns the code of the function they were created in, rather than the code of the generator expression itself, which can make things confusing when there are multiple such expressions, e.g.:

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

g1, g2 = get_2_generators()
getsource(g1.gi_code)
>>>'def get_2_generators():\n    my_generator_1 = (x*2 for x in range(5)) \n    my_generator_2 = (x*3 for x in range(6))\n    return my_generator_1, my_generator_2\n'

getsource(g2.gi_code)
>>>'def get_2_generators():\n    my_generator_1 = (x*2 for x in range(5)) \n    my_generator_2 = (x*3 for x in range(6))\n    return my_generator_1, my_generator_2\n'

Can we fix both of the above by making getsource return the code of the generator expression itself?

CPython versions tested on:

3.12

Operating systems tested on:

Windows

Linked PRs
  • gh-121933

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with inspect.getsource and the generator gi_code examples in the report, then review linked PR gh-121933 for the current direction. Compare generator-function and generator-expression cases, and verify that completed work distinguishes multiple generators instead of returning unrelated enclosing source.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
developer-experience
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.