Skip to content

Commit

Permalink
Raise a nicer error for dedent on closures
Browse files Browse the repository at this point in the history
  • Loading branch information
slundberg committed Nov 21, 2023
1 parent 0b8467e commit 72e9658
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion guidance/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,14 @@ def strip_multiline_string_indents(f):
source = textwrap.dedent(inspect.getsource(f))
blanks = '\n' * f.__code__.co_firstlineno # padd the source so the lines in the file line up for the debugger
source = blanks + '\n'.join(source.splitlines()[1:]) # remove the decorator first line.
# print(source)

# define the external closure variables so f.__closure__ will match our recompiled version
if len(f.__code__.co_freevars) > 0:
raise Exception("You currently must use @guidance(dedent=False) for closure functions (function nested within other functions that reference the outer functions variables)!")
lines = source.split("\n")
lines[0] = "def __outer__closure_wrap():"
lines[1] = " " + ",".join(f.__code__.co_freevars) + " = " + ",".join("None" for _ in f.__code__.co_freevars)
source = " \n".join(lines) # TODO: this does not quite work because new_code_obj is now the __outer__closure_wrap() function...could be fixed with work...

old_code_obj = f.__code__
old_ast = ast.parse(source)
Expand Down

0 comments on commit 72e9658

Please sign in to comment.