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.

Author lennyt
Recipients lennyt
Date 2019-08-22.22:46:06
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1566513967.26.0.232695535045.issue37922@roundup.psfhosted.org>
In-reply-to
Content
Here's a case where `inspect.getsource` returns the wrong class definition when a file contains multiple class definitions with the same name. This pattern is valid runtime behavior when the class definitions are inside different scopes (e.g. a factory pattern where classes are defined and returned inside a function).

```
import inspect


def foo0():
    class Foo:
        x = 4
    return Foo


def foo1():
    class Foo:
        x = 5
    return Foo


print(inspect.getsource(foo1()))

print(foo1().x)
print(foo0().x)
```

Running this file produces
```
❯ python inspect-getsource-issue.py
    class Foo:
        x = 4

5
4
```
History
Date User Action Args
2019-08-22 22:46:07lennytsetrecipients: + lennyt
2019-08-22 22:46:07lennytsetmessageid: <1566513967.26.0.232695535045.issue37922@roundup.psfhosted.org>
2019-08-22 22:46:07lennytlinkissue37922 messages
2019-08-22 22:46:07lennytcreate