-
-
Notifications
You must be signed in to change notification settings - Fork 30.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Why was Traversable implemented? #96870
Comments
Moreover, importlib resources aims to be extensible, so a custom loader that loads its modules (and resources) from some other source (database, brokerage service, space imagery) is able to supply Traversable resources.
Maybe
That may address the typing issue. Can you possibly put together an example that illustrates the typing issue (using mypy or similar)?
Declaring |
Thank you for the detailed answer. I understand now.
Would it be more correct for I use PyCharm's builtin type analyzer (might be mypy behind the scenes but I have no idea...) which is static. I think dynamic registration would still leave the issue unsolved.
Yes indeed. As I wrote, I'm not too familiar with mypy so I can only attest to strange behavior in PyCharm. Apologies for the specific use-case but it's a simple one. I'm collecting paths to resource files and casting them to from importlib import resources
from aiopath.path import AsyncPath
for resource in resources.files("resources_dir").iterdir():
AsyncPath(resource) # Expected type 'str | PathLike[str]', got 'Traversable' instead Of course since My temporary solution was to explicitly mark from pathlib import Path
from importlib import resources
from aiopath.path import AsyncPath
resource: Path
for resource in resources.files("resources_dir").iterdir():
AsyncPath(resource) # No type warning Initially I suspected a bug in the |
It wouldn't - There are lots of ways to just shut the type checker up, but none of them solve the underlying (valid) issue that it's reporting. If you're quite sure that the package you're enumerating will never be hosted in a zip or be used with any other loader mechanism that might not just give you native filesystem paths (or if you just want to fail in a case where it does), I'd personally just guard the |
In some sense,
I hope it's clear now that the issue isn't that If what your application needs is the guarantee of a file on the file system, importlib resources does provide the as_file context to ensure that a
Perhaps more importantly, the call to I hope that adds some clarity. I'm closing this issue as answered, but feel free to continue the conversation and we can re-open if there's more to be discussed or investigated. |
I'm a bit confused as to why
Traversable
was ever implemented in the first place.Seems like
Traversable
is supposed to represent file system paths, however I was sure that's why we havePath
s.Whatever
Traversable
's added value is, I still think the class should be coupled toPath
type-wise. Maybe by markingTraversable
asPathLike
.At the moment, functions like
importlib.resources.files
specify a return type ofIterator[Traversable]
but in reality returnIterator[Path]
. This results in confusing type hint warnings since IDEs have no way to know thatTraversable
andPath
are related.I'm not very experienced with the newer versions of
importlib
so my assumptions might be a bit off, hoping one of could shed some light.Thanks
Originally posted by @moomoohk in #88366 (comment)
The text was updated successfully, but these errors were encountered: