Repository navigation
bpo-44196: document that Traversable's methods should raise FileNotFoundError #26272
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 all commits
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 |
|---|---|---|
|
|
@@ -367,13 +367,19 @@ def iterdir(self): | |
| def read_bytes(self): | ||
| """ | ||
| Read contents of self as bytes | ||
|
|
||
| If the file cannot be found, FileNotFoundError should be raised, | ||
| although this is not guaranteed for compatibility reasons. | ||
| """ | ||
| with self.open('rb') as strm: | ||
| return strm.read() | ||
|
|
||
| def read_text(self, encoding=None): | ||
| """ | ||
| Read contents of self as text | ||
|
|
||
| If the file cannot be found, FileNotFoundError should be raised, | ||
| although this is not guaranteed for compatibility reasons. | ||
|
Comment on lines
+370
to
+382
Member
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. Since these implementations don't perform any specific checks on fileness, I'd leave out any documentation about what exceptions are raised and defer to the docs for open. |
||
| """ | ||
| with self.open(encoding=encoding) as strm: | ||
| return strm.read() | ||
|
|
@@ -382,23 +388,35 @@ def read_text(self, encoding=None): | |
| def is_dir(self) -> bool: | ||
| """ | ||
| Return True if self is a dir | ||
|
|
||
| If the directory/file cannot be found, FileNotFoundError should be raised, | ||
|
FFY00 marked this conversation as resolved.
|
||
| although this is not guaranteed for compatibility reasons. | ||
| """ | ||
|
|
||
| @abc.abstractmethod | ||
| def is_file(self) -> bool: | ||
| """ | ||
| Return True if self is a file | ||
|
|
||
| If the directory/file cannot be found, FileNotFoundError should be raised, | ||
|
FFY00 marked this conversation as resolved.
|
||
| although this is not guaranteed for compatibility reasons. | ||
| """ | ||
|
|
||
| @abc.abstractmethod | ||
| def joinpath(self, child): | ||
| """ | ||
| Return Traversable child in self | ||
|
|
||
| If the file cannot be found, FileNotFoundError should be raised, | ||
|
FFY00 marked this conversation as resolved.
|
||
| although this is not guaranteed for compatibility reasons. | ||
| """ | ||
|
|
||
| def __truediv__(self, child): | ||
| """ | ||
| Return Traversable child in self | ||
|
|
||
| If the file cannot be found, FileNotFoundError should be raised, | ||
|
FFY00 marked this conversation as resolved.
|
||
| although this is not guaranteed for compatibility reasons. | ||
| """ | ||
| return self.joinpath(child) | ||
|
|
||
|
|
@@ -410,6 +428,9 @@ def open(self, mode='r', *args, **kwargs): | |
|
|
||
| When opening as text, accepts encoding parameters such as those | ||
| accepted by io.TextIOWrapper. | ||
|
|
||
| If the file cannot be found, FileNotFoundError should be raised, | ||
|
Member
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'd probably change the terminology. The term "the file" could be more clear. Maybe something like "the object referenced by self". Also, "open" may raise other exceptions. For example, if self references a directory and someone attempts to open it, they should expect "IsADirectoryError" to be raised. Or if the resource was on a network and the network has dropped, they might get a socket.ConnectionError. Or if it was an HTTP resource and access has been revoked, they may get an HTTPAccessDenied error. I'm not sure it's appropriate to attempt to prescribe which exceptions should or shouldn't be expected. |
||
| although this is not guaranteed for compatibility reasons. | ||
| """ | ||
|
|
||
| @abc.abstractproperty | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| Document that importlib.abc.Traversable methods should raise | ||
| FileNotFoundError. |
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.
I'm inclined to say let's not duplicate the docs from the code. Let's just link to the code if needed.