Closed frenzymadness closed 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:
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
.
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:
or
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 returnsMultiplexedPath
so both workarounds are fine but mypy complains about it becauseTraversable
neither has_paths
notparent
attributes.When I tried to change the return value in the annotation from
Traversable
toMultiplexedPath
mypy did not complain about it so it seems that the function cannot return anything else thanMultiplexedPath
. Is that correct? Would it make sense to make the type annotation more specific?