mirror of
https://github.com/hwchase17/langchain.git
synced 2025-09-05 21:12:48 +00:00
core[patch]: fix beta, deprecated typing (#18877)
**Description:** While not technically incorrect, the TypeVar used for the `@beta` decorator prevented pyright (and thus most vscode users) from correctly seeing the types of functions/classes decorated with `@beta`. This is in part due to a small bug in pyright (https://github.com/microsoft/pyright/issues/7448 ) - however, the `Type` bound in the typevar `C = TypeVar("C", Type, Callable)` is not doing anything - classes are `Callables` by default, so by my understanding binding to `Type` does not actually provide any more safety - the modified annotation still works correctly for both functions, properties, and classes. --------- Co-authored-by: Bagatur <22008038+baskaryan@users.noreply.github.com> Co-authored-by: Bagatur <baskaryan@gmail.com>
This commit is contained in:
@@ -9,11 +9,12 @@ https://github.com/matplotlib/matplotlib/blob/main/lib/matplotlib/_api/deprecati
|
||||
This module is for internal use only. Do not use it in your own code.
|
||||
We may change the API at any time with no warning.
|
||||
"""
|
||||
|
||||
import contextlib
|
||||
import functools
|
||||
import inspect
|
||||
import warnings
|
||||
from typing import Any, Callable, Generator, Type, TypeVar
|
||||
from typing import Any, Callable, Generator, Type, TypeVar, Union, cast
|
||||
|
||||
from langchain_core._api.internal import is_caller_internal
|
||||
|
||||
@@ -25,7 +26,7 @@ class LangChainBetaWarning(DeprecationWarning):
|
||||
# PUBLIC API
|
||||
|
||||
|
||||
T = TypeVar("T", Type, Callable)
|
||||
T = TypeVar("T", bound=Union[Callable[..., Any], Type])
|
||||
|
||||
|
||||
def beta(
|
||||
@@ -143,7 +144,7 @@ def beta(
|
||||
obj.__init__ = functools.wraps(obj.__init__)( # type: ignore[misc]
|
||||
warn_if_direct_instance
|
||||
)
|
||||
return obj
|
||||
return cast(T, obj)
|
||||
|
||||
elif isinstance(obj, property):
|
||||
if not _obj_type:
|
||||
@@ -202,7 +203,7 @@ def beta(
|
||||
"""
|
||||
wrapper = functools.wraps(wrapped)(wrapper)
|
||||
wrapper.__doc__ = new_doc
|
||||
return wrapper
|
||||
return cast(T, wrapper)
|
||||
|
||||
old_doc = inspect.cleandoc(old_doc or "").strip("\n")
|
||||
|
||||
@@ -225,9 +226,10 @@ def beta(
|
||||
)
|
||||
|
||||
if inspect.iscoroutinefunction(obj):
|
||||
return finalize(awarning_emitting_wrapper, new_doc)
|
||||
finalized = finalize(awarning_emitting_wrapper, new_doc)
|
||||
else:
|
||||
return finalize(warning_emitting_wrapper, new_doc)
|
||||
finalized = finalize(warning_emitting_wrapper, new_doc)
|
||||
return cast(T, finalized)
|
||||
|
||||
return beta
|
||||
|
||||
|
@@ -14,7 +14,7 @@ import contextlib
|
||||
import functools
|
||||
import inspect
|
||||
import warnings
|
||||
from typing import Any, Callable, Generator, Type, TypeVar
|
||||
from typing import Any, Callable, Generator, Type, TypeVar, Union, cast
|
||||
|
||||
from langchain_core._api.internal import is_caller_internal
|
||||
|
||||
@@ -30,7 +30,7 @@ class LangChainPendingDeprecationWarning(PendingDeprecationWarning):
|
||||
# PUBLIC API
|
||||
|
||||
|
||||
T = TypeVar("T", Type, Callable)
|
||||
T = TypeVar("T", bound=Union[Type, Callable[..., Any]])
|
||||
|
||||
|
||||
def deprecated(
|
||||
@@ -182,7 +182,7 @@ def deprecated(
|
||||
obj.__init__ = functools.wraps(obj.__init__)( # type: ignore[misc]
|
||||
warn_if_direct_instance
|
||||
)
|
||||
return obj
|
||||
return cast(T, obj)
|
||||
|
||||
elif isinstance(obj, property):
|
||||
if not _obj_type:
|
||||
@@ -241,7 +241,7 @@ def deprecated(
|
||||
"""
|
||||
wrapper = functools.wraps(wrapped)(wrapper)
|
||||
wrapper.__doc__ = new_doc
|
||||
return wrapper
|
||||
return cast(T, wrapper)
|
||||
|
||||
old_doc = inspect.cleandoc(old_doc or "").strip("\n")
|
||||
|
||||
@@ -267,9 +267,10 @@ def deprecated(
|
||||
)
|
||||
|
||||
if inspect.iscoroutinefunction(obj):
|
||||
return finalize(awarning_emitting_wrapper, new_doc)
|
||||
finalized = finalize(awarning_emitting_wrapper, new_doc)
|
||||
else:
|
||||
return finalize(warning_emitting_wrapper, new_doc)
|
||||
finalized = finalize(warning_emitting_wrapper, new_doc)
|
||||
return cast(T, finalized)
|
||||
|
||||
return deprecate
|
||||
|
||||
|
@@ -308,7 +308,7 @@ def convert_to_openai_function(
|
||||
elif isinstance(function, type) and issubclass(function, BaseModel):
|
||||
return cast(Dict, convert_pydantic_to_openai_function(function))
|
||||
elif isinstance(function, BaseTool):
|
||||
return format_tool_to_openai_function(function)
|
||||
return cast(Dict, format_tool_to_openai_function(function))
|
||||
elif callable(function):
|
||||
return convert_python_function_to_openai_function(function)
|
||||
else:
|
||||
|
@@ -23,7 +23,9 @@ def _fake_runnable(
|
||||
class FakeStructuredChatModel(FakeListChatModel):
|
||||
"""Fake ChatModel for testing purposes."""
|
||||
|
||||
def with_structured_output(self, schema: Union[Dict, Type[BaseModel]]) -> Runnable:
|
||||
def with_structured_output(
|
||||
self, schema: Union[Dict, Type[BaseModel]], **kwargs: Any
|
||||
) -> Runnable:
|
||||
return RunnableLambda(partial(_fake_runnable, schema))
|
||||
|
||||
@property
|
||||
|
Reference in New Issue
Block a user