aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--nb/core.py37
-rw-r--r--nbs/core.ipynb184
2 files changed, 199 insertions, 22 deletions
diff --git a/nb/core.py b/nb/core.py
index 90f5d00..04a313b 100644
--- a/nb/core.py
+++ b/nb/core.py
@@ -15,16 +15,41 @@ def pubattr(o):
return [a for a in dir(o) if not a.startswith('_')]
# cell: 78d61a07
-def patch_to(o: object) -> Callable:
+def patch_to(
+ o:object, # The object to patch.
+ cls_method:bool=False, # Patch the decorated function to `o` as a classmethod
+ static_method:bool=False, # Patch the decorated function to `o` as a staticmethod
+ prop:bool=False, # Patch the decorated function to `o` as a property
+)->Callable:
"Decorator that takes in a object and attaches the decorated function onto it."
- def inner(fn: Callable) -> Callable:
+ def inner(fn:Callable)->Callable:
nm = callable_name(fn)
+ if cls_method: fn = classmethod(fn)
+ if static_method: fn = staticmethod(fn)
+ if prop: fn = property(fn)
setattr(o, nm, fn)
return fn
return inner
# cell: 4eb650ab
-def patch(fn: Callable) -> Callable:
- "Decorator to patch the object of the type-hinted 'self' argument of `fn` with `fn`."
- self = inspect.get_annotations(fn)['self']
- return patch_to(self)(fn)
+def patch(
+ fn:Callable|None=None, # The function to patch onto the object specified via a `self` or `cls` type hint.
+ prop:bool=False, # Whether to patch `fn` onto the `self` type hint as a property.
+)->Callable: # The patched `fn`
+ """Decorator to patch an object using type hints on `fn`.
+
+ `fn` must have either `self` or `cls` arguments present with a type hint.
+ - `self`: Created as either an instance method or property if `prop=True`.
+ - `cls`: Created as a class method
+
+ If `self` and `cls` is missing or `self` is missing with `prop=True`,
+ a ValueError is raised.
+ """
+ def inner(fn:Callable)->Callable:
+ anno = inspect.get_annotations(fn)
+ if 'self' not in anno and prop: raise ValueError('Must have `self` argument with prop=True')
+ if 'self' in anno: return patch_to(anno['self'], prop=prop)(fn)
+ if 'cls' in anno: return patch_to(anno['cls'], cls_method=True)(fn)
+ raise ValueError('Must have either `self` or `cls` argument when using patch')
+ if fn is not None: return inner(fn) # @patch case, as opposed to @patch()
+ return inner
diff --git a/nbs/core.ipynb b/nbs/core.ipynb
index 849949e..5df15b2 100644
--- a/nbs/core.ipynb
+++ b/nbs/core.ipynb
@@ -169,13 +169,21 @@
"outputs": [],
"source": [
"#| export\n",
- "def patch_to(o: object) -> Callable:\n",
+ "def patch_to(\n",
+ " o:object, # The object to patch.\n",
+ " cls_method:bool=False, # Patch the decorated function to `o` as a classmethod\n",
+ " static_method:bool=False, # Patch the decorated function to `o` as a staticmethod\n",
+ " prop:bool=False, # Patch the decorated function to `o` as a property\n",
+ ")->Callable:\n",
" \"Decorator that takes in a object and attaches the decorated function onto it.\"\n",
- " def inner(fn: Callable) -> Callable:\n",
+ " def inner(fn:Callable)->Callable:\n",
" nm = callable_name(fn)\n",
+ " if cls_method: fn = classmethod(fn)\n",
+ " if static_method: fn = staticmethod(fn)\n",
+ " if prop: fn = property(fn)\n",
" setattr(o, nm, fn)\n",
" return fn\n",
- " return inner"
+ " return inner\n"
]
},
{
@@ -212,8 +220,7 @@
"outputs": [],
"source": [
"@patch_to(_T)\n",
- "def say_hello(self: _T, name: str) -> str:\n",
- " return f'Hello, {name}'"
+ "def say_hello(self: _T, name: str): return f'Hello, {name}'"
]
},
{
@@ -237,6 +244,93 @@
},
{
"cell_type": "markdown",
+ "id": "b9c102e7",
+ "metadata": {},
+ "source": [
+ "We can also do this with class methods:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "f5eca4e3",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "@patch_to(_T, cls_method=True)\n",
+ "def say_good_morning(cls:_T): return 'Good morning'"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "3a52ddb4",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "test.equal(_T.say_good_morning(), 'Good morning')"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "29565467",
+ "metadata": {},
+ "source": [
+ "And with static methods:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "da2e0279",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "@patch_to(_T, static_method=True)\n",
+ "def say_good_evening(): return 'Good evening'"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "d3802615",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "test.equal(_T.say_good_evening(), 'Good evening')"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "6e694762",
+ "metadata": {},
+ "source": [
+ "And properties:"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "be5a1f11",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "@patch_to(_T, prop=True)\n",
+ "def hi(self:_T): return 'hi'"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "d637ab8e",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "test.equal(_T().hi, 'hi')"
+ ]
+ },
+ {
+ "cell_type": "markdown",
"id": "4acd774c",
"metadata": {},
"source": [
@@ -255,10 +349,35 @@
"outputs": [],
"source": [
"#| export\n",
- "def patch(fn: Callable) -> Callable:\n",
- " \"Decorator to patch the object of the type-hinted 'self' argument of `fn` with `fn`.\"\n",
- " self = inspect.get_annotations(fn)['self']\n",
- " return patch_to(self)(fn)"
+ "def patch(\n",
+ " fn:Callable|None=None, # The function to patch onto the object specified via a `self` or `cls` type hint.\n",
+ " prop:bool=False, # Whether to patch `fn` onto the `self` type hint as a property.\n",
+ ")->Callable: # The patched `fn`\n",
+ " \"\"\"Decorator to patch an object using type hints on `fn`.\n",
+ "\n",
+ " `fn` must have either `self` or `cls` arguments present with a type hint.\n",
+ " - `self`: Created as either an instance method or property if `prop=True`.\n",
+ " - `cls`: Created as a class method\n",
+ "\n",
+ " If `self` and `cls` is missing or `self` is missing with `prop=True`,\n",
+ " a ValueError is raised.\n",
+ " \"\"\"\n",
+ " def inner(fn:Callable)->Callable:\n",
+ " anno = inspect.get_annotations(fn)\n",
+ " if 'self' not in anno and prop: raise ValueError('Must have `self` argument with prop=True')\n",
+ " if 'self' in anno: return patch_to(anno['self'], prop=prop)(fn)\n",
+ " if 'cls' in anno: return patch_to(anno['cls'], cls_method=True)(fn)\n",
+ " raise ValueError('Must have either `self` or `cls` argument when using patch')\n",
+ " if fn is not None: return inner(fn) # @patch case, as opposed to @patch()\n",
+ " return inner"
+ ]
+ },
+ {
+ "cell_type": "markdown",
+ "id": "13272b5e",
+ "metadata": {},
+ "source": [
+ "The one caveat is we can't easily support static methods because we naturally may have no arguments to investigate the type hints of to know which object to patch with static methods."
]
},
{
@@ -277,26 +396,59 @@
"outputs": [],
"source": [
"@patch\n",
- "def say_goodbye(self: _T, name: str) -> str:\n",
- " return f'Goodbye, {name}'"
+ "def say_goodbye(self:_T, name): return f'Goodbye, {name}'"
]
},
{
- "cell_type": "markdown",
- "id": "d001522d",
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "6aa1ffbc",
"metadata": {},
+ "outputs": [],
"source": [
- "Can our `_t` object now say goodbye?"
+ "test.equal(_T().say_goodbye('eugene'), 'Goodbye, eugene')"
]
},
{
"cell_type": "code",
"execution_count": null,
- "id": "6aa1ffbc",
+ "id": "b5d14f35",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "@patch\n",
+ "def say_bye(cls:_T): return 'bye'"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "f3ce8fe7",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "test.equal(_T.say_bye(), 'bye')"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "0c59c003",
+ "metadata": {},
+ "outputs": [],
+ "source": [
+ "@patch(prop=True)\n",
+ "def bye(self:_T): return 'bye'"
+ ]
+ },
+ {
+ "cell_type": "code",
+ "execution_count": null,
+ "id": "5fedf001",
"metadata": {},
"outputs": [],
"source": [
- "test.equal(_t.say_goodbye('eugene'), 'Goodbye, eugene')"
+ "test.equal(_T().bye, 'bye')"
]
},
{