Skip to content

Comments

bpo-44196: document that Traversable's methods should raise FileNotFoundError#26272

Closed
FFY00 wants to merge 1 commit intopython:mainfrom
FFY00:bpo-44196
Closed

bpo-44196: document that Traversable's methods should raise FileNotFoundError#26272
FFY00 wants to merge 1 commit intopython:mainfrom
FFY00:bpo-44196

Conversation

@FFY00
Copy link
Member

@FFY00 FFY00 commented May 20, 2021

@FFY00 FFY00 changed the title bpo-44195: add importlib.abc.TraversableResources bpo-44196: document that Traversable's methods should raise FileNotFoundError May 20, 2021
.. versionadded:: 3.9
.. versionadded:: 3.9

.. abstractmethod:: name()
Copy link
Member

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.

When opening as text, accepts encoding parameters such as those
accepted by io.TextIOWrapper.

If the file cannot be found, FileNotFoundError should be raised,
Copy link
Member

Choose a reason for hiding this comment

The 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.

Comment on lines +370 to +382

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.
Copy link
Member

Choose a reason for hiding this comment

The 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.

@jaraco
Copy link
Member

jaraco commented May 23, 2021

After looking through each of these methods, I'm uncertain that any of them should indicate anything about a FileNotFoundError. Maybe I misunderstood the question when we discussed earlier. Let's go back to the core issue and determine if any documentation is needed.

@jaraco jaraco closed this May 23, 2021
@FFY00
Copy link
Member Author

FFY00 commented May 23, 2021

Of course 🤦, I derived this from ResourceReader without having a proper think on where it makes sense, my goal was to mimic the behavior.
Having a proper look at it, I think we should keep the error on open. The issue is that we would like to have an excepted way for how open could fail, because open failing is expected if the traversable path does not exist or open does not make sense in the context of the implementation. We want an escape hatch for those cases -- if the design expects such use-cases, it should also provide a mechanism for us to deal with them.

This could take a different form than a FileNotFoundError, but I think that's probably what makes the most sense here considering the current implementations and the design of ResourceReader, which this superseeds.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants