Add a `copy_property` to compat.py to copy properties of class
@@ -65,6 +65,32 @@ def copy_function(func, global_ctx=None):
return functools.update_wrapper(fn, func)
+def copy_property(prop, global_ctx=None):
+ """
+ Copy the property, with given base global_ctx.
+
+ References
+ ----------
+ 1. https://stackoverflow.com/q/28613518
+ """
+ fget = (
+ copy_function(prop.fget, global_ctx)
+ if isinstance(prop.fget, FunctionType)
+ else prop.fget
+ )
+ fset = (
+ copy_function(prop.fset, global_ctx)
+ if isinstance(prop.fset, FunctionType)
+ else prop.fset
+ )
+ fdel = (
+ copy_function(prop.fdel, global_ctx)
+ if isinstance(prop.fdel, FunctionType)
+ else prop.fdel
+ )
+ return property(fget, fset, fdel)
+
+
def copy_class(cls):
if issubclass(cls, Enum):
return cls
@@ -374,7 +400,11 @@ def with_module_map( # noqa: C901
# run replacing module context again for accurance
global_ctx = replace_context(global_ctx, source_module, target_module)
- fn = copy_function(meth, global_ctx)
+ fn = (
+ copy_property(meth, global_ctx)
+ if isinstance(meth, property)
+ else copy_function(meth, global_ctx)
+ )
if patch_docstring:
setattr(fn, "__doc__", getattr(meth, "__doc__"))
