This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Unsupported provider

classification
Title: inspect.getsource(SomeClass) doesn't show @decorators
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.8
process
Status: closed Resolution: duplicate
Dependencies: Superseder: inspect.getsource returns incorrect source for classes when class definition is part of multiline strings
View: 35113
Assigned To: Nosy List: takluyver, xtreak, yselivanov
Priority: normal Keywords:

Created on 2012-09-03 15:13 by takluyver, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Messages (4)
msg169767 - (view) Author: Thomas Kluyver (takluyver) * Date: 2012-09-03 15:13
Since bug #1006219 was fixed, inspect.getsource(func) has returned the source of a function including any decorators on the function. But doing the same with a class, the returned source doesn't include decorators.

With functions, the co_firstlineno attribute of the code object points to the start of the decorators. With classes, that's not possible, so it's likely that a bit more regex trickery will be needed to scan back to decorators.

I've confirmed the same thing happens in 3.2 and 2.7, and looking at the code of inspect.py in trunk, it looks like it will do the same thing.
msg222682 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2014-07-10 18:09
@Thomas sorry about the delay in getting back to you.
msg340966 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2019-04-27 04:49
This should be resolved by issue35113 . I have rewritten the getsource function for classes to use AST to deal with nested classes. The PR adds support for showing the decorator in the source. I would propose closing this as a duplicate of the other issue.
msg366726 - (view) Author: Karthikeyan Singaravelan (xtreak) * (Python committer) Date: 2020-04-18 16:19
New changeset 696136b993e11b37c4f34d729a0375e5ad544ade by Karthikeyan Singaravelan in branch 'master':
bpo-35113: Fix inspect.getsource to return correct source for inner classes (#10307)
https://github.com/python/cpython/commit/696136b993e11b37c4f34d729a0375e5ad544ade
History
Date User Action Args
2022-04-11 14:57:35adminsetgithub: 60060
2020-04-18 16:19:39xtreaksetmessages: + msg366726
2019-08-31 10:47:09serhiy.storchakasetstatus: open -> closed
superseder: inspect.getsource returns incorrect source for classes when class definition is part of multiline strings
resolution: duplicate
stage: resolved
2019-04-27 04:49:56xtreaksetnosy: + xtreak

messages: + msg340966
versions: + Python 3.8, - Python 3.4, Python 3.5
2019-04-26 18:51:07BreamoreBoysetnosy: - BreamoreBoy
2014-07-10 18:09:51BreamoreBoysetnosy: + BreamoreBoy, yselivanov

messages: + msg222682
versions: + Python 3.4, Python 3.5, - Python 3.2
2012-09-03 15:13:44takluyvercreate