diff options
Diffstat (limited to 'nbs/core.ipynb')
| -rw-r--r-- | nbs/core.ipynb | 65 |
1 files changed, 57 insertions, 8 deletions
diff --git a/nbs/core.ipynb b/nbs/core.ipynb index 6e050ae..46c7085 100644 --- a/nbs/core.ipynb +++ b/nbs/core.ipynb @@ -31,7 +31,7 @@ "source": [ "#| export\n", "import enum,inspect,re\n", - "from typing import Callable\n", + "from typing import Callable,IO\n", "\n", "import nbformat\n", "from nbformat import NotebookNode" @@ -80,7 +80,7 @@ "id": "268db6ac", "metadata": {}, "source": [ - "To read in `ipynb` files, we can use a library provided Jupyter called `nbformat`." + "To read in `ipynb` files, we can use a library provided by Jupyter called `nbformat`." ] }, { @@ -267,7 +267,7 @@ "id": "97162699", "metadata": {}, "source": [ - "A regular expression ran against each line in a cell should allow us to find each directive we support. Since we'll need to re-create this regression expression for a few directives, we'll create a small helper." + "A regular expression ran against each line in a cell should allow us to find each directive we support. Since we'll need to re-create this regular expression for a few directives, we'll create a small helper." ] }, { @@ -394,7 +394,8 @@ "test.is_true(has_directive('#| default_exp: bootstrap'))\n", "test.is_true(has_directive('#| export'))\n", "test.is_true(has_directive('max(1,20)\\n#| export'))\n", - "test.is_false(has_directive('# comment\\nmax(1, 20)\\n'))" + "test.is_false(has_directive('# comment\\nmax(1, 20)\\n'))\n", + "test.is_false(has_directive('#| notarealdirective'))" ] }, { @@ -727,7 +728,7 @@ "\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", + " - `cls`: Created as a class method e.g. type[MyClass]\n", "\n", " If `self` and `cls` is missing or `self` is missing with `prop=True`,\n", " a ValueError is raised.\n", @@ -736,7 +737,7 @@ " 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", + " if 'cls' in anno: return patch_to(anno['cls'].__args__[0], 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" @@ -772,6 +773,48 @@ { "cell_type": "code", "execution_count": null, + "id": "f4be06e3-4630-41fa-ba15-cc4a31bc990d", + "metadata": {}, + "outputs": [], + "source": [ + "import inspect" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "e0830676-7420-4914-9888-60b508d69c6c", + "metadata": {}, + "outputs": [], + "source": [ + "class Test: ...\n", + "def say_test(cls: Test, name): return f'test {name}' " + ] + }, + { + "cell_type": "code", + "execution_count": null, + "id": "b1f7d9c9-d842-4cb1-b64d-ba3b4ea45b57", + "metadata": {}, + "outputs": [ + { + "data": { + "text/plain": [ + "__main__.Test" + ] + }, + "execution_count": null, + "metadata": {}, + "output_type": "execute_result" + } + ], + "source": [ + "inspect.get_annotations(say_test)['cls']" + ] + }, + { + "cell_type": "code", + "execution_count": null, "id": "6aa1ffbc", "metadata": {}, "outputs": [], @@ -787,7 +830,7 @@ "outputs": [], "source": [ "@patch\n", - "def say_bye(cls:_T): return 'bye'" + "def say_bye(cls:type[_T]): return 'bye'" ] }, { @@ -832,7 +875,13 @@ ] } ], - "metadata": {}, + "metadata": { + "kernelspec": { + "display_name": "python3", + "language": "python", + "name": "python3" + } + }, "nbformat": 4, "nbformat_minor": 5 } |