[New-bugs-announce] [issue37922] inspect.getsource returns wrong class definition when multiple class definitions share the same name (but are defined in different scopes)
Leonard Truong
report at bugs.python.org
Thu Aug 22 18:46:07 EDT 2019
New submission from Leonard Truong <leonard at 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 <report at bugs.python.org>
<https://bugs.python.org/issue37922>
_______________________________________
More information about the New-bugs-announce
mailing list