amogorkon / justuse

Just use() code from anywhere - a functional import alternative with advanced features like inline version checks, autoreload, module globals injection before import and more.
MIT License
49 stars 8 forks source link

Make relative imports and use(relative_path) work inside a module pulled from the web - use(git(..)) #5

Open amogorkon opened 3 years ago

amogorkon commented 3 years ago

use() on a github URL works for vanilla modules (no or only builtin imports), however it would and should not work "just like that" if classical relative imports or use() with a Path is applied to import modules from the same project/package. Attempting to resolve those relative paths could result in internal version conflicts or worse, in hijacked code. Instead, there should be an exception if attempting to do so, a guess at what the user might mean and supplying the user with that info (guessed_url, hash_algo, hash_valu) for them to insert into their calling code. This insertion should be done via an alias mapping that includes the source name, the path that should be mapped to an URL with the corresponding hashing details. Now, when use() is used from within the specified source, it should try to look up the path and map it to a call to URL.

greyblue9 commented 3 years ago

Would we need to add something to sys.meta_path to enable us to dynamically download and resolve these imports, probably at the very end of the list so that any possible local module would be preferred to the online version?

The added resolver would probably have the module import 'root' as a Path as an attribute of the resolver?

greyblue9 commented 3 years ago

Clarifying my above comment with some API details:

Our special resolver is actually a finder with one method:

def find_module(abs_name: str, parent_pkg_path: str = None) -> loader:

See PEP302, meta_path

amogorkon commented 3 years ago

Hmm.. initially, I wasn't thinking of relative import from online resources, but I won't complain if it's possible to adapt the mechanism from use(Path("foo.py")) to import foo, just keep in mind that use(Path(..)) is more general than import, so we should make the mechanism work for that first and go from there.

Which means that I think that the aliases should live in Use(), not sys.meta_path primarily

amogorkon commented 3 years ago

With import_to_use #31 proposed, I think this one should be named "path_to_url" to make the distinction perfectly clear.

amogorkon commented 2 years ago

@thirteenpylons have fun and feel free to experiment ;) we could even introduce a new case for github urls like use(use.GIT("amogorkon/justuse")) or somesuch if it helps.