Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion fastcore/basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -1176,7 +1176,15 @@ def _inner(f):
for o in functools.WRAPPER_ASSIGNMENTS: setattr(nf, o, getattr(f,o))
nf.__name__,nf.__qualname__ = _nm,f"{c_.__name__}.{_nm}"
nf.__code__ = nf.__code__.replace(co_qualname=nf.__qualname__)
if hasattr(c_, _nm) and not hasattr(c_, onm): setattr(c_, onm, getattr(c_, _nm))
if hasattr(c_, _nm) and not hasattr(c_, onm):
orig = getattr(c_, _nm)
raw = next((b.__dict__[_nm] for b in c_.__mro__ if _nm in b.__dict__), None)
# Keep a descriptor only if class lookup resolved to it.
if isinstance(raw, staticmethod) and orig is raw.__func__: orig = raw
if isinstance(raw, (classmethod, _clsmethod)) and isinstance(orig, MethodType):
func = raw.f if isinstance(raw, _clsmethod) else raw.__func__
if orig.__self__ is c_ and orig.__func__ is func: orig = raw
setattr(c_, onm, orig)
attr = (_clsmethod(nf) if cls_method else staticmethod(nf) if static_method else
getattr(c_, _nm).setter(nf) if set_prop else property(nf) if as_prop else nf)
setattr(c_, _nm, attr)
Expand Down
167 changes: 165 additions & 2 deletions ship/01_basics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -8177,7 +8177,15 @@
" for o in functools.WRAPPER_ASSIGNMENTS: setattr(nf, o, getattr(f,o))\n",
" nf.__name__,nf.__qualname__ = _nm,f\"{c_.__name__}.{_nm}\"\n",
" nf.__code__ = nf.__code__.replace(co_qualname=nf.__qualname__)\n",
" if hasattr(c_, _nm) and not hasattr(c_, onm): setattr(c_, onm, getattr(c_, _nm))\n",
" if hasattr(c_, _nm) and not hasattr(c_, onm):\n",
" orig = getattr(c_, _nm)\n",
" raw = next((b.__dict__[_nm] for b in c_.__mro__ if _nm in b.__dict__), None)\n",
" # Keep a descriptor only if class lookup resolved to it.\n",
" if isinstance(raw, staticmethod) and orig is raw.__func__: orig = raw\n",
" if isinstance(raw, (classmethod, _clsmethod)) and isinstance(orig, MethodType):\n",
" func = raw.f if isinstance(raw, _clsmethod) else raw.__func__\n",
" if orig.__self__ is c_ and orig.__func__ is func: orig = raw\n",
" setattr(c_, onm, orig)\n",
" attr = (_clsmethod(nf) if cls_method else staticmethod(nf) if static_method else\n",
" getattr(c_, _nm).setter(nf) if set_prop else property(nf) if as_prop else nf)\n",
" setattr(c_, _nm, attr)\n",
Expand Down Expand Up @@ -8577,7 +8585,7 @@
"id": "d9310d77",
"metadata": {},
"source": [
"Patching `classmethod` shouldn't affect how python's inheritance works"
"Patching `classmethod` shouldn't affect how python's inheritance works. Replaced methods remain available as `_orig_<name>`, including class and static methods, with their binding preserved for instances and subclasses."
]
},
{
Expand All @@ -8601,6 +8609,161 @@
"test_eq(child.type_cls(), FastChild)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "d21b29ae",
"metadata": {},
"outputs": [],
"source": [
"class _OriginalClass:\n",
" @classmethod\n",
" def owner(cls): return cls\n",
"\n",
"class _OriginalChild(_OriginalClass): pass\n",
"\n",
"@patch_to(_OriginalClass, cls_method=True)\n",
"def owner(cls): return cls._orig_owner()\n",
"\n",
"for cls in (_OriginalClass, _OriginalChild):\n",
" test_is(cls.owner(), cls)\n",
" test_is(cls().owner(), cls)\n",
" test_is(cls._orig_owner(), cls)\n",
" test_is(cls()._orig_owner(), cls)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "66dead29",
"metadata": {},
"outputs": [],
"source": [
"class _OriginalStatic:\n",
" @staticmethod\n",
" def add(x): return x+1\n",
"\n",
"class _OriginalStaticChild(_OriginalStatic): pass\n",
"\n",
"@patch_to(_OriginalStatic, static_method=True, nm='add')\n",
"def replacement(x): return _OriginalStatic._orig_add(x)+10\n",
"\n",
"for cls in (_OriginalStatic, _OriginalStaticChild):\n",
" test_eq(cls.add(2), 13)\n",
" test_eq(cls().add(2), 13)\n",
" test_eq(cls._orig_add(2), 3)\n",
" test_eq(cls()._orig_add(2), 3)\n",
"\n",
"@patch_to(_OriginalStatic, static_method=True, nm='add')\n",
"def replacement(x): return _OriginalStatic._orig_add(x)+20\n",
"\n",
"test_eq(_OriginalStatic().add(2), 23)\n",
"test_eq(_OriginalStatic()._orig_add(2), 3)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "673d9d48",
"metadata": {},
"outputs": [],
"source": [
"class _PatchedOriginal: pass\n",
"\n",
"@patch(cls_method=True)\n",
"def owner(cls:_PatchedOriginal): return cls\n",
"\n",
"@patch(cls_method=True)\n",
"def owner(cls:_PatchedOriginal): return cls._orig_owner()\n",
"\n",
"class _PatchedOriginalChild(_PatchedOriginal): pass\n",
"\n",
"for cls in (_PatchedOriginal, _PatchedOriginalChild):\n",
" test_is(cls.owner(), cls)\n",
" test_is(cls().owner(), cls)\n",
"\n",
"class _UnpatchedOriginal:\n",
" @classmethod\n",
" def owner(cls): return cls\n",
"\n",
"class _InheritedOriginal(_UnpatchedOriginal): pass\n",
"\n",
"@patch(cls_method=True, nm='owner')\n",
"def replacement(cls:_InheritedOriginal): return cls._orig_owner()\n",
"\n",
"class _InheritedOriginalChild(_InheritedOriginal): pass\n",
"\n",
"test_is(_InheritedOriginal.owner(), _InheritedOriginal)\n",
"test_is(_InheritedOriginalChild().owner(), _InheritedOriginalChild)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "70347555",
"metadata": {},
"outputs": [],
"source": [
"class _OriginalMembers:\n",
" def value(self): return 2\n",
" @property\n",
" def size(self): return 3\n",
"\n",
"@patch_to(_OriginalMembers)\n",
"def value(self): return self._orig_value()+10\n",
"\n",
"@patch_to(_OriginalMembers, as_prop=True)\n",
"def size(self): return self._orig_size+10\n",
"\n",
"test_eq(_OriginalMembers().value(), 12)\n",
"test_eq(_OriginalMembers().size, 13)\n",
"\n",
"class _DynamicOriginalMeta(type):\n",
" def __getattr__(cls, nm):\n",
" if nm == 'value': return 4\n",
" raise AttributeError(nm)\n",
"\n",
"class _DynamicOriginal(metaclass=_DynamicOriginalMeta): pass\n",
"\n",
"@patch_to(_DynamicOriginal)\n",
"def value(self): return self._orig_value+10\n",
"\n",
"test_eq(_DynamicOriginal().value(), 14)\n",
"\n",
"class _OriginalMeta(type):\n",
" def owner(cls): return cls\n",
"\n",
"class _MetaclassOriginal(metaclass=_OriginalMeta): pass\n",
"\n",
"@patch_to(_MetaclassOriginal, cls_method=True)\n",
"def owner(cls): return cls._orig_owner()\n",
"\n",
"test_is(_MetaclassOriginal.owner(), _MetaclassOriginal)\n",
"\n",
"class _OriginalMro: pass\n",
"\n",
"@patch_to(_OriginalMro, cls_method=True)\n",
"def mro(cls): return cls._orig_mro()\n",
"\n",
"test_eq(_OriginalMro.mro(), [_OriginalMro, object])\n",
"\n",
"class _OwnerMeta(type):\n",
" @property\n",
" def owner(cls): return vars(cls).get('_owner_override', lambda: 'metaclass')\n",
"\n",
" @owner.setter\n",
" def owner(cls, replacement): cls._owner_override = replacement.__get__(None, cls)\n",
"\n",
"class _ShadowedOwner(metaclass=_OwnerMeta):\n",
" @classmethod\n",
" def owner(cls): return 'class MRO'\n",
"\n",
"@patch_to(_ShadowedOwner, cls_method=True)\n",
"def owner(cls): return cls._orig_owner()\n",
"\n",
"test_eq(_ShadowedOwner.owner(), 'metaclass')\n"
]
},
{
"cell_type": "markdown",
"id": "62fbc70d",
Expand Down