Working to Make Python Lazy(iscinumpy.dev) |
Working to Make Python Lazy(iscinumpy.dev) |
Wait, that seems bad. These kinds of modules (especially numba) are often exactly the ones that you want to delay importing. Why not provide a way to check the existence at import time? How are you supposed to handle nonexistence in that case?
import importlib.util
if importlib.util.find_spec("numpy") is None:
... # whatever you wanted to do if numpy is missing
lazy import numpy> Currently, disabling lazy imports disabled the syntax keyword, which means that you can’t use it for circular imports, type checking, etc.
Imo this is a good thing, laziness shouldn't be semantically important. The ability to force disable laziness while maintaining semantics is important for linting and testing, and more often than not, circular imports are a sign of bad code structure.
> As soft keywords, their use in the grammar is possible while still preserving compatibility with existing code that uses these names as identifier names.
Also, you need something that works for simple one-file scripts, not just a whole package or project.
I might recommend doing something like importing from something like `numpy.version` (or some other "random" very small utils package) so that the work done on file load is still fairly small.
"Oh you don't have optional dependency? Then do X" needs to happen at runtime right?
What would that be for numba?
For example, numba._version only imports standard library stuff so is probably good enough here[0]
It would be nice to have general querying capabilities here, of course. I just think that in practice there's at least a halfway-decent workaround in almost any real situation
[0] https://github.com/numba/numba/blob/main/numba/_version.py