mirror of
https://github.com/hwchase17/langchain.git
synced 2025-06-24 15:43:54 +00:00
core: Include global variables in variables found by get_function_nonlocals (#25936)
Thank you for contributing to LangChain! - [ ] **PR title**: "package: description" - Where "package" is whichever of langchain, community, core, experimental, etc. is being modified. Use "docs: ..." for purely docs changes, "templates: ..." for template changes, "infra: ..." for CI changes. - Example: "community: add foobar LLM" - [ ] **PR message**: ***Delete this entire checklist*** and replace with - **Description:** a description of the change - **Issue:** the issue # it fixes, if applicable - **Dependencies:** any dependencies required for this change - **Twitter handle:** if your PR gets announced, and you'd like a mention, we'll gladly shout you out! - [ ] **Add tests and docs**: If you're adding a new integration, please include 1. a test for the integration, preferably unit tests that do not rely on network access, 2. an example notebook showing its use. It lives in `docs/docs/integrations` directory. - [ ] **Lint and test**: Run `make format`, `make lint` and `make test` from the root of the package(s) you've modified. See contribution guidelines for more: https://python.langchain.com/docs/contributing/ Additional guidelines: - Make sure optional dependencies are imported within a function. - Please do not add dependencies to pyproject.toml files (even optional ones) unless they are required for unit tests. - Most PRs should not touch more than one package. - Changes should be backwards compatible. - If you are adding something to community, do not re-import it in langchain. If no one reviews your PR within a few days, please @-mention one of baskaryan, efriis, eyurtsev, ccurme, vbarda, hwchase17.
This commit is contained in:
parent
36bbdc776e
commit
464dae8ac2
@ -13,7 +13,7 @@ tests:
|
|||||||
poetry run pytest $(TEST_FILE)
|
poetry run pytest $(TEST_FILE)
|
||||||
|
|
||||||
test_watch:
|
test_watch:
|
||||||
poetry run ptw --snapshot-update --now . -- -vv tests/unit_tests
|
poetry run ptw --snapshot-update --now . -- -vv $(TEST_FILE)
|
||||||
|
|
||||||
test_profile:
|
test_profile:
|
||||||
poetry run pytest -vv tests/unit_tests/ --profile-svg
|
poetry run pytest -vv tests/unit_tests/ --profile-svg
|
||||||
|
@ -393,7 +393,9 @@ def get_function_nonlocals(func: Callable) -> List[Any]:
|
|||||||
visitor = FunctionNonLocals()
|
visitor = FunctionNonLocals()
|
||||||
visitor.visit(tree)
|
visitor.visit(tree)
|
||||||
values: List[Any] = []
|
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:
|
if k in visitor.nonlocals:
|
||||||
values.append(v)
|
values.append(v)
|
||||||
for kk in visitor.nonlocals:
|
for kk in visitor.nonlocals:
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import sys
|
import sys
|
||||||
from typing import Callable, Dict
|
from typing import Callable, Dict, Tuple
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
|
|
||||||
@ -41,6 +41,9 @@ def test_indent_lines_after_first(text: str, prefix: str, expected_output: str)
|
|||||||
assert indented_text == expected_output
|
assert indented_text == expected_output
|
||||||
|
|
||||||
|
|
||||||
|
global_agent = RunnableLambda(lambda x: x * 3)
|
||||||
|
|
||||||
|
|
||||||
def test_nonlocals() -> None:
|
def test_nonlocals() -> None:
|
||||||
agent = RunnableLambda(lambda x: x * 2)
|
agent = RunnableLambda(lambda x: x * 2)
|
||||||
|
|
||||||
@ -53,7 +56,23 @@ def test_nonlocals() -> None:
|
|||||||
def my_func3(input: str) -> str:
|
def my_func3(input: str) -> str:
|
||||||
return agent.invoke(input)
|
return agent.invoke(input)
|
||||||
|
|
||||||
|
def my_func4(input: str) -> str:
|
||||||
|
return global_agent.invoke(input)
|
||||||
|
|
||||||
|
def my_func5() -> Tuple[Callable[[str], str], RunnableLambda]:
|
||||||
|
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_func) == []
|
||||||
assert get_function_nonlocals(my_func2) == []
|
assert get_function_nonlocals(my_func2) == []
|
||||||
assert get_function_nonlocals(my_func3) == [agent.invoke]
|
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_func3).deps == [agent]
|
||||||
|
assert RunnableLambda(my_func4).deps == [global_agent]
|
||||||
|
assert RunnableLambda(func).deps == [nl]
|
||||||
|
Loading…
Reference in New Issue
Block a user