From b27f81c51ce1658cd778366f2068d7277e5d4455 Mon Sep 17 00:00:00 2001 From: Utkarsha Gupte <89600822+UtkarshaGupte@users.noreply.github.com> Date: Thu, 4 Apr 2024 07:22:38 -0700 Subject: [PATCH] core[patch]: mypy ignore fixes #17048 (#19931) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit core/langchain_core/_api[Patch]: mypy ignore fixes #17048 Related to #17048 Applied mypy fixes to below two files: libs/core/langchain_core/_api/deprecation.py libs/core/langchain_core/_api/beta_decorator.py Summary of Fixes: **Issue 1** class _deprecated_property(type(obj)): # type: ignore error: Unsupported dynamic base class "type" [misc] Fix: 1. Added an __init__ method to _deprecated_property to initialize the fget, fset, fdel, and __doc__ attributes. 2. In the __get__, __set__, and __delete__ methods, we now use the self.fget, self.fset, and self.fdel attributes to call the original methods after emitting the warning.
 3. The finalize function now creates an instance of _deprecated_property with the fget, fset, fdel, and doc attributes from the original obj property. 

**Issue 2**
 

 def finalize( # type: ignore wrapper: Callable[..., Any], new_doc: str ) -> T: 

error: All conditional function variants must have identical signatures
 

Fix:
Ensured that both definitions of the finalize function have the same signature Twitter Handle - https://x.com/gupteutkarsha?s=11&t=uwHe4C3PPpGRvoO5Qpm1aA --- .../langchain_core/_api/beta_decorator.py | 32 +++++++++++-------- libs/core/langchain_core/_api/deprecation.py | 32 +++++++++++-------- 2 files changed, 36 insertions(+), 28 deletions(-) diff --git a/libs/core/langchain_core/_api/beta_decorator.py b/libs/core/langchain_core/_api/beta_decorator.py index 84c18c581e..280c1c99c2 100644 --- a/libs/core/langchain_core/_api/beta_decorator.py +++ b/libs/core/langchain_core/_api/beta_decorator.py @@ -124,7 +124,7 @@ def beta( _name = _name or obj.__name__ old_doc = obj.__doc__ - def finalize(_: Any, new_doc: str) -> T: + def finalize(wrapper: Callable[..., Any], new_doc: str) -> T: """Finalize the annotation of a class.""" try: obj.__doc__ = new_doc @@ -153,30 +153,36 @@ def beta( _name = _name or obj.fget.__name__ old_doc = obj.__doc__ - class _beta_property(type(obj)): # type: ignore + class _beta_property(property): """A beta property.""" - def __get__(self, instance, owner=None): # type: ignore + def __init__(self, fget=None, fset=None, fdel=None, doc=None): + super().__init__(fget, fset, fdel, doc) + self.__orig_fget = fget + self.__orig_fset = fset + self.__orig_fdel = fdel + + def __get__(self, instance, owner=None): if instance is not None or owner is not None: emit_warning() - return super().__get__(instance, owner) + return self.fget(instance) - def __set__(self, instance, value): # type: ignore + def __set__(self, instance, value): if instance is not None: emit_warning() - return super().__set__(instance, value) + return self.fset(instance, value) - def __delete__(self, instance): # type: ignore + def __delete__(self, instance): if instance is not None: emit_warning() - return super().__delete__(instance) + return self.fdel(instance) - def __set_name__(self, owner, set_name): # type: ignore + def __set_name__(self, owner, set_name): nonlocal _name if _name == "": _name = set_name - def finalize(_: Any, new_doc: str) -> Any: # type: ignore + def finalize(wrapper: Callable[..., Any], new_doc: str) -> Any: """Finalize the property.""" return _beta_property( fget=obj.fget, fset=obj.fset, fdel=obj.fdel, doc=new_doc @@ -186,12 +192,10 @@ def beta( if not _obj_type: _obj_type = "function" wrapped = obj - _name = _name or obj.__name__ # type: ignore + _name = _name or obj.__name__ old_doc = wrapped.__doc__ - def finalize( # type: ignore - wrapper: Callable[..., Any], new_doc: str - ) -> T: + def finalize(wrapper: Callable[..., Any], new_doc: str) -> T: """Wrap the wrapped function using the wrapper and update the docstring. Args: diff --git a/libs/core/langchain_core/_api/deprecation.py b/libs/core/langchain_core/_api/deprecation.py index 484d591f1b..31304665e0 100644 --- a/libs/core/langchain_core/_api/deprecation.py +++ b/libs/core/langchain_core/_api/deprecation.py @@ -162,7 +162,7 @@ def deprecated( ) old_doc = obj.__doc__ - def finalize(_: Any, new_doc: str) -> T: + def finalize(wrapper: Callable[..., Any], new_doc: str) -> T: """Finalize the deprecation of a class.""" try: obj.__doc__ = new_doc @@ -191,30 +191,36 @@ def deprecated( _name = _name or obj.fget.__name__ old_doc = obj.__doc__ - class _deprecated_property(type(obj)): # type: ignore + class _deprecated_property(property): """A deprecated property.""" - def __get__(self, instance, owner=None): # type: ignore + def __init__(self, fget=None, fset=None, fdel=None, doc=None): + super().__init__(fget, fset, fdel, doc) + self.__orig_fget = fget + self.__orig_fset = fset + self.__orig_fdel = fdel + + def __get__(self, instance, owner=None): if instance is not None or owner is not None: emit_warning() - return super().__get__(instance, owner) + return self.fget(instance) - def __set__(self, instance, value): # type: ignore + def __set__(self, instance, value): if instance is not None: emit_warning() - return super().__set__(instance, value) + return self.fset(instance, value) - def __delete__(self, instance): # type: ignore + def __delete__(self, instance): if instance is not None: emit_warning() - return super().__delete__(instance) + return self.fdel(instance) - def __set_name__(self, owner, set_name): # type: ignore + def __set_name__(self, owner, set_name): nonlocal _name if _name == "": _name = set_name - def finalize(_: Any, new_doc: str) -> Any: # type: ignore + def finalize(wrapper: Callable[..., Any], new_doc: str) -> Any: """Finalize the property.""" return _deprecated_property( fget=obj.fget, fset=obj.fset, fdel=obj.fdel, doc=new_doc @@ -224,12 +230,10 @@ def deprecated( if not _obj_type: _obj_type = "function" wrapped = obj - _name = _name or obj.__name__ # type: ignore + _name = _name or obj.__name__ old_doc = wrapped.__doc__ - def finalize( # type: ignore - wrapper: Callable[..., Any], new_doc: str - ) -> T: + def finalize(wrapper: Callable[..., Any], new_doc: str) -> T: """Wrap the wrapped function using the wrapper and update the docstring. Args: