lf1-io / padl

Functional deep learning
Apache License 2.0
106 stars 4 forks source link

Make PADL work in Base Python Interpreter Environment #434

Open sjrl opened 2 years ago

sjrl commented 2 years ago

🛰️ Feature

Description

Make PADL to work in environments that do not save executed source code (e.g. in the base Python Interpreter Environment). Currently PADL will throw a TypeError if the source code of the transform is not findable by the inspect module.

from padl import transform

@transform
def f(x):
    return x + 1

gives

Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/padl/padl/wrap.py", line 278, in transform
    return _wrap_function(wrappee, ignore_scope)
  File "/padl/padl/wrap.py", line 40, in _wrap_function
    if '@' in caller.code_context[0] or caller.code_context[0].startswith('def'):
TypeError: 'NoneType' object is not subscriptable

Solution

It would be nice if transforms could still be made and executed, but warn the user that saving will not work in this environment. We could do this by avoiding inspection when the source code is not available (e.g. returns as None) and print a warning to the user that saving will not work. Most likely a rare use-case, but it could be worth supporting.

Alternatives

If we do not want to support this we could check if the source code is returned as None and suggest using a different interpreter like IPython.

sjrl commented 2 years ago

@wuhu What do you think?

wuhu commented 2 years ago

I think the warning is a good solution.

sjrl commented 2 years ago

We are considering solution where this code is added to padl/__init__.py:

try:
    import inspect
    from padl.dumptools.sourceget import get_source
    _ = get_source(inspect.stack()[-1].filename)
    # We do not want these to be padl level imports so we remove them
    del inspect
    del get_source
except Exception as e:
    raise RuntimeError('PADL does not work in the current Interpreter Environment because we '
                       'rely on the inspect module to find source code. '
                       'Unfortunately, the source code typed at this interactive '
                       'prompt is discarded as soon as it is parsed. Therefore, we recommend '
                       'using the IPython interpreter or Jupyter Notebooks for interactive '
                       'sessions.') from e

However, the inspect.stack()[-1].filename does not guarantee grabbing the frame that corresponds to the user input from the interactive environment. In fact depending on the environment (e.g. Python Interpreter, IPython, PyCharm, etc.) additional frames can be mixed in.