You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Typing – Traversable vs MultiplexedPath return value of files() #286
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?
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')
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.
Activity
jaraco commentedon Aug 7, 2023
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:And ZipReader returns a ZipPath:
importlib_resources/importlib_resources/readers.py
Line 53 in a203c1b
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
FileReaderby dint of being aTraversableResourceswill return aTraversableforfiles(), which may not be aMultiplexedPath.