Skip to content
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

inspect.getdoc could examine the __class__ cell for dynamically generated subclasses #82784

Open
anntzer mannequin opened this issue Oct 27, 2019 · 1 comment
Open

inspect.getdoc could examine the __class__ cell for dynamically generated subclasses #82784

anntzer mannequin opened this issue Oct 27, 2019 · 1 comment
Labels
3.9 only security fixes stdlib Python modules in the Lib dir

Comments

@anntzer
Copy link
Mannequin

anntzer mannequin commented Oct 27, 2019

BPO 38603
Nosy @1st1, @anntzer
PRs
  • bpo-38603: Inherit docstrings in dynamically generated subclasses if possible #16957
  • 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 = None
    created_at = <Date 2019-10-27.15:24:34.311>
    labels = ['library', '3.9']
    title = 'inspect.getdoc could examine the __class__ cell for dynamically generated subclasses'
    updated_at = <Date 2019-11-24.20:44:00.320>
    user = 'https://github.com/anntzer'

    bugs.python.org fields:

    activity = <Date 2019-11-24.20:44:00.320>
    actor = 'BTaskaya'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['Library (Lib)']
    creation = <Date 2019-10-27.15:24:34.311>
    creator = 'Antony.Lee'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 38603
    keywords = ['patch']
    message_count = 1.0
    messages = ['355472']
    nosy_count = 2.0
    nosy_names = ['yselivanov', 'Antony.Lee']
    pr_nums = ['16957']
    priority = 'normal'
    resolution = None
    stage = 'patch review'
    status = 'open'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue38603'
    versions = ['Python 3.9']

    @anntzer
    Copy link
    Mannequin Author

    anntzer mannequin commented Oct 27, 2019

    Currently, inspect.getdoc() fails to inherit docstrings in dynamically generated subclasses, such as

    class Base:
        def method(self): "some docstring"
    
    def make_subclass():
        class subclass(Base):
            def method(self): return super().method()
        return subclass
    
    subclass = make_subclass()
    inspect.getdoc(subclass.method)  # => returns None
    

    because inspect._findclass() tries to find the base class by parsing subclass.method.__qualname__ which is "make_subclass.<locals>.subclass.method" and chokes over .<locals>..

    In the case where the method does rely on super(), there is another way we can go back to the "owning" class of the method: by looking up the contents of the __class__ cell (which is set up to make 0-arg super()).

    Perhaps a __class__ cell could even be set up for all methods defined in dynamically created subclasses (i.e. whose __qualname__ includes .<locals>.), to help with introspection?

    @anntzer anntzer mannequin added 3.9 only security fixes stdlib Python modules in the Lib dir labels Oct 27, 2019
    @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.9 only security fixes stdlib Python modules in the Lib dir
    Projects
    None yet
    Development

    No branches or pull requests

    0 participants