A recent commit to cpython which added a brief section mentioning the possible exceptions for os.chdir()
brought about a few questions with regards to the documentation conventions, specifically on the sections for individual functions within the standard library.
- Should existing sections which describe functions as “methods” be adjusted to “functions”?
Within the programming community across a number of different languages, the terms “method” and “function” have to come to roughly refer to the same thing, usually with each language having a specific term. Across the docs and in the Python community in general, “function” seems to be the more commonly used term, but periodically in the documentation for functions, there are sentences phrased as “This method…”. Ultimately this is just a matter of semantics, but for the purposes of documentation, consistency with terminology is important.
- What are the conventions for describing exceptions for a function?
In general, it seems be useful for users to be able to quickly refer to the docs to get a basic idea of exceptions which can be raised by a particular function. However, a number of functions (particularly in OS, where the commit was made) in the standard library have no description of exception behavior. This is not to say that every possible exception should be verbosely listed though.
Instead, the docs should probably focus on the most common ones which occur from “normal” usage of the function. For example, this was the section added to os.chdir()
in the final commit:
This function can raise :exc:
OSError
and subclasses such as
:exc:FileNotFoundError
, :exc:PermissionError
, and :exc:NotADirectoryError
.
Note how it does not explicitly state every possible OSError
which can occur from the usage of os.chdir()
.
- Where should the exceptions be mentioned?
Another inconsistency which I noticed was the positional location of the exceptions. For the convenience of the users and improved readability, it seems to make the most sense to have them consistently in the same position, on their own line, and separate from the rest of the function summary (instead of buried within another section). It seems a bit unreasonable to require users to go on a “scavenger hunt” to find the common exceptions for a given function.
When adding the exceptions to os.chdir()
, I used a format which seemed to be used for a few other functions within os
. As an example, here is os.chdir()
:
Change the current working directory to path.
This function can support :ref:
specifying a file descriptor <path_fd>
. The
descriptor must refer to an opened directory, not an open file.
This function can raise :exc:
OSError
and subclasses such as
:exc:FileNotFoundError
, :exc:PermissionError
, and :exc:NotADirectoryError
.
… versionadded:: 3.3
Added support for specifying path as a file descriptor
on some platforms.
… versionchanged:: 3.6
Accepts a :term:path-like object
.
In this format, the common exceptions which can be raised are mentioned in a separate section just before the version info.
As an unrelated note to the discussion itself, I would be more than happy to begin working on applying this formatting across the standard library docs if it is approved. Rather than immediately opening individual issues and sending a large volume of PRs, I figured it would be worthwhile to begin by opening a discussion on the topic and receiving feedback from the Python community.
Note: This post has a large number of edits because I accidentally submitted the post early by pressing tab
+ enter
. I meant to do some tab indenting, but instead had selected the submit button.