diff --git a/nbdev/export.py b/nbdev/export.py index e5cf453e2..8c9fc409b 100644 --- a/nbdev/export.py +++ b/nbdev/export.py @@ -149,9 +149,13 @@ def export_names(code, func_only=False): "Find the names of the objects, functions or classes defined in `code` that are exported." #Format monkey-patches with @patch def _f(gps): - nm, cls, t = gps.groups() - if cls is not None: return f"def {cls}.{nm}():" - return '\n'.join([f"def {c}.{nm}():" for c in re.split(', *', t[1:-1])]) + nm, c, t = gps.groups() + if c is None: c, delim = t[1:-1], ',' + elif '|' in c: delim = '\|' + else: delim = None + if delim: cs = re.split(f'{delim} *', c) + else: cs = [c] + return '\n'.join([f'def {c}.{nm}():' for c in cs]) code = _re_typedispatch_func.sub('', code) code = _re_patch_func.sub(_f, code) diff --git a/nbs/00_export.ipynb b/nbs/00_export.ipynb index 4e8c5da54..dca456dcd 100644 --- a/nbs/00_export.ipynb +++ b/nbs/00_export.ipynb @@ -216,10 +216,10 @@ " 'name': 'python',\n", " 'nbconvert_exporter': 'python',\n", " 'pygments_lexer': 'ipython3',\n", - " 'version': '3.9.5'},\n", + " 'version': '3.9.7'},\n", " 'toc': {'base_numbering': 1,\n", " 'nav_menu': {},\n", - " 'number_sections': False,\n", + " 'number_sections': True,\n", " 'sideBar': True,\n", " 'skip_h1_title': False,\n", " 'title_cell': 'Table of Contents',\n", @@ -227,7 +227,7 @@ " 'toc_cell': False,\n", " 'toc_position': {},\n", " 'toc_section_display': True,\n", - " 'toc_window_display': False}}" + " 'toc_window_display': True}}" ] }, "execution_count": null, @@ -275,7 +275,7 @@ "data": { "text/plain": [ "{'cell_type': 'code',\n", - " 'execution_count': None,\n", + " 'execution_count': 1,\n", " 'metadata': {'hide_input': False},\n", " 'outputs': [],\n", " 'source': '#|hide\\n#|default_exp export\\n#|default_cls_lvl 3\\nfrom nbdev.showdoc import show_doc'}" @@ -680,8 +680,12 @@ "test_eq(tst.groups(), (\"func\", \"Class\", None))\n", "tst = _re_patch_func.search(\"\"\"\n", "@patch\n", - "def func (obj:(Class1, Class2), a)\"\"\")\n", - "test_eq(tst.groups(), (\"func\", None, \"(Class1, Class2)\"))\n", + "def func (obj:Class1|Class2, a)\"\"\")\n", + "test_eq(tst.groups(), (\"func\", \"Class1|Class2\", None))\n", + "tst = _re_patch_func.search(\"\"\"\n", + "@patch\n", + "def func (obj:Class1|Class2, a:int)->int:\"\"\")\n", + "test_eq(tst.groups(), (\"func\", \"Class1|Class2\", None))\n", "tst = _re_patch_func.search(\"\"\"\n", "@patch\n", "def func (obj:(Class1, Class2), a:int)->int:\"\"\")\n", @@ -690,8 +694,8 @@ "@patch\n", "@log_args(but='a,b')\n", "@funcs_kwargs\n", - "def func (obj:(Class1, Class2), a:int)->int:\"\"\")\n", - "test_eq(tst.groups(), (\"func\", None, \"(Class1, Class2)\"))\n", + "def func (obj:Class1|Class2, a:int)->int:\"\"\")\n", + "test_eq(tst.groups(), (\"func\", \"Class1|Class2\", None))\n", "tst = _re_patch_func.search(\"\"\"\n", "@patch\n", "@contextmanager\n", @@ -809,9 +813,13 @@ " \"Find the names of the objects, functions or classes defined in `code` that are exported.\"\n", " #Format monkey-patches with @patch\n", " def _f(gps):\n", - " nm, cls, t = gps.groups()\n", - " if cls is not None: return f\"def {cls}.{nm}():\"\n", - " return '\\n'.join([f\"def {c}.{nm}():\" for c in re.split(', *', t[1:-1])])\n", + " nm, c, t = gps.groups()\n", + " if c is None: c, delim = t[1:-1], ','\n", + " elif '|' in c: delim = '\\|'\n", + " else: delim = None\n", + " if delim: cs = re.split(f'{delim} *', c)\n", + " else: cs = [c]\n", + " return '\\n'.join([f'def {c}.{nm}():' for c in cs])\n", "\n", " code = _re_typedispatch_func.sub('', code)\n", " code = _re_patch_func.sub(_f, code)\n", @@ -874,6 +882,7 @@ "test_eq(export_names(\"@patch\\ndef my_func(x:Class):\\n pass\", func_only=True), [\"Class.my_func\"])\n", "test_eq(export_names(\"some code\\n@patch\\ndef my_func(x:Class, y):\\n pass\"), [\"Class.my_func\"])\n", "test_eq(export_names(\"some code\\n@patch\\ndef my_func(x:(Class1,Class2), y):\\n pass\"), [\"Class1.my_func\", \"Class2.my_func\"])\n", + "test_eq(export_names(\"some code\\n@patch\\ndef my_func(x:Class1|Class2, y):\\n pass\"), [\"Class1.my_func\", \"Class2.my_func\"])\n", "\n", "#Check delegates\n", "test_eq(export_names(\"@delegates(keep=True)\\nclass someClass:\\n pass\"), [\"someClass\"])\n", diff --git a/nbs/02_showdoc.ipynb b/nbs/02_showdoc.ipynb index ea0921f5e..35604c093 100644 --- a/nbs/02_showdoc.ipynb +++ b/nbs/02_showdoc.ipynb @@ -39,6 +39,15 @@ "except ModuleNotFoundError: pass" ] }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "from __future__ import annotations" + ] + }, { "cell_type": "markdown", "metadata": {}, @@ -708,10 +717,10 @@ "metadata": {}, "outputs": [], "source": [ - "def o(a:(list, int)): return a\n", + "def o(a:list|int): return a\n", "sig = inspect.signature(o)\n", "params = [format_param(p) for _,p in sig.parameters.items()]\n", - "test_eq(params, ['**`a`**:`(list, int)`'])" + "test_eq(params, ['**`a`**:`list | int`'])" ] }, { @@ -1463,8 +1472,8 @@ " **kwargs\n", "):\n", " return b, (_a(**kwargs))\n", - "_docment = ({\"b\":AttrDict({\"docment\":\"Second\",\"anno\":str,\"default\":inspect._empty}),\n", - " \"a\":AttrDict({\"docment\":\"First passed to `_a`\",\"anno\":int,\"default\":2}),\n", + "_docment = ({\"b\":AttrDict({\"docment\":\"Second\",\"anno\":\"str\",\"default\":inspect._empty}),\n", + " \"a\":AttrDict({\"docment\":\"First passed to `_a`\",\"anno\":\"int\",\"default\":2}),\n", " \"return\": AttrDict({\"docment\":None,\"anno\":inspect._empty,\"default\":inspect._empty})},\n", " [\"a\"])\n", "test_eq(_handle_delegates(_b),_docment)" @@ -1576,7 +1585,7 @@ { "data": { "text/markdown": [ - "
notebook2script
[source]notebook2script
[source]notebook2script
(**`fname`**=*`None`*, **`silent`**=*`False`*, **`to_dict`**=*`False`*, **`bare`**=*`False`*)\n",
"\n",
@@ -1634,13 +1643,13 @@
"_str = '_A._add
[source]_A._add
(**`a`**, **`b`**)\\n\\n\\n\\n||Type|Default|Details|\\n|---|---|---|---|\\n|**`a`**|||First val|\\n|**`b`**|||Second val|\\n'\n",
"test_eq(show_doc(_A._add, disp=False), _str)\n",
"test_eq(show_doc(_A._add, disp=False), _str)\n",
- "_str = '''DocsTestClass.test_self
[source]DocsTestClass.test_self
[source]DocsTestClass.test_self
(**`cls`**, **`arg`**)\n",
"\n",
"'''\n",
"test_eq(show_doc(DocsTestClass.test_self, disp=False), _str)\n",
- "_str = '''DocsTestClass.test_cls
[source]DocsTestClass.test_cls
[source]DocsTestClass.test_cls
(**`arg`**)\n",
"\n",
@@ -1708,8 +1717,8 @@
" )->str: # it's a return string\n",
" return d\n",
"\n",
- "tst_str = '''class
A
[source]A
(**`a`**:`int`=*`2`*, **`b`**:`Sequence`\\\\[`int`\\\\]=*`(1, 2, 3)`*)\\n\\nTest dataclass'''\n",
- "assert tst_str == show_doc(A, disp=False)"
+ "tst_str = '''class
A
[source]A
(**`a`**:`int`=*`2`*, **`b`**:`Sequence[int]`=*`(1, 2, 3)`*)\\n\\nTest dataclass'''\n",
+ "test_eq(show_doc(A, disp=False), tst_str)"
]
},
{
@@ -1763,7 +1772,7 @@
{
"data": {
"text/markdown": [
- "class
DocsTestClass
[source]class
DocsTestClass
[source]DocsTestClass
()\n",
"\n",
@@ -1790,7 +1799,7 @@
{
"data": {
"text/markdown": [
- "DocsTestClass.test
[source]DocsTestClass.test
[source]DocsTestClass.test
()\n",
"\n"
diff --git a/nbs/sync.html b/nbs/sync.html
index 52e79ecdd..f064bb923 100644
--- a/nbs/sync.html
+++ b/nbs/sync.html
@@ -67,7 +67,9 @@ get_name
[source]+
get_name
(obj
)+
get_name
[source]+
get_name
(obj
)Get the name of
@@ -134,7 +136,9 @@obj
get_name
-
qual_name
[source]+
qual_name
(obj
)+
qual_name
[source]+
qual_name
(obj
)Get the qualified name of
@@ -190,7 +194,9 @@obj
qual_name
-
source_nb
[source]+
source_nb
(func
,is_name
=None
,return_all
=False
,mod
=None
)+
source_nb
[source]+
source_nb
(func
,is_name
=None
,return_all
=False
,mod
=None
)Return the name of the notebook where
@@ -278,7 +284,9 @@func
was definedReading the library -
relimport2name
[source]+
relimport2name
(name
,mod_name
)+
relimport2name
[source]+
relimport2name
(name
,mod_name
)Unwarps a relative import in
@@ -368,32 +376,15 @@name
according tomod_name
relimport2name
-
nbdev_update_lib
[source]+
nbdev_update_lib
(fname
:str
=None
,silent
:bool_arg
=False
)+
nbdev_update_lib
[source]+
nbdev_update_lib
(fname
:str
=None
,silent
:bool_arg
=False
)Propagates any change in the modules matching
-fname
to the notebooks that created them-
+- - - -- Type -Default -Details -- -- fname
- str
- None
A python filename or glob to convert -- - -- silent
- bool_arg
- False
Dont print results -||Type|Default|Details| +|---|---|---|---| +|
@@ -451,7 +442,9 @@fname
|str
|None
|A python filename or glob to convert| +|silent
|bool_arg
|False
|Dont print results|Diff & trust notebooks -
nbdev_diff_nbs
[source]+
nbdev_diff_nbs
()+
nbdev_diff_nbs
[source]+
nbdev_diff_nbs
()Prints the diff between an export of the library in notebooks and the actual modules
@@ -507,32 +500,15 @@
nbdev_diff_nbs
-
nbdev_trust_nbs
[source]+
nbdev_trust_nbs
(fname
:str
=None
,force_all
:bool
=False
)+
nbdev_trust_nbs
[source]+
nbdev_trust_nbs
(fname
:str
=None
,force_all
:bool
=False
)Trust notebooks matching
-fname
-
+- - - -- Type -Default -Details -- -- fname
- str
- None
A notebook name or glob to convert -- - -- force_all
- bool
- False
Trust even notebooks that havent changed -||Type|Default|Details| +|---|---|---|---| +|
fname
|str
|None
|A notebook name or glob to convert| +|force_all
|bool
|False
|Trust even notebooks that havent changed|