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

allow constructors to be documented separately from class #60772

Closed
cjerdonek opened this issue Nov 28, 2012 · 8 comments
Closed

allow constructors to be documented separately from class #60772

cjerdonek opened this issue Nov 28, 2012 · 8 comments
Labels
docs Documentation in the Doc dir type-feature A feature request or enhancement

Comments

@cjerdonek
Copy link
Member

BPO 16568
Nosy @birkenfeld, @terryjreedy, @ncoghlan, @ezio-melotti, @merwok, @cjerdonek

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 2012-11-28.09:03:32.756>
labels = ['type-feature', 'docs']
title = 'allow constructors to be documented separately from class'
updated_at = <Date 2012-12-03.08:10:13.511>
user = 'https://github.com/cjerdonek'

bugs.python.org fields:

activity = <Date 2012-12-03.08:10:13.511>
actor = 'Arfrever'
assignee = 'docs@python'
closed = False
closed_date = None
closer = None
components = ['Documentation']
creation = <Date 2012-11-28.09:03:32.756>
creator = 'chris.jerdonek'
dependencies = []
files = []
hgrepos = []
issue_num = 16568
keywords = []
message_count = 6.0
messages = ['176524', '176567', '176619', '176621', '176623', '176625']
nosy_count = 8.0
nosy_names = ['georg.brandl', 'terry.reedy', 'ncoghlan', 'ezio.melotti', 'eric.araujo', 'Arfrever', 'chris.jerdonek', 'docs@python']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue16568'
versions = []

@cjerdonek
Copy link
Member Author

This issue is to settle on and provide a way to document the constructor of a class separately from the overall summary/description of a class.

This is something that Ezio, Nick, and I discussed briefly on IRC a few hours ago. We all see the value in it.

Currently, Sphinx's "class" directive is used in many places to document the constructor of a class. One drawback of this approach is that linking to the class using the :class: role links to the constructor as opposed to the class summary. As a result, in many cases the class description needs to be added before the class directive, and a second link needs to be created and used for the overall description.

One way to address this would be to create a new "constructor" directive similar to directives like method, classmethod, and staticmethod. The constructor documentation could then be hyperlinked to using a new :constructor: role (or perhaps :ctor: to go with the pattern of 4-letter roles). The class summary could then go immediately after the class directive, with the constructor directive following, as follows:

.. class:: Foo

Description of Foo.

.. constructor:: Foo(bar=1)

  Return a Foo.

This could render as--

class **Foo**

Description of Foo.

*constructor* **Foo**(bar=1)

  Return a Foo.

It is possible that something similar could be achieved by abusing the method directive for constructors and linking to them using :meth:`~Foo.Foo`, but that wouldn't be ideal in a few respects.

Nick also raised a related issue for base classes, etc. I will let him speak to that issue, which might be best as part of another new issue.

@cjerdonek cjerdonek added docs Documentation in the Doc dir type-feature A feature request or enhancement labels Nov 28, 2012
@ezio-melotti
Copy link
Member

I was wondering if this should be called initializer instead of constructor. Another idea is to keep using the "method" directive and use :initializer: to differentiate it from the others. This might be easier to implement, but OTOH is not consistent with the staticmethod and classmethod directives though.

@merwok
Copy link
Member

merwok commented Nov 29, 2012

I’m interested in an explanation of the value of doing this.

In the threading docs for example, the classes (i.e. initializers) are documented in one section, with awkward links to “Thread objects”, “Lock objects”, etc. This felt much more cumbersome to me than more modern class/attribute/method combos. Isn’t it the simplest thing to document one class, usage, constructors and all attributes, in one place?

On the other hand, I don’t have the view of an outsider anymore, so I can’t say what makes sense / reads better for the doc’s audience (as opposed to doc editors).

@ezio-melotti
Copy link
Member

This came up while documenting the str type in datamodel.rst (IIRC), and linking to it from functions.rst.

On one hand you want to say what the str type is, on the other hand you want to document the constructor and explain how can you use str() to obtain str objects.
Putting both under the constructor doesn't work too well, especially when there's a lot of text. Putting the str type description in a plain paragraph before the "class" directive doesn't work too well either, because :class:`str` will link to the constructor documentation.

The idea is to keep the two separate but still provide an easy way to link to the class without missing the general description.

@merwok
Copy link
Member

merwok commented Nov 29, 2012

For all the built-in types, I see the value now.

Thinking aloud: Separating the namespaces used by the str and func roles would probably require too many changes in Sphinx.

The current way is to use the function directive to document the class/constructor usage in one place, and use ref to link to the section describing the class. You can also have the cool class/method combo if you configure the class directive with the noindex option. Is that not good enough?

@ezio-melotti
Copy link
Member

For all the built-in types, I see the value now.

The same applies for non built-in types too though (e.g. http://docs.python.org/3.4/library/collections.html). The only difference is that the built-in ones have entries in functions.rst and use more links.

Is that not good enough?

Not really, and creating additional links and suppressing others with :noindex: feels wrong. The idea is that :class:`str` should just work and link to the right place.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
@AA-Turner
Copy link
Member

I think just documenting .. method:: __init__ would work -- this is for example done in difflib.

A

@AA-Turner AA-Turner added the pending The issue will be closed if no feedback is provided label Sep 22, 2023
@terryjreedy
Copy link
Member

In a file, the class and each method can have separate docstrings. In the class doc, there are separate entries for the class and the __init__ methods, and occasionally the __new__ method. Elsewhere in the doc, we have class and method role directives. I don't see a need for new constructor and initializer directives. In any case, I think any further (meta) discussion is a doc or devguide discussion.

@terryjreedy terryjreedy closed this as not planned Won't fix, can't repro, duplicate, stale Dec 13, 2023
@terryjreedy terryjreedy removed the pending The issue will be closed if no feedback is provided label Dec 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs Documentation in the Doc dir type-feature A feature request or enhancement
Projects
None yet
Development

No branches or pull requests

5 participants