Skip to content

Commit a434735

Browse files
authored
Merge pull request #110 from Salehbigdeli/patch-decorator-with-optional-argument
Patch decorator with optional argument
2 parents 767e34d + 604c2f8 commit a434735

File tree

2 files changed

+41
-6
lines changed

2 files changed

+41
-6
lines changed

fastcore/foundation.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,11 @@ def _inner(f):
3939
return _inner
4040

4141
# Cell
42-
def patch(f):
42+
def patch(f=None, *, as_prop=False, cls_method=False):
4343
"Decorator: add `f` to the first parameter's class (based on f's type annotations)"
44+
if f is None: return partial(patch, as_prop=as_prop, cls_method=cls_method)
4445
cls = next(iter(f.__annotations__.values()))
45-
return patch_to(cls)(f)
46+
return patch_to(cls, as_prop=as_prop, cls_method=cls_method)(f)
4647

4748
# Cell
4849
def patch_property(f):

nbs/01_foundation.ipynb

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,7 @@
203203
"@patch_to(_T5, cls_method=True)\n",
204204
"def func(cls, x): return cls.attr + x # you can access class attributes in the normal way\n",
205205
"\n",
206-
"t = _T5()\n",
207-
"test_eq(t.func(4), 7)"
206+
"test_eq(_T5.func(4), 7)"
208207
]
209208
},
210209
{
@@ -259,10 +258,11 @@
259258
"outputs": [],
260259
"source": [
261260
"#export\n",
262-
"def patch(f):\n",
261+
"def patch(f=None, *, as_prop=False, cls_method=False):\n",
263262
" \"Decorator: add `f` to the first parameter's class (based on f's type annotations)\"\n",
263+
" if f is None: return partial(patch, as_prop=as_prop, cls_method=cls_method)\n",
264264
" cls = next(iter(f.__annotations__.values()))\n",
265-
" return patch_to(cls)(f)"
265+
" return patch_to(cls, as_prop=as_prop, cls_method=cls_method)(f)"
266266
]
267267
},
268268
{
@@ -315,6 +315,40 @@
315315
"test_eq(t.func2.__qualname__, '_T9.func2')"
316316
]
317317
},
318+
{
319+
"cell_type": "markdown",
320+
"metadata": {},
321+
"source": [
322+
"Just like `patch_to` decorator you can use `as_propas_prop` and `cls_method` parameters with `patch` decorator:"
323+
]
324+
},
325+
{
326+
"cell_type": "code",
327+
"execution_count": null,
328+
"metadata": {},
329+
"outputs": [],
330+
"source": [
331+
"@patch(as_prop=True)\n",
332+
"def add_ten(self:_T5): return self + 10\n",
333+
"\n",
334+
"t = _T5(4)\n",
335+
"test_eq(t.add_ten, 14)"
336+
]
337+
},
338+
{
339+
"cell_type": "code",
340+
"execution_count": null,
341+
"metadata": {},
342+
"outputs": [],
343+
"source": [
344+
"class _T5(int): attr = 3 # attr is a class attribute we will access in a later method\n",
345+
" \n",
346+
"@patch(cls_method=True)\n",
347+
"def func(cls:_T5, x): return cls.attr + x # you can access class attributes in the normal way\n",
348+
"\n",
349+
"test_eq(_T5.func(4), 7)"
350+
]
351+
},
318352
{
319353
"cell_type": "code",
320354
"execution_count": null,

0 commit comments

Comments
 (0)