<div dir="ltr"><br><br><div class="gmail_quote"><div dir="ltr">On Thu, 7 Apr 2016 at 12:03 Chris Barker <<a href="mailto:chris.barker@noaa.gov">chris.barker@noaa.gov</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Apr 7, 2016 at 11:59 AM, Brett Cannon <span dir="ltr"><<a href="mailto:brett@python.org" target="_blank">brett@python.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_quote"><div>  class StringLike(abc.ABC):</div><div><br></div><div>    @abstractmethod</div><div>    def __str__(self):</div><div>        """Return the string representation of something."""</div><div><br></div><div>  StringLike.register(pathlib.PurePath)  # Any 3rd-party library can do the same.</div><div><br></div><div>You could also call the class StringablePath or something and get the exact same concept across where you are using the registration abilities of ABCs to semantically delineate when a class's __str__() returns a usable file path.</div><div><br></div><div>The drawback is that this isn't easily backported like `path.__ospath__() if hasattr(path, '__ospath__') else path` for libraries that don't necessarily have access to pathlib but want to be compatible with accepting path objects.</div></div></div>
</blockquote></div><div class="gmail_extra"><br></div></div></div><div dir="ltr"><div class="gmail_extra">and a plus is that it's compatible with type hinting -- is that the future of Python???</div></div></blockquote><div><br></div><div>So the other way to do this is to combine the proposals:</div><div><br></div><div>  class BasePath(abc.ABC):</div><div><br></div><div>      @abstractmethod</div><div>      def __ospath__(self):</div><div>          """Return the file system path, serialized as a string."""</div><div><br></div><div>Then pathlib.PurePath can inherit from this ABC and anyone else can as well or be registered as doing so. Then in typing.py you can have:</div><div><br></div><div>  class Path(extra=pathlib.BasePath):</div><div>     __slots__ = ()</div><div><br></div><div>  PathLike = Union[str, Path]</div><div><br></div><div>Then any third-party library can register with the ABC and get the typing correctly (assuming I didn't botch the type specification).</div><div><br></div><div>Guido also had some protocol proposal a while back that I think he floated here, but I don't think the discussion really went anywhere as it was an early idea.</div></div></div>