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

Deprecate direct access to globals like debug and verbose. #11311

Merged
merged 29 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
6a928c4
Move globals like `debug` and `verbose` to their own module.
obi1kenobi Oct 2, 2023
eaacf21
Fix lint.
obi1kenobi Oct 2, 2023
0769605
Fix function name.
obi1kenobi Oct 2, 2023
c45c367
Delint.
obi1kenobi Oct 2, 2023
30d8b4f
Delint, take two.
obi1kenobi Oct 2, 2023
f266fba
Workaround for duplicated attributes depending on how they are imported.
obi1kenobi Oct 2, 2023
bbb9987
Merge branch 'master' into pg/move_globals_to_own_module
obi1kenobi Oct 2, 2023
e4e3371
Update state-resetting code.
obi1kenobi Oct 2, 2023
c238b02
Delint.
obi1kenobi Oct 2, 2023
30f8dbc
Add warnings to the repo.
obi1kenobi Oct 2, 2023
d0a28ab
Merge branch 'master' into pg/move_globals_to_own_module
obi1kenobi Oct 3, 2023
7ed2616
Merge branch 'master' into pg/move_globals_to_own_module
obi1kenobi Oct 4, 2023
8154f48
Add explicit `get_<X>()` functions.
obi1kenobi Oct 9, 2023
454c0c8
Update docs to point to new locations.
obi1kenobi Oct 9, 2023
c7b5abf
Remove type hints since type checkers seem to read `__getattr__()`.
obi1kenobi Oct 9, 2023
39baef0
Remove unused imports.
obi1kenobi Oct 9, 2023
6888c55
Import the underscored values in the top level `__init__`.
obi1kenobi Oct 9, 2023
0fbda65
Do not report warnings when using deprecated code internally.
obi1kenobi Oct 9, 2023
4596710
Move `globals` to be a top-level `langchain` namespace.
obi1kenobi Oct 9, 2023
97e398e
Fix test imports.
obi1kenobi Oct 9, 2023
1af7da5
Fix type hint.
obi1kenobi Oct 9, 2023
ef840a1
Use underscored underlying values in tests.
obi1kenobi Oct 9, 2023
41f2e31
Ruff lint.
obi1kenobi Oct 9, 2023
a6260b7
Fix type hint.
obi1kenobi Oct 9, 2023
42c8a9f
Merge branch 'master' into pg/move_globals_to_own_module
hwchase17 Oct 11, 2023
0c55aa1
Merge branch 'pg/move_globals_to_own_module' of github.com:hwchase17/…
hwchase17 Oct 11, 2023
5560519
cr
hwchase17 Oct 11, 2023
c0562d4
cr
hwchase17 Oct 12, 2023
c780715
Merge branch 'master' into pg/move_globals_to_own_module
hwchase17 Oct 12, 2023
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
Prev Previous commit
Next Next commit
Add explicit get_<X>() functions.
  • Loading branch information
obi1kenobi committed Oct 9, 2023
commit 8154f48bc60f579456cc04f45d3e5e1d8c04ec04
39 changes: 32 additions & 7 deletions libs/langchain/langchain/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,17 +29,24 @@ def _is_interactive_env() -> bool:
return hasattr(sys, "ps2")


def _warn_on_import(name: str) -> None:
def _warn_on_import(name: str, replacement: str = None) -> None:
"""Warn on import of deprecated module."""
if _is_interactive_env():
# No warnings for interactive environments.
# This is done to avoid polluting the output of interactive environments
# where users rely on auto-complete and may trigger this warning
# even if they are not using any deprecated modules
return
warnings.warn(
f"Importing {name} from langchain root module is no longer supported."
)

if replacement:
warnings.warn(
f"Importing {name} from langchain root module is no longer supported. "
f"Please use {replacement} instead."
)
else:
warnings.warn(
f"Importing {name} from langchain root module is no longer supported."
)


# Surfaces Deprecation and Pending Deprecation warnings from langchain.
Expand Down Expand Up @@ -327,19 +334,37 @@ def __getattr__(name: str) -> Any:
elif name == "verbose":
from langchain.utils.globals import verbose

_warn_on_import(name)
_warn_on_import(
name,
replacement=(
"langchain.utils.globals.set_verbose() / "
"langchain.utils.globals.get_verbose()",
),
)

return verbose
elif name == "debug":
from langchain.utils.globals import debug

_warn_on_import(name)
_warn_on_import(
name,
replacement=(
"langchain.utils.globals.set_debug() / "
"langchain.utils.globals.get_debug()",
),
)

return debug
elif name == "llm_cache":
from langchain.utils.globals import llm_cache

_warn_on_import(name)
_warn_on_import(
name,
replacement=(
"langchain.utils.globals.set_llm_cache() / "
"langchain.utils.globals.get_llm_cache()",
),
)

return llm_cache
else:
Expand Down
86 changes: 77 additions & 9 deletions libs/langchain/langchain/utils/globals.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,13 @@
from langchain.schema import BaseCache


verbose: bool = False
debug: bool = False
llm_cache: Optional["BaseCache"] = None
# DO NOT USE THESE VALUES DIRECTLY!
# Use them only via `get_<X>()` and `set_<X>()` below,
# or else your code may behave unexpectedly with other uses of these global settings:
# https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004
_verbose: bool = False
_debug: bool = False
_llm_cache: Optional["BaseCache"] = None


def set_verbose(value: bool) -> None:
Expand All @@ -22,8 +26,29 @@ def set_verbose(value: bool) -> None:
# have migrated to using `set_verbose()` here.
langchain.verbose = value

global verbose
verbose = value
global _verbose
_verbose = value


def get_verbose() -> bool:
"""Get the value of the `verbose` global setting."""
import langchain

# N.B.: This is a workaround for an unfortunate quirk of Python's
# module-level `__getattr__()` implementation:
# https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004
#
# Remove it once `langchain.verbose` is no longer supported, and once all users
# have migrated to using `set_verbose()` here.
#
# In the meantime, the `verbose` setting is considered True if either the old
# or the new value are True. This accommodates users who haven't migrated
# to using `set_verbose()` yet. Those users are getting deprecation warnings
# directing them to use `set_verbose()` when they import `langhchain.verbose`.
old_verbose = langchain.verbose

global _verbose
return _verbose or old_verbose


def set_debug(value: bool) -> None:
Expand All @@ -38,8 +63,29 @@ def set_debug(value: bool) -> None:
# have migrated to using `set_debug()` here.
langchain.debug = value

global debug
debug = value
global _debug
_debug = value


def get_debug() -> bool:
"""Get the value of the `debug` global setting."""
import langchain

# N.B.: This is a workaround for an unfortunate quirk of Python's
# module-level `__getattr__()` implementation:
# https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004
#
# Remove it once `langchain.debug` is no longer supported, and once all users
# have migrated to using `set_debug()` here.
#
# In the meantime, the `debug` setting is considered True if either the old
# or the new value are True. This accommodates users who haven't migrated
# to using `set_debug()` yet. Those users are getting deprecation warnings
# directing them to use `set_debug()` when they import `langhchain.debug`.
old_debug = langchain.debug

global _debug
return _debug or old_debug


def set_llm_cache(value: "BaseCache") -> None:
Expand All @@ -54,5 +100,27 @@ def set_llm_cache(value: "BaseCache") -> None:
# have migrated to using `set_llm_cache()` here.
langchain.llm_cache = value

global llm_cache
llm_cache = value
global _llm_cache
_llm_cache = value


def get_llm_cache() -> bool:
"""Get the value of the `llm_cache` global setting."""
import langchain

# N.B.: This is a workaround for an unfortunate quirk of Python's
# module-level `__getattr__()` implementation:
# https://github.com/langchain-ai/langchain/pull/11311#issuecomment-1743780004
#
# Remove it once `langchain.llm_cache` is no longer supported, and once all users
# have migrated to using `set_llm_cache()` here.
#
# In the meantime, the `llm_cache` setting returns whichever of its two backing
# sources is truthy (not `None` and non-empty), or the old value if both are falsy.
# This accommodates users who haven't migrated to using `set_llm_cache()` yet.
# Those users are getting deprecation warnings
# directing them to use `set_llm_cache()` when they import `langhchain.llm_cache`.
old_llm_cache = langchain.llm_cache

global _llm_cache
return _llm_cache or old_llm_cache
14 changes: 13 additions & 1 deletion libs/langchain/tests/unit_tests/test_globals.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from langchain.utils.globals import set_debug, set_verbose
from langchain.utils.globals import get_debug, get_verbose, set_debug, set_verbose


def test_debug_is_settable_directly() -> None:
Expand All @@ -22,6 +22,9 @@ def test_debug_is_settable_directly() -> None:
# If we access `debug` via a function used elsewhere in langchain,
# it also sees the same new value.
assert new_value == new_fn_reading

# If we access `debug` via `get_debug()` we also get the same value.
assert new_value == get_debug()
finally:
# Make sure we don't alter global state, even if the test fails.
# Always reset `debug` to the value it had before.
Expand Down Expand Up @@ -49,6 +52,9 @@ def test_debug_is_settable_via_setter() -> None:
# If we access `debug` via a function used elsewhere in langchain,
# it also sees the same new value.
assert new_value == new_fn_reading

# If we access `debug` via `get_debug()` we also get the same value.
assert new_value == get_debug()
finally:
# Make sure we don't alter global state, even if the test fails.
# Always reset `debug` to the value it had before.
Expand Down Expand Up @@ -76,6 +82,9 @@ def test_verbose_is_settable_directly() -> None:
# If we access `verbose` via a function used elsewhere in langchain,
# it also sees the same new value.
assert new_value == new_fn_reading

# If we access `verbose` via `get_verbose()` we also get the same value.
assert new_value == get_verbose()
finally:
# Make sure we don't alter global state, even if the test fails.
# Always reset `verbose` to the value it had before.
Expand Down Expand Up @@ -103,6 +112,9 @@ def test_verbose_is_settable_via_setter() -> None:
# If we access `verbose` via a function used elsewhere in langchain,
# it also sees the same new value.
assert new_value == new_fn_reading

# If we access `verbose` via `get_verbose()` we also get the same value.
assert new_value == get_verbose()
finally:
# Make sure we don't alter global state, even if the test fails.
# Always reset `verbose` to the value it had before.
Expand Down
Loading