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
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?
The text was updated successfully, but these errors were encountered:
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.
Related to: python/cpython#106614 and #263
I see two possible workarounds for the mentioned issue:
or
but both have issues with typing because the signature of
files
function is:importlib_resources/importlib_resources/_common.py
Line 52 in 02f31b7
In my specific case, files always returns
MultiplexedPath
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?The text was updated successfully, but these errors were encountered: