ManderaGeneral / generalimport

Handle all your optional dependencies with a single call!
https://pypi.org/project/generalimport/
Apache License 2.0
15 stars 1 forks source link

Better import handling technique #12

Closed Mandera closed 1 year ago

Mandera commented 1 year ago

Current loader idea is to seperate module names based on if they are namespaces or not. This happens immediately, not very efficient because each check requires an import.

It then puts on Importer in the start to handle all namespace imports. And one at the end to get the rest of the names if the normal importers failed to resolve.

I think a better idea would be to only have one loader in the start which all imports go through. When an import is made this loader does a new identical import but this next time this loader will ignore it and gets the result of the import if possible. When it has the "normal" import (still inside the loader method) it can check it: If it's None or a namespace package then we resolve it to our fake module.

This would be much better and faster if it works, also only requiring one Importer.

Mandera commented 1 year ago

Seems to work! https://gist.github.com/Mandera/aa007a685e69df71391dacccdcc1deb6