Skip to content

Commit 7c550e8

Browse files
committed
empty
1 parent fc0681c commit 7c550e8

File tree

3 files changed

+90
-25
lines changed

3 files changed

+90
-25
lines changed

fastcore/_nbdev.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@
244244
"gather_attrs": "05_transform.ipynb",
245245
"gather_attr_names": "05_transform.ipynb",
246246
"Pipeline": "05_transform.ipynb",
247+
"empty": "06_docments.ipynb",
247248
"docments": "06_docments.ipynb",
248249
"test_sig": "07_meta.ipynb",
249250
"FixSigMeta": "07_meta.ipynb",

fastcore/docments.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
# AUTOGENERATED! DO NOT EDIT! File to edit: nbs/06_docments.ipynb (unless otherwise specified).
22

3-
__all__ = ['docments']
3+
4+
from __future__ import annotations
5+
6+
7+
__all__ = ['empty', 'docments']
48

59
# Cell
10+
#nbdev_comment from __future__ import annotations
611
from tokenize import tokenize,COMMENT
712
from ast import parse,FunctionDef
813
from io import BytesIO
@@ -37,6 +42,9 @@ def _param_locs(s, returns=True):
3742
if returns and defn.returns: res[defn.returns.lineno] = 'return'
3843
return res
3944

45+
# Cell
46+
empty = Parameter.empty
47+
4048
# Cell
4149
def _get_comment(line, arg, comments, parms):
4250
if line in comments: return comments[line].strip()
@@ -48,7 +56,7 @@ def _get_comment(line, arg, comments, parms):
4856
return dedent('\n'.join(reversed(res))) if res else None
4957

5058
def _get_full(anno, name, default, docs):
51-
if anno==Parameter.empty and default!=Parameter.empty: anno = type(default)
59+
if anno==empty and default!=empty: anno = type(default)
5260
return AttrDict(docment=docs.get(name), anno=anno, default=default)
5361

5462
# Cell
@@ -58,14 +66,14 @@ def docments(s, full=False, returns=True, eval_str=False):
5866
comments = {o.start[0]:_clean_comment(o.string) for o in _tokens(s) if o.type==COMMENT}
5967
parms = _param_locs(s, returns=returns)
6068
docs = {arg:_get_comment(line, arg, comments, parms) for line,arg in parms.items()}
61-
if not full: return docs
69+
if not full: return AttrDict(docs)
6270

6371
if isinstance(s,str): s = eval(s)
6472
sig = signature(s)
6573
res = {arg:_get_full(p.annotation, p.name, p.default, docs) for arg,p in sig.parameters.items()}
66-
if returns: res['return'] = _get_full(sig.return_annotation, 'return', Parameter.empty, docs)
74+
if returns: res['return'] = _get_full(sig.return_annotation, 'return', empty, docs)
6775
if eval_str:
6876
hints = type_hints(s)
6977
for k,v in res.items():
7078
if k in hints: v['anno'] = hints.get(k)
71-
return res
79+
return AttrDict(res)

nbs/06_docments.ipynb

Lines changed: 76 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
"outputs": [],
2626
"source": [
2727
"#export\n",
28+
"from __future__ import annotations\n",
2829
"from tokenize import tokenize,COMMENT\n",
2930
"from ast import parse,FunctionDef\n",
3031
"from io import BytesIO\n",
@@ -151,6 +152,16 @@
151152
" return res"
152153
]
153154
},
155+
{
156+
"cell_type": "code",
157+
"execution_count": null,
158+
"metadata": {},
159+
"outputs": [],
160+
"source": [
161+
"#export\n",
162+
"empty = Parameter.empty"
163+
]
164+
},
154165
{
155166
"cell_type": "code",
156167
"execution_count": null,
@@ -168,7 +179,7 @@
168179
" return dedent('\\n'.join(reversed(res))) if res else None\n",
169180
"\n",
170181
"def _get_full(anno, name, default, docs):\n",
171-
" if anno==Parameter.empty and default!=Parameter.empty: anno = type(default)\n",
182+
" if anno==empty and default!=empty: anno = type(default)\n",
172183
" return AttrDict(docment=docs.get(name), anno=anno, default=default)"
173184
]
174185
},
@@ -185,17 +196,17 @@
185196
" comments = {o.start[0]:_clean_comment(o.string) for o in _tokens(s) if o.type==COMMENT}\n",
186197
" parms = _param_locs(s, returns=returns)\n",
187198
" docs = {arg:_get_comment(line, arg, comments, parms) for line,arg in parms.items()}\n",
188-
" if not full: return docs\n",
199+
" if not full: return AttrDict(docs)\n",
189200
"\n",
190201
" if isinstance(s,str): s = eval(s)\n",
191202
" sig = signature(s)\n",
192203
" res = {arg:_get_full(p.annotation, p.name, p.default, docs) for arg,p in sig.parameters.items()}\n",
193-
" if returns: res['return'] = _get_full(sig.return_annotation, 'return', Parameter.empty, docs)\n",
204+
" if returns: res['return'] = _get_full(sig.return_annotation, 'return', empty, docs)\n",
194205
" if eval_str:\n",
195206
" hints = type_hints(s)\n",
196207
" for k,v in res.items():\n",
197208
" if k in hints: v['anno'] = hints.get(k)\n",
198-
" return res"
209+
" return AttrDict(res)"
199210
]
200211
},
201212
{
@@ -212,6 +223,13 @@
212223
"outputs": [
213224
{
214225
"data": {
226+
"text/markdown": [
227+
"```json\n",
228+
"{ 'a': 'the 1st number to add',\n",
229+
" 'b': 'the 2nd number to add',\n",
230+
" 'return': 'the result of adding `a` to `b`'}\n",
231+
"```"
232+
],
215233
"text/plain": [
216234
"{'a': 'the 1st number to add',\n",
217235
" 'b': 'the 2nd number to add',\n",
@@ -242,7 +260,7 @@
242260
{
243261
"data": {
244262
"text/plain": [
245-
"{'a': int, 'return': int}"
263+
"{'a': 'int', 'return': 'int'}"
246264
]
247265
},
248266
"execution_count": null,
@@ -269,14 +287,14 @@
269287
{
270288
"data": {
271289
"text/markdown": [
272-
"- docment: the 1st number to add\n",
273-
"- anno: <class 'int'>\n",
274-
"- default: <class 'inspect._empty'>"
290+
"```json\n",
291+
"{ 'anno': 'int',\n",
292+
" 'default': <class 'inspect._empty'>,\n",
293+
" 'docment': 'the 1st number to add'}\n",
294+
"```"
275295
],
276296
"text/plain": [
277-
"- docment: the 1st number to add\n",
278-
"- anno: <class 'int'>\n",
279-
"- default: <class 'inspect._empty'>"
297+
"{'docment': 'the 1st number to add', 'anno': 'int', 'default': inspect._empty}"
280298
]
281299
},
282300
"execution_count": null,
@@ -303,14 +321,14 @@
303321
{
304322
"data": {
305323
"text/markdown": [
306-
"- docment: the 1st number to add\n",
307-
"- anno: <class 'int'>\n",
308-
"- default: <class 'inspect._empty'>"
324+
"```json\n",
325+
"{ 'anno': <class 'int'>,\n",
326+
" 'default': <class 'inspect._empty'>,\n",
327+
" 'docment': 'the 1st number to add'}\n",
328+
"```"
309329
],
310330
"text/plain": [
311-
"- docment: the 1st number to add\n",
312-
"- anno: <class 'int'>\n",
313-
"- default: <class 'inspect._empty'>"
331+
"{'docment': 'the 1st number to add', 'anno': int, 'default': inspect._empty}"
314332
]
315333
},
316334
"execution_count": null,
@@ -353,6 +371,15 @@
353371
"outputs": [
354372
{
355373
"data": {
374+
"text/markdown": [
375+
"```json\n",
376+
"{ 'a': 'The first operand',\n",
377+
" 'b': 'This is the second of the operands to the *addition* operator.\\n'\n",
378+
" 'Note that passing a negative value here is the equivalent of the '\n",
379+
" '*subtraction* operator.',\n",
380+
" 'return': \"The result is calculated using Python's builtin `+` operator.\"}\n",
381+
"```"
382+
],
356383
"text/plain": [
357384
"{'a': 'The first operand',\n",
358385
" 'b': 'This is the second of the operands to the *addition* operator.\\nNote that passing a negative value here is the equivalent of the *subtraction* operator.',\n",
@@ -401,9 +428,38 @@
401428
"outputs": [
402429
{
403430
"data": {
431+
"text/markdown": [
432+
"```json\n",
433+
"{'a': 'First operand', 'b': '2nd operand', 'self': None}\n",
434+
"```"
435+
],
436+
"text/plain": [
437+
"{'self': None, 'a': 'First operand', 'b': '2nd operand'}"
438+
]
439+
},
440+
"execution_count": null,
441+
"metadata": {},
442+
"output_type": "execute_result"
443+
}
444+
],
445+
"source": [
446+
"docments(Adder)"
447+
]
448+
},
449+
{
450+
"cell_type": "code",
451+
"execution_count": null,
452+
"metadata": {},
453+
"outputs": [
454+
{
455+
"data": {
456+
"text/markdown": [
457+
"```json\n",
458+
"{'return': 'Integral result of addition operator', 'self': None}\n",
459+
"```"
460+
],
404461
"text/plain": [
405-
"({'self': None, 'a': 'First operand', 'b': '2nd operand'},\n",
406-
" {'self': None, 'return': 'Integral result of addition operator'})"
462+
"{'self': None, 'return': 'Integral result of addition operator'}"
407463
]
408464
},
409465
"execution_count": null,
@@ -412,7 +468,7 @@
412468
}
413469
],
414470
"source": [
415-
"docments(Adder),docments(Adder.calculate)"
471+
"docments(Adder.calculate)"
416472
]
417473
},
418474
{

0 commit comments

Comments
 (0)