New submission from Leonard Truong <leon...@truong.io>:

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
```

----------
components: Library (Lib)
files: inspect-getsource-issue.py
messages: 350235
nosy: lennyt
priority: normal
severity: normal
status: open
title: inspect.getsource returns wrong class definition when multiple class 
definitions share the same name (but are defined in different scopes)
versions: Python 3.7
Added file: https://bugs.python.org/file48557/inspect-getsource-issue.py

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue37922>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to