Skip to content

Commit ba002c3

Browse files
committed
fixes #789
1 parent 4070419 commit ba002c3

2 files changed

Lines changed: 10 additions & 12 deletions

File tree

fastcore/basics.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1098,7 +1098,7 @@ def __init__(self, f): self.f = f
10981098
def __get__(self, _, f_cls): return MethodType(self.f, f_cls)
10991099

11001100
# %% ../nbs/01_basics.ipynb #3f2733ef
1101-
def patch_to(cls, as_prop=False, cls_method=False, set_prop=False, nm=None, glb=None, once:bool=False):
1101+
def patch_to(cls, as_prop=False, cls_method=False, set_prop=False, nm=None, glb=None):
11021102
"Decorator: add `f` to `cls`"
11031103
if glb is None: glb = sys._getframe(1).f_globals
11041104
def _inner(f):
@@ -1113,17 +1113,16 @@ def _inner(f):
11131113
elif set_prop: attr = getattr(c_, _nm).setter(nf)
11141114
elif as_prop: attr = property(nf)
11151115
else:
1116-
if hasattr(c_, onm) and once: break
1117-
if hasattr(c_, _nm): setattr(c_, onm, getattr(c_, _nm))
1116+
if hasattr(c_, _nm) and not hasattr(c_, onm): setattr(c_, onm, getattr(c_, _nm))
11181117
attr = nf
11191118
setattr(c_, _nm, attr)
11201119
return glb.get(_nm, builtins.__dict__.get(_nm, None))
11211120
return _inner
11221121

11231122
# %% ../nbs/01_basics.ipynb #8faf7b86
1124-
def patch(f=None, *, as_prop=False, cls_method=False, set_prop=False, nm=None, once:bool=False):
1123+
def patch(f=None, *, as_prop=False, cls_method=False, set_prop=False, nm=None):
11251124
"Decorator: add `f` to the first parameter's class (based on f's type annotations)"
1126-
if f is None: return partial(patch, as_prop=as_prop, cls_method=cls_method, set_prop=set_prop, nm=nm, once=once)
1125+
if f is None: return partial(patch, as_prop=as_prop, cls_method=cls_method, set_prop=set_prop, nm=nm)
11271126
ann,glb,loc = get_annotations_ex(f)
11281127
if cls_method:
11291128
if 'cls' not in ann: raise TypeError(f"@patch with cls_method=True requires 'cls' to have a type annotation")
@@ -1132,7 +1131,7 @@ def patch(f=None, *, as_prop=False, cls_method=False, set_prop=False, nm=None, o
11321131
if not ann: raise TypeError(f"@patch requires the first parameter of `{f.__name__}` to have a type annotation")
11331132
cls = next(iter(ann.values()))
11341133
cls = union2tuple(eval_type(cls, glb, loc))
1135-
return patch_to(cls, as_prop=as_prop, cls_method=cls_method, set_prop=set_prop, nm=nm, once=once, glb=sys._getframe(1).f_globals)(f)
1134+
return patch_to(cls, as_prop=as_prop, cls_method=cls_method, set_prop=set_prop, nm=nm, glb=sys._getframe(1).f_globals)(f)
11361135

11371136
# %% ../nbs/01_basics.ipynb #d1732261
11381137
def compile_re(pat):

nbs/01_basics.ipynb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6509,7 +6509,7 @@
65096509
"outputs": [],
65106510
"source": [
65116511
"#| export\n",
6512-
"def patch_to(cls, as_prop=False, cls_method=False, set_prop=False, nm=None, glb=None, once:bool=False):\n",
6512+
"def patch_to(cls, as_prop=False, cls_method=False, set_prop=False, nm=None, glb=None):\n",
65136513
" \"Decorator: add `f` to `cls`\"\n",
65146514
" if glb is None: glb = sys._getframe(1).f_globals\n",
65156515
" def _inner(f):\n",
@@ -6524,8 +6524,7 @@
65246524
" elif set_prop: attr = getattr(c_, _nm).setter(nf)\n",
65256525
" elif as_prop: attr = property(nf)\n",
65266526
" else:\n",
6527-
" if hasattr(c_, onm) and once: break\n",
6528-
" if hasattr(c_, _nm): setattr(c_, onm, getattr(c_, _nm))\n",
6527+
" if hasattr(c_, _nm) and not hasattr(c_, onm): setattr(c_, onm, getattr(c_, _nm))\n",
65296528
" attr = nf\n",
65306529
" setattr(c_, _nm, attr)\n",
65316530
" return glb.get(_nm, builtins.__dict__.get(_nm, None))\n",
@@ -6718,9 +6717,9 @@
67186717
"outputs": [],
67196718
"source": [
67206719
"#| export\n",
6721-
"def patch(f=None, *, as_prop=False, cls_method=False, set_prop=False, nm=None, once:bool=False):\n",
6720+
"def patch(f=None, *, as_prop=False, cls_method=False, set_prop=False, nm=None):\n",
67226721
" \"Decorator: add `f` to the first parameter's class (based on f's type annotations)\"\n",
6723-
" if f is None: return partial(patch, as_prop=as_prop, cls_method=cls_method, set_prop=set_prop, nm=nm, once=once)\n",
6722+
" if f is None: return partial(patch, as_prop=as_prop, cls_method=cls_method, set_prop=set_prop, nm=nm)\n",
67246723
" ann,glb,loc = get_annotations_ex(f)\n",
67256724
" if cls_method:\n",
67266725
" if 'cls' not in ann: raise TypeError(f\"@patch with cls_method=True requires 'cls' to have a type annotation\")\n",
@@ -6729,7 +6728,7 @@
67296728
" if not ann: raise TypeError(f\"@patch requires the first parameter of `{f.__name__}` to have a type annotation\")\n",
67306729
" cls = next(iter(ann.values()))\n",
67316730
" cls = union2tuple(eval_type(cls, glb, loc))\n",
6732-
" return patch_to(cls, as_prop=as_prop, cls_method=cls_method, set_prop=set_prop, nm=nm, once=once, glb=sys._getframe(1).f_globals)(f)"
6731+
" return patch_to(cls, as_prop=as_prop, cls_method=cls_method, set_prop=set_prop, nm=nm, glb=sys._getframe(1).f_globals)(f)"
67336732
]
67346733
},
67356734
{

0 commit comments

Comments
 (0)