Live data from Hacker News

Working to Make Python Lazy

iscinumpy.dev

1–10 of 21 posts

Re: Working to Make Python Lazy

#2
> This pattern, for example, can’t be lazy: try: import numpy; except ModuleNotFoundError: ...

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?

Re: Working to Make Python Lazy

#4
This feature seems really valuable for commandline tools! However this paragraph gave me pause:

> 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.

Re: Working to Make Python Lazy

#5
post #2

> This pattern, for example, can’t be lazy: try: import numpy; except ModuleNotFoundError: ... 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?

...crash?

Seriously: crash if your dependencies aren't available. There are better ways to do optional dependencies. ImportError ain't it.

Re: Working to Make Python Lazy

#6
post #5
post #2

> This pattern, for example, can’t be lazy: try: import numpy; except ModuleNotFoundError: ... 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?

...crash? Seriously: crash if your dependencies aren't available. There are better ways to do optional dependencies. ImportError ain't it.

What is the right way to handle an optional dependency?

Re: Working to Make Python Lazy

#7
post #2

> This pattern, for example, can’t be lazy: try: import numpy; except ModuleNotFoundError: ... 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?

> The error here will move to the first usage of something from numpy. There is a semi-lazy alternative:

  import importlib.util

  if importlib.util.find_spec("numpy") is None:
  ... # whatever you wanted to do if numpy is missing

  lazy import numpy

Re: Working to Make Python Lazy

#8
post #6
post #5

Earlier quoted context omitted.

...crash? Seriously: crash if your dependencies aren't available. There are better ways to do optional dependencies. ImportError ain't it.

What is the right way to handle an optional dependency?

I think this import flow tends to be the canonical one.

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.

Post reply on HN