Skip to content

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

Description

@Viicos

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

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    stdlibStandard Library Python modules in the Lib/ directorytype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions