Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

core: Include global variables in variables found by get_function_nonlocals #25936

Merged
merged 3 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
core: Include global variables in variables found by get_function_non…
…locals
  • Loading branch information
nfcampos committed Sep 2, 2024
commit 6312bf29c69937575d278da50cd06b4efbce22b6
2 changes: 1 addition & 1 deletion libs/core/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ tests:
poetry run pytest $(TEST_FILE)

test_watch:
poetry run ptw --snapshot-update --now . -- -vv tests/unit_tests
poetry run ptw --snapshot-update --now . -- -vv $(TEST_FILE)

test_profile:
poetry run pytest -vv tests/unit_tests/ --profile-svg
Expand Down
7 changes: 5 additions & 2 deletions libs/core/langchain_core/runnables/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -393,7 +393,9 @@ def get_function_nonlocals(func: Callable) -> List[Any]:
visitor = FunctionNonLocals()
visitor.visit(tree)
values: List[Any] = []
for k, v in inspect.getclosurevars(func).nonlocals.items():
closure = inspect.getclosurevars(func)
candidates = {**closure.globals, **closure.nonlocals}
for k, v in candidates.items():
if k in visitor.nonlocals:
values.append(v)
for kk in visitor.nonlocals:
Expand Down Expand Up @@ -469,7 +471,8 @@ def __radd__(self, other: AddableDict) -> AddableDict:
class SupportsAdd(Protocol[_T_contra, _T_co]):
"""Protocol for objects that support addition."""

def __add__(self, __x: _T_contra) -> _T_co: ...
def __add__(self, __x: _T_contra) -> _T_co:
...


Addable = TypeVar("Addable", bound=SupportsAdd[Any, Any])
Expand Down
19 changes: 19 additions & 0 deletions libs/core/tests/unit_tests/runnables/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@ def test_indent_lines_after_first(text: str, prefix: str, expected_output: str)
assert indented_text == expected_output


global_agent = RunnableLambda(lambda x: x * 3)


def test_nonlocals() -> None:
agent = RunnableLambda(lambda x: x * 2)

Expand All @@ -53,7 +56,23 @@ def my_func2(input: str) -> str:
def my_func3(input: str) -> str:
return agent.invoke(input)

def my_func4(input: str) -> str:
return global_agent.invoke(input)

def my_func5() -> Callable[[str], str]:
global_agent = RunnableLambda(lambda x: x * 3)

def my_func6(input: str) -> str:
return global_agent.invoke(input)

return my_func6, global_agent

assert get_function_nonlocals(my_func) == []
assert get_function_nonlocals(my_func2) == []
assert get_function_nonlocals(my_func3) == [agent.invoke]
assert get_function_nonlocals(my_func4) == [global_agent.invoke]
func, nl = my_func5()
assert get_function_nonlocals(func) == [nl.invoke]
assert RunnableLambda(my_func3).deps == [agent]
assert RunnableLambda(my_func4).deps == [global_agent]
assert RunnableLambda(func).deps == [nl]
Loading