python / importlib_resources

Backport of the importlib.resources module
Apache License 2.0
64 stars 44 forks source link

Typing – Traversable vs MultiplexedPath return value of files() #286

Closed frenzymadness closed 1 year ago

frenzymadness commented 1 year ago

Related to: https://github.com/python/cpython/issues/106614 and https://github.com/python/importlib_resources/issues/263

I see two possible workarounds for the mentioned issue:

str((files("jupyterlab_server.test_data") / "dummy").parent)

or

str(files("jupyterlab_server.test_data")._paths[0])

but both have issues with typing because the signature of files function is: https://github.com/python/importlib_resources/blob/02f31b78d37da2525662369a4db2ace47188f4af/importlib_resources/_common.py#L52 In my specific case, files always returns MultiplexedPath so both workarounds are fine but mypy complains about it because Traversable neither has _paths not parent attributes.

When I tried to change the return value in the annotation from Traversable to MultiplexedPath mypy did not complain about it so it seems that the function cannot return anything else than MultiplexedPath. Is that correct? Would it make sense to make the type annotation more specific?

jaraco commented 1 year ago

It's possible the current implementation of importlib resources will only return a MultiplexedPath, but the interface is extensible, with alternate resource providers directed to return objects conforming to the more general Traversable protocol.

But when I look at the code, the only reader that returns a MultiplexedPath is a NamespaceReader. That makes me think that other readers probably don't return MultiplexedPath.

Indeed, a standard module will return a pathlib.Path:

 draft @ mkdir foo
 draft @ touch foo/__init__.py
 draft @ pip-run importlib_resources
Python 3.11.4 (main, Jun 20 2023, 17:23:00) [Clang 14.0.3 (clang-1403.0.22.14.1)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import importlib_resources as res
>>> res.files('foo')
PosixPath('/Users/jaraco/draft/foo')

And ZipReader returns a ZipPath:

https://github.com/python/importlib_resources/blob/a203c1b26ee921bd4b17cc8e405148b382ae46aa/importlib_resources/readers.py#L53

mypy did not complain

Probably the reason it did not complain is that the type annotations are incomplete. Still, mypy should have been smart enough to detect that a FileReader by dint of being a TraversableResources will return a Traversable for files(), which may not be a MultiplexedPath.