core[patch]: add alternative_import to deprecated (#15781)

This commit is contained in:
Erick Friis 2024-01-09 14:45:28 -08:00 committed by GitHub
parent 04a5a37e92
commit 656e87beb9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 28 additions and 5 deletions

View File

@ -39,6 +39,7 @@ def deprecated(
message: str = "",
name: str = "",
alternative: str = "",
alternative_import: str = "",
pending: bool = False,
obj_type: str = "",
addendum: str = "",
@ -105,6 +106,7 @@ def deprecated(
_name: str = name,
_message: str = message,
_alternative: str = alternative,
_alternative_import: str = alternative_import,
_pending: bool = pending,
_addendum: str = addendum,
) -> T:
@ -117,6 +119,7 @@ def deprecated(
message=_message,
name=_name,
alternative=_alternative,
alternative_import=_alternative_import,
pending=_pending,
obj_type=_obj_type,
addendum=_addendum,
@ -145,7 +148,9 @@ def deprecated(
if not _obj_type:
_obj_type = "class"
wrapped = obj.__init__ # type: ignore
_name = _name or obj.__name__
_name = _name or (
f"{obj.__module__}.{obj.__name__}" if obj.__module__ else obj.__name__
)
old_doc = obj.__doc__
def finalize(_: Any, new_doc: str) -> T:
@ -271,6 +276,7 @@ def warn_deprecated(
message: str = "",
name: str = "",
alternative: str = "",
alternative_import: str = "",
pending: bool = False,
obj_type: str = "",
addendum: str = "",
@ -307,6 +313,10 @@ def warn_deprecated(
"""
if pending and removal:
raise ValueError("A pending deprecation cannot have a scheduled removal")
if alternative and alternative_import:
raise ValueError("Cannot specify both alternative and alternative_import")
if alternative_import and "." not in alternative_import:
raise ValueError("alternative_import must be a fully qualified module path")
if not pending:
if not removal:
@ -320,6 +330,7 @@ def warn_deprecated(
if not message:
message = ""
package = name.split(".")[0].replace("_", "-") if "." in name else "LangChain"
if obj_type:
message += f"The {obj_type} `{name}`"
@ -329,12 +340,24 @@ def warn_deprecated(
if pending:
message += " will be deprecated in a future version"
else:
message += f" was deprecated in LangChain {since}"
message += f" was deprecated in {package} {since}"
if removal:
message += f" and will be removed {removal}"
if alternative:
if alternative_import:
alt_package = alternative_import.split(".")[0].replace("_", "-")
if alt_package == package:
message += f". Use {alternative_import} instead."
else:
alt_module, alt_name = alternative_import.rsplit(".", 1)
message += (
f". An updated version of the {obj_type} exists in the "
f"{alt_package} package and should be used instead. To use it run "
f"`pip install -U {alt_package}` and import as "
f"`from {alt_module} import {alt_name}`."
)
elif alternative:
message += f". Use {alternative} instead."
if addendum:

View File

@ -219,8 +219,8 @@ def test_whole_class_deprecation() -> None:
assert len(warning_list) == 2
warning = warning_list[0].message
assert str(warning) == (
"The class `DeprecatedClass` was deprecated in "
"LangChain 2.0.0 and will be removed in 3.0.0"
"The class `tests.unit_tests._api.test_deprecation.DeprecatedClass` was "
"deprecated in tests 2.0.0 and will be removed in 3.0.0"
)
warning = warning_list[1].message