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

Ethan Furman ethan at stoneleaf.us
Mon Apr 18 10:03:28 EDT 2016


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~



More information about the Python-Dev mailing list