pull/18714/head
Eugene Yurtsev 3 months ago
parent f6cfc68bcc
commit df32037497

@ -27,36 +27,6 @@ class LangChainDeprecationWarning(DeprecationWarning):
class LangChainPendingDeprecationWarning(PendingDeprecationWarning):
"""A class for issuing deprecation warnings for LangChain users."""
def is_warning_internal() -> bool:
"""Check if the warning is internal to LangChain libraries."""
# Get the current stack frame
try:
stack = inspect.stack(context=1)
for idx, frame in enumerate(stack):
if frame.filename.split("/")[-1] != "deprecation.py":
break
# Here we will be looking at the first frame which is not in deprecation.py
# This frame is the caller of the warning, and it will be the
# @deprecated decorator usually (i.e., the file that uses the decorator)
# We want to step another frame back to get the actual caller of the function
# that uses the decorator.
idx += 1
caller_frame = stack[idx].frame
# You can now use the frame object, for example, to get information about the code
if "__package__" not in caller_frame.f_globals:
return False
package = caller_frame.f_globals["__package__"]
# Most langchain libraries start with langchain.
# So this will suppress warnings from most langchain libraries.
# This does not have to be perfect
return package.startswith("langchain")
except Exception:
return False
SURFACE_INTERNAL_WARNINGS = (
os.environ.get("SURFACE_INTERNAL_WARNINGS", "false").lower() == "true"
)
@ -446,10 +416,9 @@ def caller_aware_warn(
surface_internal_warnings: bool = False,
) -> None:
"""Warn deprecated"""
is_warning_internal()
if surface_internal_warnings:
warnings.warn(message, category=category, stacklevel=2)
else:
if is_warning_internal():
if is_caller_internal(depth=2):
return
warnings.warn(message, category=category, stacklevel=2)

@ -75,6 +75,17 @@ def test_warn_deprecated(kwargs: Dict[str, Any], expected_message: str) -> None:
assert str(warning) == expected_message
def test_internal_warnings_disabled() -> None:
"""Test that internal warnings are disabled properly"""
with patch.object(deprecation, "_get_surface_internal_warnings") as func:
func.return_value = False
with warnings.catch_warnings(record=True) as warning_list:
warnings.simplefilter("always")
warn_deprecated("1.0.0", pending=True)
assert len(warning_list) == 0
def test_undefined_deprecation_schedule() -> None:
"""This test is expected to fail until we defined a deprecation schedule."""
with pytest.raises(NotImplementedError):

Loading…
Cancel
Save