-
-
Notifications
You must be signed in to change notification settings - Fork 31k
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
Support Traversable
objects in the native loaders and finders
#102097
Comments
cc @python/importlib-team |
cc @pfmoore too because I am pretty sure this would be handy for https://github.com/pfmoore/editables |
It might have saved me having to write a custom loader, but as that's done now, it's not that big a deal for me. I wouldn't change Also, I'm not entirely sure how this would work for the Would the work @barneygale is doing to make Actually, I just checked the docs and |
Why not use
If |
Most of the stuff here is not really a problem for most people, but the limited scope makes it unusable for some more advanced projects, like https://github.com/mesonbuild/meson-python. The issue is that for the people where
Writing a
Yes, definitely. From my initial testing, nothing obvious breaks. User code is a worry, though, so we might want a different attribute. The
Which hints to different kind of objects possibly being present on the list, so we may be able to get away with it, but definitely something to consider and discuss further.
I am pretty sure that currently importing |
I'm -1 on this as I don't see a big enough benefit for the code complexity (or I'm -0 if I drop being an import expert and let other folks worry about compatibility and eventual bug reports 😉). |
Maybe my understanding of the complexity required is wrong, but it doesn't seem that complicated to me. From my understanding, in Do you have anything specific in mind regarding the complexity? I can commit to dealing with the fallout if there's any issue, for whatever that's worth. We probably want other people reviewing changes, so I know it doesn't help that much. |
This idea is interesting. I'm +0 on the idea, maybe +1 since there's a known unmet use-case without it. Since FFY00 is willing to support it, I'd be willing to help review an implementation. |
That in my experience, anything involving import semantics is complex. 😉 I'm also recovering from COVID-19, so in my slightly more tired state this isn't getting me excited enough to get past -0. |
I was finally able to realize why I'm uneasy about this proposal: it goes against the language definition. If you read https://docs.python.org/3/reference/import.html#path-entry-finders you will notice it says:
Shanging things so On top of that, there's backwards-compatibility concerns. While the language definition may say "other types are [to be] ignored", we all know at least some, if not most, people have been too lazy to check the types of things on As such, you will need to come up with a plan on how to handle all of this which starts to lurch into PEP territory. |
Yes, I understand. My plan was to try to check how much breakage adding these objects into |
How were you planning to do that? One of the great challenges of managing this sort of backwards-compatibility is all the "dark code" there is out there which we don't have access to. And my guess is there's a much bigger chance a company made a custom importer than an open source project. |
Traversable
objects in sys.path
Traversable
objects in the native loaders and finders
You're right. The important part of the proposal (the bit that actually solves an issue) does not need this breakage anyway, maybe I am getting ahead of myself a bit. Simply having our loaders being able to take |
Feature or enhancement
Summarizing, it would consist of users being able to add
Traversable
objects tosys.path
and having the default loaders pick them up.Pitch
This proposal aims to simplify the implementation of import redirection and similar use-cases by introducing support for abstracted paths in our default loaders.
Loader
s are a bit too lowCurrently, one of the most common use-cases of custom loaders is to implement some kind of import redirection, not to add support for new module types. By import redirection, I mean that the loader maps import names to normal modules, but in different locations.
A good example of this would be loaders for packages installed in editable mode.
The loader abstraction is a bit lower level than desired for this use-case, so implementing a loader for these applications is pretty hard. One needs to either re-implement all the resolution and import logic, or subclass the default loaders and customize them.
How
Traversable
s can helpTraversable
is a protocol introduced in 3.9 to provide a path abstraction to use inimportlib.resources
. If we change the default loaders to use this abstraction, we remove the need for customs loaders to implement support for data sources.Placing an abstraction at this level should allow us to better support environments that lack a native file-system, support custom ... more consistently, and even possibly remove some complexity from the import system, like
zipimport
, in favor of a lightweightZipPath
alternative + the default loaders.Implementation
This would likely consist of adding a new
_Path
type that implements theTraversable
protocol usingos.path
, to use with string paths, and then change the loaders to use the abstraction, instead of usingos.path
directly.Some loaders use features that are not provided by
Traversable
, likestat
, but we can ensure backwards compatibility by setting__fspath__
in our_Path
implementation, and still performing those operations if a file system path is available.I am not super worried about the performance impacts of this, as I think this abstraction should be fairly light.
Previous discussion
This is somewhat related to #89710, and is a step in the right direction for that.
The text was updated successfully, but these errors were encountered: