[Python-Dev] Pathlib enhancements - acceptable inputs and outputs for __fspath__ and os.fspath()
Nick Coghlan
ncoghlan at gmail.com
Sun Apr 17 08:36:15 EDT 2016
On 17 April 2016 at 18:03, Stephen J. Turnbull <stephen at xemacs.org> wrote:
> Nick Coghlan writes:
> > and instead throw exceptions in those cases.
>
> Then I don't understand the current design of fsdecode and fsencode.
> Shouldn't they raise on str and bytes respectively, rather than
> passing them through? In general, I would expect that something
> that's explicitly intended to be polymorphic would be documented as
> such, and the *caller* would be responsible for type-checking and
> raising if it got the wrong thing.
>
I was initially surprised myself, but then realised it made sense for their
intended use cases - if almost every usage looks like "obj if
isinstance(obj, str) else os.fsdecode(obj)", then there ends up being a
strong pragmatic case for pushing the pass-through down into the underlying
function to reduce code duplication and rejecting str input in the cases
where it isn't supported. By contrast, there are lots of places where
"obj.decode()" gets called without a pass-through for objects that are
already decoded.
Cheers,
Nick.
--
Nick Coghlan | ncoghlan at gmail.com | Brisbane, Australia
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20160417/6953c682/attachment.html>
More information about the Python-Dev
mailing list