[issue37922] inspect.getsource returns wrong class definition when multiple class definitions share the same name (but are defined in different scopes)

2019-08-22 Thread Leonard Truong


Leonard Truong  added the comment:

Turns out this is a documented issue in the source code: 
https://github.com/python/cpython/blob/3.7/Lib/inspect.py#L794-L796

--

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



[issue37922] inspect.getsource returns wrong class definition when multiple class definitions share the same name (but are defined in different scopes)

2019-08-22 Thread Leonard Truong

New submission from Leonard Truong :

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 
<https://bugs.python.org/issue37922>
___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com