-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
DOC: add guide on shared docstrings #20016
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
Changes from 1 commit
0e43660
afae00a
b040ab1
47e51e7
b6512b5
8c221ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1077,5 +1077,78 @@ The branch will still exist on GitHub, so to delete it there do:: | |
|
||
git push origin --delete shiny-new-feature | ||
|
||
Sharing Docstrings | ||
================== | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this line is not long enough |
||
Pandas has a system for sharing docstrings, with slight variations, between | ||
classes. This helps us keep docstrings consistent, while keeping things clear | ||
for the user reading. It comes at the cost of some complexity when writing. | ||
|
||
Each shared docstring will have a base template with variables, like | ||
``%(klass)s``. The variables filled in later on using the ``Substitution`` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. filled -> are filled |
||
decorator. Finally, docstrings can be appended to with the ``Appender`` | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we reference the Substituion/Appender via links to the code? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. AFAIK, there isn't a good way to link to a specific function. I think you can only do a specific line (with or without a commit). If we pin the commit things may go out of date. If don't do a specific commit then the line number will become incorrect. |
||
decorator. | ||
|
||
In this example, we'll create a parent docstring normally (this is like | ||
``pandas.core.generic.NDFrame``. Then we'll have two children (like | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. we'll -> we will, don't use contractions :> |
||
``pandas.core.series.Series`` and ``pandas.core.frame.DataFrame``). We'll | ||
substitute the children's class names in this docstring. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. childrens' There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Children is a plural noun not ending in "s", so children's is the correct plural possessive. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. hmm, yeah never get these right :>. We'll -> we will |
||
|
||
.. code-block:: python | ||
|
||
class Parent: | ||
def my_function(self): | ||
"""Apply my function to %(klass)s.""" | ||
... | ||
|
||
class ChildA(Parent): | ||
@Substitution(klass="ChildA") | ||
@Appender(Parent.my_function.__doc__) | ||
def my_function(self): | ||
... | ||
|
||
class ChildB(Parent): | ||
@Substitution(klass="ChildB") | ||
@Appender(Parent.my_function.__doc__) | ||
def my_function(self): | ||
... | ||
|
||
The resulting docstrings are | ||
|
||
.. code-block:: python | ||
|
||
>>> print(Parent.my_function.__doc__) | ||
Apply my function to %(klass)s. | ||
>>> print(ChildA.my_function.__doc__) | ||
Apply my function to ChildA. | ||
>>> print(ChildB.my_function.__doc__) | ||
Apply my function to ChildB. | ||
|
||
Notice two things: | ||
|
||
1. We "append" the parent docstring to the children docstrings, which are | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we use |
||
initially empty. | ||
2. Python decorators are applied inside out. So the order is Append then | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. double backticks on Append/Substitution |
||
Substitution, even though Substitution comes first in the file. | ||
|
||
Our files will often contain a module-level ``_shared_doc_kwargs`` with some | ||
common substitution values (things like ``klass``, ``axes``, etc). | ||
|
||
You can substitute and append in one shot with something like | ||
|
||
.. code-block:: python | ||
|
||
@Appender(template % _shared_doc_kwargs) | ||
def my_function(self): | ||
... | ||
|
||
where ``template`` may come from a module-level ``_shared_docs`` dictionary | ||
mapping function names to docstrings. Wherever possible, we prefer using | ||
``Appender`` and ``Substitution``, since the docstring-writing processes is | ||
slightly closer to normal. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't fully understand this reasoning (both with using |
||
|
||
See ``pandas.core.generic.NDFrame.fillna`` for an example template, and | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you point to actual code There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same issue with linking specific lines / commits, though maybe this one is OK, since we're giving an example. But unfortunately I can't link to the correct version, since the correction is in this PR :) |
||
``pandas.core.series.Series.fillna`` and ``pandas.core.generic.frame.fillna`` | ||
for the filled versions. | ||
|
||
.. _Gitter: https://gitter.im/pydata/pandas |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you add a ref here
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will do. Going to wait till the docstring guide is merged and I move it to that document so that the name makes sense.