
On Wed, Dec 1, 2021 at 10:40 PM Christopher Barker <pythonchb@gmail.com> wrote:
I know this isn't really the place for this conversation, but:
which is what `os.PathLike` represents, hence why `str` isn't covered by it);
wait, what? It seems so clear to me that "PathLike" (as a type specifier) would mean: anything that can be passed into os.fspath to give me a path. (or, of course to the stdlib functions that take paths)
That is not what the docs say: https://docs.python.org/3/library/os.html#os.PathLike. And as the creator of that ABC it's very much on purpose (see https://www.python.org/dev/peps/pep-0519/ for details).
Isn't the entire purpose of os.fspath that you can write code like:
def fun(some_kind_of_path): some_kind_of_path = os.fspath(some_kind_of_path)
(or just pass it to a function you takes PathLIke)
and go on your merry way -- e.g. duck typing, baby!
Depends on what "your merry way" is. As the docs say, os.fspath() is about getting the file system representation. That's not something to directly manipulate in a pathlib world unless you're using os.path to manipulate that string encoding (but which PEP 519 was specifically created to avoid such encoding headaches): https://docs.python.org/3/library/os.html#os.fspath .
Is there really no way to annotate that simply now?
Once again, depends on what "simply" means to you. The examples I gave I don't find complicated, especially when we are talking about APIs that are trying to accept a broad set of types to convert into a single type. If you want to create a type variable that represents anything that is valid to `os.fspath()` or `pathlib.Path` to avoid typing out 2 or 4 types, then that can be considered for inclusion in the stdlib somewhere. But as I said previously, in my code I just take pathlib.Path or pathlib.PurePath and I'm done as I don't want to be dealing with encoded file paths and instead with objects that represent file paths.
-CHB
-- Christopher Barker, PhD (Chris)
Python Language Consulting - Teaching - Scientific Software Development - Desktop GUI and Web Development - wxPython, numpy, scipy, Cython