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
Specify possible exceptions for os.chdir() #81659
Comments
In the section describing the functionality of os.chdir(), there is no mention of possible errors that can be raised. This differentiates significantly from the sections of other methods, such as os.is_dir(): "This method can raise :exc:`OSError`, such as :exc:`PermissionError`, but :exc:`FileNotFoundError` is caught and not raised." For the purpose of consistency and providing additional useful information, I would suggest doing the same for os.chdir(). os.chdir() differs from os.is_dir() by directly raising the OSErrors FileNotFoundError and NotADirectoryError rather than handling them. I would suggest the addition of the following in os.chdir()'s section: "This method can raise :exc:`OSError`, such as exc:`FileNotFoundError` if the *path* does not lead to a valid file or exc:`NotADirectoryError` if the file specified is not a valid directory." or a less verbose alternative: "This method can raise :exc:`OSError`, such as exc:`FileNotFoundError` and exc:`NotADirectoryError`." If a user is reading the documentation, they might naturally assume that os.chdir() catches the FileNotFoundError, similar to the behavior in os.is_dir(). Also, the NotADirectoryError is not explicitly mentioned anywhere else on the "os.rst" page. If it were to be mentioned anywhere, os.chdir() would probably be the most relevant location to do so. Also, as a general question, what exactly is the standard for mentioning errors in the method sections for the Python documentation? For the purposes of clarity, I think it would be quite useful to explicitly mention errors, and a brief description of situations which raise them. If the behavior of the errors is consistent across of a group of methods, it can instead be mentioned in the header section. However, if the behavior not consistent, such as in this case, I think it is useful to briefly mention how the method deviates. It also seems useful to mention the error at least once if it is not explicitly mentioned elsewhere on the page, even if the parent is mentioned, such as with NotADirectoryError and OSError. This seems to be particularly important to do for the docs in a language such as Python, as it does not require the raise-able/throw-able errors to be mentioned in the code. I have already created a branch in my local fork of the cpython repository which implements this change, but I'll await feedback before submitting the PR. |
Minor clarifications: This change is referring to the "Files and Directories" section of the "os.rst" page in "cpython/Doc/library/os.rst". Also, I realized that I forgot to add the colon before "exc" in a couple locations. This was just a typo when writing the comment, it is correctly formatted in my local branch. |
https://docs.python.org/3/library/os.html#os-file-dir has os.chdir(path)
Change the current working directory to path.
This function can support specifying a file descriptor. The descriptor must refer to an opened directory, not an open file. Most of the other entries in do not mention errors, but is_dir has the line mentioned. I think the condensed line is enough to add, as the sub-exception names are self-explanatory; and when they occur, the full message certainly is. (I verified both possibilities mentioned.) Most users should just catch OSError, if anything. Make a PR with the short message and request me as a reviewer. |
Thanks for the feedback terry. Do you think it would be helpful to work through some of the other commonly used functions in OS, adding condensed explanations of exceptions which can be raised? In general, it seems quite useful for users to be able to figure out the exceptions in which they may need to account for when using each of the functions. Also, should the naming convention be "method" or "function"? The two roughly refer to the same thing, but as far as I'm aware, the convention in Python is to refer to them as "functions". I'm used to calling them "methods" from my prior experience with OOP, so that's why I did so in my initial condensed description. On the docs for OS, there seems to be a few instances where the term "method" is used. Personally, I don't mind the usage of either term, at the end of the day it's just semantics. But for the purposes of documentation, consistency seems preferable. Should I create a PR for replacing the mentions of "method" with "function"? Since it's quite minor and in the same OS.rst file, should I link it to this issue instead of creating a new one? |
Actually, I don't believe that I have the appropriate permissions to manually specify reviewers for PRs in the cpython repository. Do I have to be added a member of the Python organization on GitHub for this? I'm registered as a PSF contributing member, but I don't think that alone translates to any repository permissions. Could I be added as a member with low level permissions such as editing issue labels without being granted commit? This would also be helpful for adding "skip news" labels on any other issues, I've been doing a decent amount of PR reviews recently. |
Please ask your questions about doc conventions, both about exceptions and function/method. Either someone will point to something in the devguide, or we might get a discussion about something that should be added. I would not merge more extensive changes without some feedback. |
Exceptions for os.chdir() have been added to the docs. |
Spinoff issues should be separate. |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: