Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch decorator with optional argument #110

Merged
merged 3 commits into from
Oct 7, 2020
Merged
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
5 changes: 3 additions & 2 deletions fastcore/foundation.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ def _inner(f):
return _inner

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

# Cell
def patch_property(f):
Expand Down
42 changes: 38 additions & 4 deletions nbs/01_foundation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,7 @@
"@patch_to(_T5, cls_method=True)\n",
"def func(cls, x): return cls.attr + x # you can access class attributes in the normal way\n",
"\n",
"t = _T5()\n",
"test_eq(t.func(4), 7)"
"test_eq(_T5.func(4), 7)"
]
},
{
Expand Down Expand Up @@ -259,10 +258,11 @@
"outputs": [],
"source": [
"#export\n",
"def patch(f):\n",
"def patch(f=None, *, as_prop=False, cls_method=False):\n",
" \"Decorator: add `f` to the first parameter's class (based on f's type annotations)\"\n",
" if f is None: return partial(patch, as_prop=as_prop, cls_method=cls_method)\n",
" cls = next(iter(f.__annotations__.values()))\n",
" return patch_to(cls)(f)"
" return patch_to(cls, as_prop=as_prop, cls_method=cls_method)(f)"
]
},
{
Expand Down Expand Up @@ -315,6 +315,40 @@
"test_eq(t.func2.__qualname__, '_T9.func2')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Just like `patch_to` decorator you can use `as_propas_prop` and `cls_method` parameters with `patch` decorator:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"@patch(as_prop=True)\n",
"def add_ten(self:_T5): return self + 10\n",
"\n",
"t = _T5(4)\n",
"test_eq(t.add_ten, 14)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class _T5(int): attr = 3 # attr is a class attribute we will access in a later method\n",
" \n",
"@patch(cls_method=True)\n",
"def func(cls:_T5, x): return cls.attr + x # you can access class attributes in the normal way\n",
"\n",
"test_eq(_T5.func(4), 7)"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down