Support os.PathLike
In Python 3.6, a Protocol was added (os.PathLike), which specifies that any object that provides __fspath__() represents a path; this includes pathlib, importlib.resources.files (Python 3.8+), and various third party libraries. A nice enhancement could be to support base_uri with something like this:
... base_uri: os.PathLike | str, ...
if isinstance(base_uri, os.PathLike):
base_uri = f"file://{os.fspath(base_uri)}"
(Not totally sure how this might integrate; I was just helping someone with the library and realized this wasn't set up to take fspath's, and didn't see it mentioned in other issues).
I'm revisiting this due to having a look at a bunch of tickets surrounding $ref, e.g. https://github.com/python-jsonschema/jsonschema/issues/274#issuecomment-1195387858. I'm looking for ways to make this more obvious to people / harder to make a mistake, and this sort of thing seems like it'd help, but it's a bit more complicated than the proposed addition.
Specifically the behavior lots of people seem to want is "treat directories as if they have trailing slashes" (even though this isn't what RFC 3986 says to do) -- that makes something like what's in the proposed snippet more complicated, because it involves hitting the filesystem to tell whether the path is a directory or not, and worse, may give wrong answers if you try to validate a schema which uses file:// URIs on a different filesystem than the original one, where the same paths may not be the same kinds of entries.
The best idea I have at the minute is to introduce a RefResolver.for_directory() classmethod which would take an os.PathLike which the user is thereby explicitly claiming to be a directory, and then to always / automatically add the trailing slash there.
Comments are welcome from you or anyone else, but putting this here since I think it's slightly relevant for pure os.PathLike support.
An essential full replacement of $ref related support was just merged (and is now in alpha as v4.18.0a1).
The rewrite is a lot more flexible when it comes to resolving references, albeit the defaults are more conservative. But this kind of thing should very well be possible now via e.g. this
Going to close this since I think things have changed significantly in a way that hopefully makes it easy for someone to do this, with pathlib or whatever suits their fancy, but comments of course welcome and any feedback on the prerelease is very very welcome!