Skip to content

inspect.getsource(SomeClass) doesn't show @decorators #60060

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
takluyver mannequin opened this issue Sep 3, 2012 · 4 comments
Closed

inspect.getsource(SomeClass) doesn't show @decorators #60060

takluyver mannequin opened this issue Sep 3, 2012 · 4 comments
Labels
3.8 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@takluyver
Copy link
Mannequin

takluyver mannequin commented Sep 3, 2012

BPO 15856
Nosy @takluyver, @1st1, @tirkarthi
Superseder
  • bpo-35113: inspect.getsource returns incorrect source for classes when class definition is part of multiline strings
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2019-08-31.10:47:09.340>
    created_at = <Date 2012-09-03.15:13:44.404>
    labels = ['3.8', 'type-bug', 'library']
    title = "inspect.getsource(SomeClass) doesn't show @decorators"
    updated_at = <Date 2020-04-18.16:19:39.465>
    user = 'https://github.com/takluyver'

    bugs.python.org fields:

    activity = <Date 2020-04-18.16:19:39.465>
    actor = 'xtreak'
    assignee = 'none'
    closed = True
    closed_date = <Date 2019-08-31.10:47:09.340>
    closer = 'serhiy.storchaka'
    components = ['Library (Lib)']
    creation = <Date 2012-09-03.15:13:44.404>
    creator = 'takluyver'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 15856
    keywords = []
    message_count = 4.0
    messages = ['169767', '222682', '340966', '366726']
    nosy_count = 3.0
    nosy_names = ['takluyver', 'yselivanov', 'xtreak']
    pr_nums = []
    priority = 'normal'
    resolution = 'duplicate'
    stage = 'resolved'
    status = 'closed'
    superseder = '35113'
    type = 'behavior'
    url = 'https://bugs.python.org/issue15856'
    versions = ['Python 3.8']

    @takluyver
    Copy link
    Mannequin Author

    takluyver mannequin commented Sep 3, 2012

    Since bug bpo-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.

    @takluyver takluyver mannequin added stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Sep 3, 2012
    @BreamoreBoy
    Copy link
    Mannequin

    BreamoreBoy mannequin commented Jul 10, 2014

    @thomas sorry about the delay in getting back to you.

    @tirkarthi
    Copy link
    Member

    This should be resolved by bpo-35113 . 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.

    @tirkarthi tirkarthi added the 3.8 (EOL) end of life label Apr 27, 2019
    @tirkarthi
    Copy link
    Member

    New changeset 696136b by Karthikeyan Singaravelan in branch 'master':
    bpo-35113: Fix inspect.getsource to return correct source for inner classes (bpo-10307)
    696136b

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.8 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    2 participants