◐ Shell
clean mode source ↗

`inspect.getsource` (`findsource`) not working as expected when duplicate class is used

Bug report

Consider the following example:

import inspect

class Model:
    a = 1

class Model:
    a = 2

print(Model.a)
#> 2
print(inspect.getsource(Model))
#> class Model:
#>     a = 1

I think we should expect the last class to be used (it works as expected if we use functions instead of classes).

This issue was raised in an external project:

And I'm facing this issue while working on:

Let me know if this should be fixed, I'll take a deeper look at what was done in #10307 and see if I can make a PR

Your environment

  • CPython versions tested on: 3.11.4
  • Operating system and architecture: Ubuntu

Linked PRs