Skip to content

support py310 style union annotations #636

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

Merged
merged 1 commit into from
Jun 27, 2022
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
10 changes: 7 additions & 3 deletions nbdev/export.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
31 changes: 20 additions & 11 deletions nbs/00_export.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -216,18 +216,18 @@
" '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",
" 'title_sidebar': 'Contents',\n",
" 'toc_cell': False,\n",
" 'toc_position': {},\n",
" 'toc_section_display': True,\n",
" 'toc_window_display': False}}"
" 'toc_window_display': True}}"
]
},
"execution_count": null,
Expand Down Expand Up @@ -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'}"
Expand Down Expand Up @@ -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",
Expand All @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
31 changes: 20 additions & 11 deletions nbs/02_showdoc.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@
"except ModuleNotFoundError: pass"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from __future__ import annotations"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down Expand Up @@ -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`'])"
]
},
{
Expand Down Expand Up @@ -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)"
Expand Down Expand Up @@ -1576,7 +1585,7 @@
{
"data": {
"text/markdown": [
"<h4 id=\"notebook2script\" class=\"doc_header\"><code>notebook2script</code><a href=\"https://github.com/fastai/nbdev/tree/master/nbdev/export.py#L430\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
"<h4 id=\"notebook2script\" class=\"doc_header\"><code>notebook2script</code><a href=\"https://github.com/fastai/nbdev/tree/master/nbdev/export.py#L434\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
"\n",
"> <code>notebook2script</code>(**`fname`**=*`None`*, **`silent`**=*`False`*, **`to_dict`**=*`False`*, **`bare`**=*`False`*)\n",
"\n",
Expand Down Expand Up @@ -1634,13 +1643,13 @@
"_str = '<h4 id=\"_A._add\" class=\"doc_header\"><code>_A._add</code><a href=\"__main__.py#L14\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\\n\\n> <code>_A._add</code>(**`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 = '''<h4 id=\"DocsTestClass.test_self\" class=\"doc_header\"><code>DocsTestClass.test_self</code><a href=\"https://github.com/fastai/nbdev/tree/master/nbdev/export.py#L450\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
"_str = '''<h4 id=\"DocsTestClass.test_self\" class=\"doc_header\"><code>DocsTestClass.test_self</code><a href=\"https://github.com/fastai/nbdev/tree/master/nbdev/export.py#L454\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
"\n",
"> <code>DocsTestClass.test_self</code>(**`cls`**, **`arg`**)\n",
"\n",
"'''\n",
"test_eq(show_doc(DocsTestClass.test_self, disp=False), _str)\n",
"_str = '''<h4 id=\"DocsTestClass.test_cls\" class=\"doc_header\"><code>DocsTestClass.test_cls</code><a href=\"https://github.com/fastai/nbdev/tree/master/nbdev/export.py#L452\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
"_str = '''<h4 id=\"DocsTestClass.test_cls\" class=\"doc_header\"><code>DocsTestClass.test_cls</code><a href=\"https://github.com/fastai/nbdev/tree/master/nbdev/export.py#L456\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
"\n",
"> <code>DocsTestClass.test_cls</code>(**`arg`**)\n",
"\n",
Expand Down Expand Up @@ -1708,8 +1717,8 @@
" )->str: # it's a return string\n",
" return d\n",
"\n",
"tst_str = '''<h2 id=\"A\" class=\"doc_header\"><code>class</code> <code>A</code><a href=\"\" class=\"source_link\" style=\"float:right\">[source]</a></h2>\\n\\n> <code>A</code>(**`a`**:`int`=*`2`*, **`b`**:`Sequence`\\\\[`int`\\\\]=*`(1, 2, 3)`*)\\n\\nTest dataclass'''\n",
"assert tst_str == show_doc(A, disp=False)"
"tst_str = '''<h2 id=\"A\" class=\"doc_header\"><code>class</code> <code>A</code><a href=\"\" class=\"source_link\" style=\"float:right\">[source]</a></h2>\\n\\n> <code>A</code>(**`a`**:`int`=*`2`*, **`b`**:`Sequence[int]`=*`(1, 2, 3)`*)\\n\\nTest dataclass'''\n",
"test_eq(show_doc(A, disp=False), tst_str)"
]
},
{
Expand Down Expand Up @@ -1763,7 +1772,7 @@
{
"data": {
"text/markdown": [
"<h2 id=\"DocsTestClass\" class=\"doc_header\"><code>class</code> <code>DocsTestClass</code><a href=\"https://github.com/fastai/nbdev/tree/master/nbdev/export.py#L446\" class=\"source_link\" style=\"float:right\">[source]</a></h2>\n",
"<h2 id=\"DocsTestClass\" class=\"doc_header\"><code>class</code> <code>DocsTestClass</code><a href=\"https://github.com/fastai/nbdev/tree/master/nbdev/export.py#L450\" class=\"source_link\" style=\"float:right\">[source]</a></h2>\n",
"\n",
"> <code>DocsTestClass</code>()\n",
"\n",
Expand All @@ -1790,7 +1799,7 @@
{
"data": {
"text/markdown": [
"<h4 id=\"DocsTestClass.test\" class=\"doc_header\"><code>DocsTestClass.test</code><a href=\"https://github.com/fastai/nbdev/tree/master/nbdev/export.py#L448\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
"<h4 id=\"DocsTestClass.test\" class=\"doc_header\"><code>DocsTestClass.test</code><a href=\"https://github.com/fastai/nbdev/tree/master/nbdev/export.py#L452\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
"\n",
"> <code>DocsTestClass.test</code>()\n",
"\n"
Expand Down
82 changes: 29 additions & 53 deletions nbs/sync.html
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ <h2 id="Finding-the-way-back-to-notebooks">Finding the way back to notebooks<a c


<div class="output_markdown rendered_html output_subarea ">
<h4 id="get_name" class="doc_header"><code>get_name</code><a href="https://github.com/fastai/nbdev/tree/master/nbdev/sync.py#L20" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>get_name</code>(<strong><code>obj</code></strong>)</p>
<h4 id="get_name" class="doc_header"><code>get_name</code><a href="https://github.com/fastai/nbdev/tree/master/nbdev/sync.py#L20" class="source_link" style="float:right">[source]</a></h4>
<blockquote>
<p><code>get_name</code>(<strong><code>obj</code></strong>)</p>
</blockquote>
<p>Get the name of <code>obj</code></p>

Expand Down Expand Up @@ -134,7 +136,9 @@ <h4 id="get_name" class="doc_header"><code>get_name</code><a href="https://githu


<div class="output_markdown rendered_html output_subarea ">
<h4 id="qual_name" class="doc_header"><code>qual_name</code><a href="https://github.com/fastai/nbdev/tree/master/nbdev/sync.py#L29" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>qual_name</code>(<strong><code>obj</code></strong>)</p>
<h4 id="qual_name" class="doc_header"><code>qual_name</code><a href="https://github.com/fastai/nbdev/tree/master/nbdev/sync.py#L29" class="source_link" style="float:right">[source]</a></h4>
<blockquote>
<p><code>qual_name</code>(<strong><code>obj</code></strong>)</p>
</blockquote>
<p>Get the qualified name of <code>obj</code></p>

Expand Down Expand Up @@ -190,7 +194,9 @@ <h4 id="qual_name" class="doc_header"><code>qual_name</code><a href="https://git


<div class="output_markdown rendered_html output_subarea ">
<h4 id="source_nb" class="doc_header"><code>source_nb</code><a href="https://github.com/fastai/nbdev/tree/master/nbdev/sync.py#L36" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>source_nb</code>(<strong><code>func</code></strong>, <strong><code>is_name</code></strong>=<em><code>None</code></em>, <strong><code>return_all</code></strong>=<em><code>False</code></em>, <strong><code>mod</code></strong>=<em><code>None</code></em>)</p>
<h4 id="source_nb" class="doc_header"><code>source_nb</code><a href="https://github.com/fastai/nbdev/tree/master/nbdev/sync.py#L36" class="source_link" style="float:right">[source]</a></h4>
<blockquote>
<p><code>source_nb</code>(<strong><code>func</code></strong>, <strong><code>is_name</code></strong>=<em><code>None</code></em>, <strong><code>return_all</code></strong>=<em><code>False</code></em>, <strong><code>mod</code></strong>=<em><code>None</code></em>)</p>
</blockquote>
<p>Return the name of the notebook where <code>func</code> was defined</p>

Expand Down Expand Up @@ -278,7 +284,9 @@ <h2 id="Reading-the-library">Reading the library<a class="anchor-link" href="#Re


<div class="output_markdown rendered_html output_subarea ">
<h4 id="relimport2name" class="doc_header"><code>relimport2name</code><a href="https://github.com/fastai/nbdev/tree/master/nbdev/sync.py#L69" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>relimport2name</code>(<strong><code>name</code></strong>, <strong><code>mod_name</code></strong>)</p>
<h4 id="relimport2name" class="doc_header"><code>relimport2name</code><a href="https://github.com/fastai/nbdev/tree/master/nbdev/sync.py#L69" class="source_link" style="float:right">[source]</a></h4>
<blockquote>
<p><code>relimport2name</code>(<strong><code>name</code></strong>, <strong><code>mod_name</code></strong>)</p>
</blockquote>
<p>Unwarps a relative import in <code>name</code> according to <code>mod_name</code></p>

Expand Down Expand Up @@ -368,32 +376,15 @@ <h4 id="relimport2name" class="doc_header"><code>relimport2name</code><a href="h


<div class="output_markdown rendered_html output_subarea ">
<h4 id="nbdev_update_lib" class="doc_header"><code>nbdev_update_lib</code><a href="https://github.com/fastai/nbdev/tree/master/nbdev/sync.py#L122" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>nbdev_update_lib</code>(<strong><code>fname</code></strong>:<code>str</code>=<em><code>None</code></em>, <strong><code>silent</code></strong>:<code>bool_arg</code>=<em><code>False</code></em>)</p>
<h4 id="nbdev_update_lib" class="doc_header"><code>nbdev_update_lib</code><a href="https://github.com/fastai/nbdev/tree/master/nbdev/sync.py#L122" class="source_link" style="float:right">[source]</a></h4>
<blockquote>
<p><code>nbdev_update_lib</code>(<strong><code>fname</code></strong>:<code>str</code>=<em><code>None</code></em>, <strong><code>silent</code></strong>:<code>bool_arg</code>=<em><code>False</code></em>)</p>
</blockquote>
<p>Propagates any change in the modules matching <code>fname</code> to the notebooks that created them</p>
<table>
<thead><tr>
<th></th>
<th>Type</th>
<th>Default</th>
<th>Details</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong><code>fname</code></strong></td>
<td><code>str</code></td>
<td><code>None</code></td>
<td>A python filename or glob to convert</td>
</tr>
<tr>
<td><strong><code>silent</code></strong></td>
<td><code>bool_arg</code></td>
<td><code>False</code></td>
<td>Dont print results</td>
</tr>
</tbody>
</table>
<p>||Type|Default|Details|
|---|---|---|---|
|<strong><code>fname</code></strong>|<code>str</code>|<code>None</code>|A python filename or glob to convert|
|<strong><code>silent</code></strong>|<code>bool_arg</code>|<code>False</code>|Dont print results|</p>

</div>

Expand Down Expand Up @@ -451,7 +442,9 @@ <h2 id="Diff-&amp;-trust-notebooks">Diff &amp; trust notebooks<a class="anchor-l


<div class="output_markdown rendered_html output_subarea ">
<h4 id="nbdev_diff_nbs" class="doc_header"><code>nbdev_diff_nbs</code><a href="https://github.com/fastai/nbdev/tree/master/nbdev/sync.py#L142" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>nbdev_diff_nbs</code>()</p>
<h4 id="nbdev_diff_nbs" class="doc_header"><code>nbdev_diff_nbs</code><a href="https://github.com/fastai/nbdev/tree/master/nbdev/sync.py#L142" class="source_link" style="float:right">[source]</a></h4>
<blockquote>
<p><code>nbdev_diff_nbs</code>()</p>
</blockquote>
<p>Prints the diff between an export of the library in notebooks and the actual modules</p>

Expand Down Expand Up @@ -507,32 +500,15 @@ <h4 id="nbdev_diff_nbs" class="doc_header"><code>nbdev_diff_nbs</code><a href="h


<div class="output_markdown rendered_html output_subarea ">
<h4 id="nbdev_trust_nbs" class="doc_header"><code>nbdev_trust_nbs</code><a href="https://github.com/fastai/nbdev/tree/master/nbdev/sync.py#L159" class="source_link" style="float:right">[source]</a></h4><blockquote><p><code>nbdev_trust_nbs</code>(<strong><code>fname</code></strong>:<code>str</code>=<em><code>None</code></em>, <strong><code>force_all</code></strong>:<code>bool</code>=<em><code>False</code></em>)</p>
<h4 id="nbdev_trust_nbs" class="doc_header"><code>nbdev_trust_nbs</code><a href="https://github.com/fastai/nbdev/tree/master/nbdev/sync.py#L159" class="source_link" style="float:right">[source]</a></h4>
<blockquote>
<p><code>nbdev_trust_nbs</code>(<strong><code>fname</code></strong>:<code>str</code>=<em><code>None</code></em>, <strong><code>force_all</code></strong>:<code>bool</code>=<em><code>False</code></em>)</p>
</blockquote>
<p>Trust notebooks matching <code>fname</code></p>
<table>
<thead><tr>
<th></th>
<th>Type</th>
<th>Default</th>
<th>Details</th>
</tr>
</thead>
<tbody>
<tr>
<td><strong><code>fname</code></strong></td>
<td><code>str</code></td>
<td><code>None</code></td>
<td>A notebook name or glob to convert</td>
</tr>
<tr>
<td><strong><code>force_all</code></strong></td>
<td><code>bool</code></td>
<td><code>False</code></td>
<td>Trust even notebooks that havent changed</td>
</tr>
</tbody>
</table>
<p>||Type|Default|Details|
|---|---|---|---|
|<strong><code>fname</code></strong>|<code>str</code>|<code>None</code>|A notebook name or glob to convert|
|<strong><code>force_all</code></strong>|<code>bool</code>|<code>False</code>|Trust even notebooks that havent changed|</p>

</div>

Expand Down