Skip to content

Commit 0f66519

Browse files
authored
Merge pull request #1482 from AnswerDotAI/syms
Change symbols dict logic to prioritize builtins
2 parents 1e47cbd + 13724ae commit 0f66519

File tree

2 files changed

+96
-13
lines changed

2 files changed

+96
-13
lines changed

nbdev/doclinks.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,9 +244,12 @@ def _build_lookup_table(strip_libs=None, incl_libs=None, skip_mods=None):
244244
for m in strip_libs:
245245
if m in entries:
246246
_d = entries[m]
247-
stripped = {remove_prefix(k,f"{mod}."):v
248-
for mod,dets in _d['syms'].items() if mod not in skip_mods
249-
for k,v in dets.items()}
247+
stripped = {}
248+
for mod, dets in _d['syms'].items():
249+
if mod not in skip_mods:
250+
for k,v in dets.items():
251+
k = remove_prefix(k,f"{mod}.")
252+
if k not in stripped: stripped[k] = v
250253
py_syms = merge(stripped, py_syms)
251254
return entries,py_syms
252255

nbs/api/05_doclinks.ipynb

Lines changed: 90 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -627,9 +627,12 @@
627627
" for m in strip_libs:\n",
628628
" if m in entries:\n",
629629
" _d = entries[m]\n",
630-
" stripped = {remove_prefix(k,f\"{mod}.\"):v\n",
631-
" for mod,dets in _d['syms'].items() if mod not in skip_mods\n",
632-
" for k,v in dets.items()}\n",
630+
" stripped = {}\n",
631+
" for mod, dets in _d['syms'].items():\n",
632+
" if mod not in skip_mods:\n",
633+
" for k,v in dets.items():\n",
634+
" k = remove_prefix(k,f\"{mod}.\")\n",
635+
" if k not in stripped: stripped[k] = v\n",
633636
" py_syms = merge(stripped, py_syms)\n",
634637
" return entries,py_syms"
635638
]
@@ -804,7 +807,7 @@
804807
"text/markdown": [
805808
"---\n",
806809
"\n",
807-
"[source](https://github.com/AnswerDotAI/nbdev/blob/master/nbdev/doclinks.py#L266){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
810+
"[source](https://github.com/AnswerDotAI/nbdev/blob/master/nbdev/doclinks.py#L269){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
808811
"\n",
809812
"### NbdevLookup.doc\n",
810813
"\n",
@@ -815,7 +818,7 @@
815818
"text/plain": [
816819
"---\n",
817820
"\n",
818-
"[source](https://github.com/AnswerDotAI/nbdev/blob/master/nbdev/doclinks.py#L266){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
821+
"[source](https://github.com/AnswerDotAI/nbdev/blob/master/nbdev/doclinks.py#L269){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
819822
"\n",
820823
"### NbdevLookup.doc\n",
821824
"\n",
@@ -906,7 +909,7 @@
906909
"text/markdown": [
907910
"---\n",
908911
"\n",
909-
"[source](https://github.com/AnswerDotAI/nbdev/blob/master/nbdev/doclinks.py#L271){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
912+
"[source](https://github.com/AnswerDotAI/nbdev/blob/master/nbdev/doclinks.py#L274){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
910913
"\n",
911914
"### NbdevLookup.code\n",
912915
"\n",
@@ -917,7 +920,7 @@
917920
"text/plain": [
918921
"---\n",
919922
"\n",
920-
"[source](https://github.com/AnswerDotAI/nbdev/blob/master/nbdev/doclinks.py#L271){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
923+
"[source](https://github.com/AnswerDotAI/nbdev/blob/master/nbdev/doclinks.py#L274){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
921924
"\n",
922925
"### NbdevLookup.code\n",
923926
"\n",
@@ -965,7 +968,7 @@
965968
"text/markdown": [
966969
"---\n",
967970
"\n",
968-
"[source](https://github.com/AnswerDotAI/nbdev/blob/master/nbdev/doclinks.py#L289){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
971+
"[source](https://github.com/AnswerDotAI/nbdev/blob/master/nbdev/doclinks.py#L292){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
969972
"\n",
970973
"### NbdevLookup.linkify\n",
971974
"\n",
@@ -974,7 +977,7 @@
974977
"text/plain": [
975978
"---\n",
976979
"\n",
977-
"[source](https://github.com/AnswerDotAI/nbdev/blob/master/nbdev/doclinks.py#L289){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
980+
"[source](https://github.com/AnswerDotAI/nbdev/blob/master/nbdev/doclinks.py#L292){target=\"_blank\" style=\"float:right; font-size:smaller\"}\n",
978981
"\n",
979982
"### NbdevLookup.linkify\n",
980983
"\n",
@@ -1075,7 +1078,7 @@
10751078
{
10761079
"data": {
10771080
"text/plain": [
1078-
"'[`str.split`](https://docs.python.org/3/library/stdtypes.html#str.split) and [`str`](https://docs.python.org/3/library/locale.html#locale.str)'"
1081+
"'[`str.split`](https://docs.python.org/3/library/stdtypes.html#str.split) and [`str`](https://docs.python.org/3/library/stdtypes.html#str)'"
10791082
]
10801083
},
10811084
"execution_count": null,
@@ -1089,6 +1092,83 @@
10891092
"NbdevLookup('nbdev_stdlib').linkify(md)"
10901093
]
10911094
},
1095+
{
1096+
"cell_type": "markdown",
1097+
"metadata": {},
1098+
"source": [
1099+
"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"
1100+
]
1101+
},
1102+
{
1103+
"cell_type": "code",
1104+
"execution_count": null,
1105+
"metadata": {},
1106+
"outputs": [
1107+
{
1108+
"data": {
1109+
"text/plain": [
1110+
"'[`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)'"
1111+
]
1112+
},
1113+
"execution_count": null,
1114+
"metadata": {},
1115+
"output_type": "execute_result"
1116+
}
1117+
],
1118+
"source": [
1119+
"md = \"`enumerate`, `builtins.enumerate` and `threading.enumerate`\"\n",
1120+
"NbdevLookup(('nbdev_stdlib')).linkify(md)"
1121+
]
1122+
},
1123+
{
1124+
"cell_type": "markdown",
1125+
"metadata": {},
1126+
"source": [
1127+
"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."
1128+
]
1129+
},
1130+
{
1131+
"cell_type": "code",
1132+
"execution_count": null,
1133+
"metadata": {},
1134+
"outputs": [
1135+
{
1136+
"data": {
1137+
"text/plain": [
1138+
"'[`find()`](https://numpy.org/doc/stable/reference/generated/numpy.char.find.html#numpy.char.find)'"
1139+
]
1140+
},
1141+
"execution_count": null,
1142+
"metadata": {},
1143+
"output_type": "execute_result"
1144+
}
1145+
],
1146+
"source": [
1147+
"md = \"`find()`\"\n",
1148+
"NbdevLookup(('nbdev_numpy','nbdev_stdlib')).linkify(md)"
1149+
]
1150+
},
1151+
{
1152+
"cell_type": "code",
1153+
"execution_count": null,
1154+
"metadata": {},
1155+
"outputs": [
1156+
{
1157+
"data": {
1158+
"text/plain": [
1159+
"'[`find()`](https://docs.python.org/3/library/gettext.html#gettext.find)'"
1160+
]
1161+
},
1162+
"execution_count": null,
1163+
"metadata": {},
1164+
"output_type": "execute_result"
1165+
}
1166+
],
1167+
"source": [
1168+
"md = \"`find()`\"\n",
1169+
"NbdevLookup(('nbdev_stdlib','nbdev_numpy')).linkify(md)"
1170+
]
1171+
},
10921172
{
10931173
"cell_type": "markdown",
10941174
"metadata": {},

0 commit comments

Comments
 (0)