[Python-Dev] file system path protocol PEP
Koos Zevenhoven
k7hoven at gmail.com
Sun May 15 07:57:39 EDT 2016
On Fri, May 13, 2016 at 7:43 PM, Chris Angelico <rosuav at gmail.com> wrote:
[...]
> "Check" accepts subclasses; "CheckExact" doesn't (it's like "type(x)
> is str"). The question is, which one SHOULD be being done?
Indeed it should do "Check", so that path libraries that do inherit
from str will still work (see also below).
> What should this do:
>
> class TmpPath(str):
> def __fspath__(self):
> return "/tmp/"+self
> x = TmpPath("foo/bar")
> open(x, "w")
>
> Does that respect __fspath__, or respect the fact that it's a string?
It should not respect __fspath__ because that would mean the behavior
is different in older versions of Python(!). This also means that such
code should never be written (except for that example you wrote ;-).
So indeed, composition would be the way to go.
So yes, I still think str subclasses should not implement __fspath__,
and that, at the very least, the docs should say that :).
-- Koos
> ChrisA
> _______________________________________________
> Python-Dev mailing list
> Python-Dev at python.org
> https://mail.python.org/mailman/listinfo/python-dev
> Unsubscribe: https://mail.python.org/mailman/options/python-dev/k7hoven%40gmail.com
More information about the Python-Dev
mailing list