X Tutup
The Wayback Machine - https://web.archive.org/web/20241115135618/https://github.com/python/cpython/issues/63603
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

Simplify per-instance control of help() output #63603

Open
ncoghlan opened this issue Oct 26, 2013 · 5 comments
Open

Simplify per-instance control of help() output #63603

ncoghlan opened this issue Oct 26, 2013 · 5 comments
Labels
stdlib Python modules in the Lib dir type-feature A feature request or enhancement

Comments

@ncoghlan
Copy link
Contributor

BPO 19404
Nosy @ncoghlan, @1st1

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 2013-10-26.07:22:18.105>
labels = ['type-feature', 'library']
title = 'Simplify per-instance control of help() output'
updated_at = <Date 2015-07-21.08:07:37.737>
user = 'https://github.com/ncoghlan'

bugs.python.org fields:

activity = <Date 2015-07-21.08:07:37.737>
actor = 'ethan.furman'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation = <Date 2013-10-26.07:22:18.105>
creator = 'ncoghlan'
dependencies = []
files = []
hgrepos = []
issue_num = 19404
keywords = []
message_count = 5.0
messages = ['201322', '201326', '201365', '201388', '201404']
nosy_count = 3.0
nosy_names = ['ncoghlan', 'python-dev', 'yselivanov']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue19404'
versions = ['Python 3.4']

@ncoghlan
Copy link
Contributor Author

While working on bpo-19330, I also took a look at whether or not I could fix the @contextmanager decorator to also provide useful help information on the resulting objects. This turned out to be difficult, since calling decorated functions produces instances of contextlib._GeneratorContextManager, and help() insists on showing the docs for that rather than a __doc__ attribute set on the context manager instance itself.

This behaviour appears to be ultimately due to "pydoc.render_doc()", which has a hardcoded list of conditions it checks to decide whether to document the instance directly or to provide the class documentation instead:
http://hg.python.org/cpython/file/2b904290b3b9/Lib/pydoc.py#l1544

This first idea that comes to mind is to simply add the additional condition that if "obj.__doc__ != type(obj).__doc__", then help() shouldn't replace the object with it's type. The simple trick that I originally explored was simply setting the __doc__ attribute on the _GeneratorContextManager instance based on the docstring of the decorated function, and I believe that approach might work for property instances, too (rather than needing to hardcode the isinstance check into render_doc()). It may even handle some of the other currently hardcoded conditions in that list.

@ncoghlan ncoghlan added stdlib Python modules in the Lib dir type-feature A feature request or enhancement labels Oct 26, 2013
@python-dev
Copy link
Mannequin

python-dev mannequin commented Oct 26, 2013

New changeset 09153a9a3bb9 by Nick Coghlan in branch 'default':
Close bpo-19330 by using public classes in contextlib
http://hg.python.org/cpython/rev/09153a9a3bb9

@ncoghlan
Copy link
Contributor Author

bpo-19031 is a report indicating that this is a problem for Enum instances as well.

@ethanfurman
Copy link
Member

Would it make sense to have the presence of a non-None __doc__ be a deciding factor? How often does an instance have a docstring where one would want the class info instead?

@ncoghlan
Copy link
Contributor Author

As far as I am aware, never. But "instance.__doc__" falls back to the
class, hence the != check rather than "is not None".

@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
stdlib Python modules in the Lib dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants
X Tutup