Skip to content

Commit 41dd90a

Browse files
committed
manual patch
1 parent 0b88cba commit 41dd90a

File tree

4 files changed

+34
-53
lines changed

4 files changed

+34
-53
lines changed

fastcore/foundation.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ def patch(f=None, *, as_prop=False, cls_method=False):
4747

4848
# Cell
4949
def patch_property(f):
50-
"Decorator: add `f` as a property to the first parameter's class (based on f's type annotations)"
50+
"Deprecated; use `patch(as_prop=True)` instead"
51+
warnings.warn("`patch_property` is deprecated and will be removed; use `patch(as_prop=True)` instead")
5152
cls = next(iter(f.__annotations__.values()))
5253
return patch_to(cls, as_prop=True)(f)
5354

fastcore/utils.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,13 @@ class Inf(metaclass=_InfMeta):
113113
pass
114114

115115
# Cell
116-
def _oper(op,a,b=float('nan')): return (lambda o:op(o,a)) if b!=b else op(a,b)
116+
_dumobj = object()
117+
def _oper(op,a,b=_dumobj): return (lambda o:op(o,a)) if b is _dumobj else op(a,b)
117118

118119
def _mk_op(nm, mod):
119120
"Create an operator using `oper` and add to the caller's module"
120121
op = getattr(operator,nm)
121-
def _inner(a,b=float('nan')): return _oper(op, a,b)
122+
def _inner(a, b=_dumobj): return _oper(op, a,b)
122123
_inner.__name__ = _inner.__qualname__ = nm
123124
_inner.__doc__ = f'Same as `operator.{nm}`, or returns partial if 1 arg'
124125
mod[nm] = _inner

nbs/01_foundation.ipynb

Lines changed: 2 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -357,57 +357,12 @@
357357
"source": [
358358
"#export\n",
359359
"def patch_property(f):\n",
360-
" \"Decorator: add `f` as a property to the first parameter's class (based on f's type annotations)\"\n",
360+
" \"Deprecated; use `patch(as_prop=True)` instead\"\n",
361+
" warnings.warn(\"`patch_property` is deprecated and will be removed; use `patch(as_prop=True)` instead\")\n",
361362
" cls = next(iter(f.__annotations__.values()))\n",
362363
" return patch_to(cls, as_prop=True)(f)"
363364
]
364365
},
365-
{
366-
"cell_type": "markdown",
367-
"metadata": {},
368-
"source": [
369-
"This is similar to using `patch_to` with the argument `as_prop=True`."
370-
]
371-
},
372-
{
373-
"cell_type": "code",
374-
"execution_count": null,
375-
"metadata": {},
376-
"outputs": [],
377-
"source": [
378-
"class _T3(int): pass\n",
379-
"\n",
380-
"@patch_property\n",
381-
"def prop(self:_T3): return self+1 #this will patch _T3 because of self's type annotation\n",
382-
"\n",
383-
"t = _T3(1)\n",
384-
"test_eq(t.prop, 2)"
385-
]
386-
},
387-
{
388-
"cell_type": "markdown",
389-
"metadata": {},
390-
"source": [
391-
"You may also patch multiple classes at once by passing a tuple of objects as the type annotation for the first argument of your function:"
392-
]
393-
},
394-
{
395-
"cell_type": "code",
396-
"execution_count": null,
397-
"metadata": {},
398-
"outputs": [],
399-
"source": [
400-
"class _T4(int): pass\n",
401-
"\n",
402-
"@patch_property\n",
403-
"def newprop(self:(_T3, _T4)): return self+5\n",
404-
"\n",
405-
"t = _T3(1)\n",
406-
"test_eq(t.newprop, 6)\n",
407-
"t = _T4(1)\n",
408-
"test_eq(t.newprop, 6)"
409-
]
410-
},
411366
{
412367
"cell_type": "code",
413368
"execution_count": null,

nbs/02_utils.ipynb

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@
294294
{
295295
"data": {
296296
"text/plain": [
297-
"<__main__._t at 0x7fb0035b9be0>"
297+
"<__main__._t at 0x7fb002551190>"
298298
]
299299
},
300300
"execution_count": null,
@@ -608,12 +608,13 @@
608608
"outputs": [],
609609
"source": [
610610
"#export\n",
611-
"def _oper(op,a,b=float('nan')): return (lambda o:op(o,a)) if b!=b else op(a,b)\n",
611+
"_dumobj = object()\n",
612+
"def _oper(op,a,b=_dumobj): return (lambda o:op(o,a)) if b is _dumobj else op(a,b)\n",
612613
"\n",
613614
"def _mk_op(nm, mod):\n",
614615
" \"Create an operator using `oper` and add to the caller's module\"\n",
615616
" op = getattr(operator,nm)\n",
616-
" def _inner(a,b=float('nan')): return _oper(op, a,b)\n",
617+
" def _inner(a, b=_dumobj): return _oper(op, a,b)\n",
617618
" _inner.__name__ = _inner.__qualname__ = nm\n",
618619
" _inner.__doc__ = f'Same as `operator.{nm}`, or returns partial if 1 arg'\n",
619620
" mod[nm] = _inner"
@@ -1023,6 +1024,29 @@
10231024
"test_eq(_f(1, '.', 3), (1,Path('.'),'3',0))"
10241025
]
10251026
},
1027+
{
1028+
"cell_type": "code",
1029+
"execution_count": null,
1030+
"metadata": {},
1031+
"outputs": [
1032+
{
1033+
"data": {
1034+
"text/plain": [
1035+
"(1, Path('.'), '3', 0)"
1036+
]
1037+
},
1038+
"execution_count": null,
1039+
"metadata": {},
1040+
"output_type": "execute_result"
1041+
}
1042+
],
1043+
"source": [
1044+
"@with_cast\n",
1045+
"def _f(a, b:Path, c:str='', d=0)->bool: return (a,b,c,d)\n",
1046+
"\n",
1047+
"_f(1, '.', 3)"
1048+
]
1049+
},
10261050
{
10271051
"cell_type": "code",
10281052
"execution_count": null,

0 commit comments

Comments
 (0)