From 72c108b003222c76191d755db7e13bb1ce8a9a14 Mon Sep 17 00:00:00 2001 From: Bagatur <22008038+baskaryan@users.noreply.github.com> Date: Wed, 22 Nov 2023 16:26:37 -0800 Subject: [PATCH] IMPROVEMENT: filter global warnings properly (#13754) --- libs/core/langchain_core/globals/__init__.py | 12 +++++------ .../tests/unit_tests/test_globals.py | 21 +++++++++++++++++++ 2 files changed, 27 insertions(+), 6 deletions(-) diff --git a/libs/core/langchain_core/globals/__init__.py b/libs/core/langchain_core/globals/__init__.py index e6210d53ac8..89963f4dace 100644 --- a/libs/core/langchain_core/globals/__init__.py +++ b/libs/core/langchain_core/globals/__init__.py @@ -27,7 +27,7 @@ def set_verbose(value: bool) -> None: warnings.filterwarnings( "ignore", message=( - "Importing verbose from langchain_core root module is no longer supported" + "Importing verbose from langchain root module is no longer supported" ), ) # N.B.: This is a workaround for an unfortunate quirk of Python's @@ -55,7 +55,7 @@ def get_verbose() -> bool: warnings.filterwarnings( "ignore", message=( - "Importing verbose from langchain_core root module is no longer supported" + ".*Importing verbose from langchain root module is no longer supported" ), ) # N.B.: This is a workaround for an unfortunate quirk of Python's @@ -87,7 +87,7 @@ def set_debug(value: bool) -> None: with warnings.catch_warnings(): warnings.filterwarnings( "ignore", - message="Importing debug from langchain_core root module is no longer supported", + message="Importing debug from langchain root module is no longer supported", ) # N.B.: This is a workaround for an unfortunate quirk of Python's # module-level `__getattr__()` implementation: @@ -113,7 +113,7 @@ def get_debug() -> bool: with warnings.catch_warnings(): warnings.filterwarnings( "ignore", - message="Importing debug from langchain_core root module is no longer supported", + message="Importing debug from langchain root module is no longer supported", ) # N.B.: This is a workaround for an unfortunate quirk of Python's # module-level `__getattr__()` implementation: @@ -145,7 +145,7 @@ def set_llm_cache(value: Optional["BaseCache"]) -> None: warnings.filterwarnings( "ignore", message=( - "Importing llm_cache from langchain_core root module is no longer supported" + "Importing llm_cache from langchain root module is no longer supported" ), ) # N.B.: This is a workaround for an unfortunate quirk of Python's @@ -173,7 +173,7 @@ def get_llm_cache() -> "BaseCache": warnings.filterwarnings( "ignore", message=( - "Importing llm_cache from langchain_core root module is no longer supported" + "Importing llm_cache from langchain root module is no longer supported" ), ) # N.B.: This is a workaround for an unfortunate quirk of Python's diff --git a/libs/langchain/tests/unit_tests/test_globals.py b/libs/langchain/tests/unit_tests/test_globals.py index 5df209c165c..b1d073ca58a 100644 --- a/libs/langchain/tests/unit_tests/test_globals.py +++ b/libs/langchain/tests/unit_tests/test_globals.py @@ -1,6 +1,27 @@ +import warnings + +from langchain_core.globals import get_debug as core_get_debug +from langchain_core.globals import get_verbose as core_get_verbose +from langchain_core.globals import set_debug as core_set_debug +from langchain_core.globals import set_verbose as core_set_verbose + from langchain.globals import get_debug, get_verbose, set_debug, set_verbose +def test_no_warning() -> None: + with warnings.catch_warnings(): + warnings.simplefilter("error") + + get_debug() + set_debug(False) + get_verbose() + set_verbose(False) + core_get_debug() + core_set_debug(False) + core_get_verbose() + core_set_verbose(False) + + def test_debug_is_settable_directly() -> None: from langchain_core.callbacks.manager import _get_debug