Skip to content

Commit

Permalink
Merge pull request #1482 from AnswerDotAI/syms
Browse files Browse the repository at this point in the history
Change symbols dict logic to prioritize builtins
  • Loading branch information
jph00 authored Dec 28, 2024
2 parents 1e47cbd + 13724ae commit 0f66519
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 13 deletions.
9 changes: 6 additions & 3 deletions nbdev/doclinks.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,12 @@ def _build_lookup_table(strip_libs=None, incl_libs=None, skip_mods=None):
for m in strip_libs:
if m in entries:
_d = entries[m]
stripped = {remove_prefix(k,f"{mod}."):v
for mod,dets in _d['syms'].items() if mod not in skip_mods
for k,v in dets.items()}
stripped = {}
for mod, dets in _d['syms'].items():
if mod not in skip_mods:
for k,v in dets.items():
k = remove_prefix(k,f"{mod}.")
if k not in stripped: stripped[k] = v
py_syms = merge(stripped, py_syms)
return entries,py_syms

Expand Down
100 changes: 90 additions & 10 deletions nbs/api/05_doclinks.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -627,9 +627,12 @@
" for m in strip_libs:\n",
" if m in entries:\n",
" _d = entries[m]\n",
" stripped = {remove_prefix(k,f\"{mod}.\"):v\n",
" for mod,dets in _d['syms'].items() if mod not in skip_mods\n",
" for k,v in dets.items()}\n",
" stripped = {}\n",
" for mod, dets in _d['syms'].items():\n",
" if mod not in skip_mods:\n",
" for k,v in dets.items():\n",
" k = remove_prefix(k,f\"{mod}.\")\n",
" if k not in stripped: stripped[k] = v\n",
" py_syms = merge(stripped, py_syms)\n",
" return entries,py_syms"
]
Expand Down Expand Up @@ -804,7 +807,7 @@
"text/markdown": [
"---\n",
"\n",
"[source](https://github.com/AnswerDotAI/nbdev/blob/master/nbdev/doclinks.py#L266){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/AnswerDotAI/nbdev/blob/master/nbdev/doclinks.py#L269){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"### NbdevLookup.doc\n",
"\n",
Expand All @@ -815,7 +818,7 @@
"text/plain": [
"---\n",
"\n",
"[source](https://github.com/AnswerDotAI/nbdev/blob/master/nbdev/doclinks.py#L266){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/AnswerDotAI/nbdev/blob/master/nbdev/doclinks.py#L269){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"### NbdevLookup.doc\n",
"\n",
Expand Down Expand Up @@ -906,7 +909,7 @@
"text/markdown": [
"---\n",
"\n",
"[source](https://github.com/AnswerDotAI/nbdev/blob/master/nbdev/doclinks.py#L271){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/AnswerDotAI/nbdev/blob/master/nbdev/doclinks.py#L274){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"### NbdevLookup.code\n",
"\n",
Expand All @@ -917,7 +920,7 @@
"text/plain": [
"---\n",
"\n",
"[source](https://github.com/AnswerDotAI/nbdev/blob/master/nbdev/doclinks.py#L271){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/AnswerDotAI/nbdev/blob/master/nbdev/doclinks.py#L274){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"### NbdevLookup.code\n",
"\n",
Expand Down Expand Up @@ -965,7 +968,7 @@
"text/markdown": [
"---\n",
"\n",
"[source](https://github.com/AnswerDotAI/nbdev/blob/master/nbdev/doclinks.py#L289){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/AnswerDotAI/nbdev/blob/master/nbdev/doclinks.py#L292){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"### NbdevLookup.linkify\n",
"\n",
Expand All @@ -974,7 +977,7 @@
"text/plain": [
"---\n",
"\n",
"[source](https://github.com/AnswerDotAI/nbdev/blob/master/nbdev/doclinks.py#L289){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"[source](https://github.com/AnswerDotAI/nbdev/blob/master/nbdev/doclinks.py#L292){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
"\n",
"### NbdevLookup.linkify\n",
"\n",
Expand Down Expand Up @@ -1075,7 +1078,7 @@
{
"data": {
"text/plain": [
"'[`str.split`](https://docs.python.org/3/library/stdtypes.html#str.split) and [`str`](https://docs.python.org/3/library/locale.html#locale.str)'"
"'[`str.split`](https://docs.python.org/3/library/stdtypes.html#str.split) and [`str`](https://docs.python.org/3/library/stdtypes.html#str)'"
]
},
"execution_count": null,
Expand All @@ -1089,6 +1092,83 @@
"NbdevLookup('nbdev_stdlib').linkify(md)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"When there is a conflict, the linkification will apply in the order of the stripped libraries and then by alphabetical order. For example, `enumerate` is both a builtin and a function in the threading module. However, since builtins comes first alphabetically, it will take priority"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'[`enumerate`](https://docs.python.org/3/library/functions.html#enumerate), [`builtins.enumerate`](https://docs.python.org/3/library/functions.html#enumerate) and [`threading.enumerate`](https://docs.python.org/3/library/threading.html#threading.enumerate)'"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"md = \"`enumerate`, `builtins.enumerate` and `threading.enumerate`\"\n",
"NbdevLookup(('nbdev_stdlib')).linkify(md)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can also take the `find()` function as another instance, it exists as a standard library and in numpy. Therefore, depending on the order of stripped libraries we pass, `find()` will link to either numpy or standard library."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'[`find()`](https://numpy.org/doc/stable/reference/generated/numpy.char.find.html#numpy.char.find)'"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"md = \"`find()`\"\n",
"NbdevLookup(('nbdev_numpy','nbdev_stdlib')).linkify(md)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'[`find()`](https://docs.python.org/3/library/gettext.html#gettext.find)'"
]
},
"execution_count": null,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"md = \"`find()`\"\n",
"NbdevLookup(('nbdev_stdlib','nbdev_numpy')).linkify(md)"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand Down

0 comments on commit 0f66519

Please sign in to comment.