Karthikeyan Singaravelan <tir.kar...@gmail.com> added the comment:
I tried ast module and it passes my test cases but I found another case since object.__name__ returns unqualified name thus matching against nested class names might conflict with the ones on the top level. Example is below where there is Spam at the module level and NestedA.Spam which is defined inside another class. getsource(NestedA.Spam) returns the source for Spam which is also unexpected. It seems ast module also doesn't call visit_ClassDef on nested class definitions thus my approach also couldn't detect NestedA.Spam and returns source for Spam. I wish class objects also had some kind of code attribute similar to functions so that line number is attached. I don't know the internals so I might be wrong here. I am adding Yury. import inspect class Egg: def func(self): pass class NestedA: class Egg: pass print(inspect.getsource(NestedA.Egg)) print(inspect.getsource(Egg)) # Output class Egg: def func(self): pass class Egg: def func(self): pass ---------- nosy: +yselivanov _______________________________________ Python tracker <rep...@bugs.python.org> <https://bugs.python.org/issue35113> _______________________________________ _______________________________________________ Python-bugs-list mailing list Unsubscribe: https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com