Re: [Python-Dev] Pathlib enhancements - acceptable inputs and outputs for __fspath__ and os.fspath()

2016年4月18日 07:05:40 -0700

On 04/18/2016 12:41 AM, Nick Coghlan wrote:
Given the variant you [Koos] suggested, what if we defined the API semantics
like this:
 # Offer the simplest possible API as the public vesion
 def fspath(pathlike) -> str:
 return os._raw_fspath(pathlike)
 # Expose the complexity in the "private" variant
 def _raw_fspath(pathlike, *, output_types = (str,)) -> (str, bytes):
 # Short-circuit for instances of the output type
 if isinstance(pathlike, output_types):
 return pathlike
 # We'd have a tidier error message here for non-path objects
 result = pathlike.__fspath__()
 if not isinstance(result, output_types):
 raise TypeError("argument is not and does not provide an
acceptable pathname")
 return result
My initial reaction was that this was overly complex, but after thinking about it a couple days I /really/ like it. It has a reasonable default for the 99% real-world use-case, while still allowing for custom and exact tailoring (for the 99% stdlib use-case ;) .
--
~Ethan~
_______________________________________________
Python-Dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/python-dev
Unsubscribe: 
https://mail.python.org/mailman/options/python-dev/archive%40mail-archive.com

Reply via email to