Skip to content

Commit b4a4107

Browse files
committed
fixes #749
1 parent 19b4b8a commit b4a4107

File tree

2 files changed

+52
-9
lines changed

2 files changed

+52
-9
lines changed

fastcore/docments.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,16 @@ def _get_property_name(p):
129129
return p.fget.func.__qualname__ if hasattr(p.fget, 'func') else p.fget.__qualname__
130130
else: return next(iter(re.findall(r'\'(.*)\'', str(p)))).split('.')[-1]
131131

132-
# %% ../nbs/04_docments.ipynb #f5efac13
132+
# %% ../nbs/04_docments.ipynb #da0465e3
133133
def get_name(obj):
134134
"Get the name of `obj`"
135+
if isinstance(obj, partial):
136+
nm = get_name(obj.func)
137+
args = [repr(a) for a in obj.args] + [f'{k}={repr(v)}' for k,v in obj.keywords.items()]
138+
return f"{nm}[partial: {', '.join(args)}]"
135139
if hasattr(obj, '__name__'): return obj.__name__
136140
elif getattr(obj, '_name', False): return obj._name
137-
elif hasattr(obj,'__origin__'): return str(obj.__origin__).split('.')[-1] #for types
141+
elif hasattr(obj,'__origin__'): return str(obj.__origin__).split('.')[-1]
138142
elif type(obj)==property: return _get_property_name(obj)
139143
else: return str(obj).split('.')[-1]
140144

@@ -346,7 +350,7 @@ def __str__(self):
346350
if curr: lines.append(', '.join(curr))
347351
body = '\n '.join(lines)
348352
docstr = f' "{self.obj.__doc__}"' if self.docstring and self.obj.__doc__ else ''
349-
return f"def {self.obj.__name__}(\n {body}\n{self._ret_str}\n{docstr}"
353+
return f"def {get_name(self.obj)}(\n {body}\n{self._ret_str}\n{docstr}"
350354

351355
__repr__ = __str__
352356
def _repr_markdown_(self): return f"```python\n{self}\n```"

nbs/04_docments.ipynb

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -399,16 +399,20 @@
399399
{
400400
"cell_type": "code",
401401
"execution_count": null,
402-
"id": "f5efac13",
402+
"id": "da0465e3",
403403
"metadata": {},
404404
"outputs": [],
405405
"source": [
406406
"#| export\n",
407407
"def get_name(obj):\n",
408408
" \"Get the name of `obj`\"\n",
409+
" if isinstance(obj, partial):\n",
410+
" nm = get_name(obj.func)\n",
411+
" args = [repr(a) for a in obj.args] + [f'{k}={repr(v)}' for k,v in obj.keywords.items()]\n",
412+
" return f\"{nm}[partial: {', '.join(args)}]\"\n",
409413
" if hasattr(obj, '__name__'): return obj.__name__\n",
410414
" elif getattr(obj, '_name', False): return obj._name\n",
411-
" elif hasattr(obj,'__origin__'): return str(obj.__origin__).split('.')[-1] #for types\n",
415+
" elif hasattr(obj,'__origin__'): return str(obj.__origin__).split('.')[-1]\n",
412416
" elif type(obj)==property: return _get_property_name(obj)\n",
413417
" else: return str(obj).split('.')[-1]"
414418
]
@@ -1843,7 +1847,7 @@
18431847
"| **Returns** | **int** | | |"
18441848
],
18451849
"text/plain": [
1846-
"DocmentTbl(obj=<function _f at 0x131f0efc0>, dm={'a': {'docment': 'description of param a', 'anno': <class 'inspect._empty'>, 'default': <class 'inspect._empty'>}, 'b': {'docment': 'description of param b', 'anno': <class 'bool'>, 'default': True}, 'c': {'docment': <class 'inspect._empty'>, 'anno': <class 'str'>, 'default': None}, 'return': {'docment': <class 'inspect._empty'>, 'anno': <class 'int'>, 'default': <class 'inspect._empty'>}}, verbose=True, returns=True, params=['a', 'b', 'c'])"
1850+
"DocmentTbl(obj=<function _f at 0x135058540>, dm={'a': {'docment': 'description of param a', 'anno': <class 'inspect._empty'>, 'default': <class 'inspect._empty'>}, 'b': {'docment': 'description of param b', 'anno': <class 'bool'>, 'default': True}, 'c': {'docment': <class 'inspect._empty'>, 'anno': <class 'str'>, 'default': None}, 'return': {'docment': <class 'inspect._empty'>, 'anno': <class 'int'>, 'default': <class 'inspect._empty'>}}, verbose=True, returns=True, params=['a', 'b', 'c'])"
18471851
]
18481852
},
18491853
"execution_count": null,
@@ -1921,7 +1925,7 @@
19211925
"| **Returns** | **str** | | **Result of doing it** |"
19221926
],
19231927
"text/plain": [
1924-
"DocmentTbl(obj=<function _f at 0x131f0f060>, dm={'a': {'docment': <class 'inspect._empty'>, 'anno': <class 'inspect._empty'>, 'default': <class 'inspect._empty'>}, 'b': {'docment': 'param b', 'anno': <class 'int'>, 'default': <class 'inspect._empty'>}, 'c': {'docment': 'param c', 'anno': <class 'str'>, 'default': 'foo'}, 'return': {'docment': 'Result of doing it', 'anno': <class 'str'>, 'default': <class 'inspect._empty'>}}, verbose=True, returns=True, params=['a', 'b', 'c'])"
1928+
"DocmentTbl(obj=<function _f at 0x134ed3a60>, dm={'a': {'docment': <class 'inspect._empty'>, 'anno': <class 'inspect._empty'>, 'default': <class 'inspect._empty'>}, 'b': {'docment': 'param b', 'anno': <class 'int'>, 'default': <class 'inspect._empty'>}, 'c': {'docment': 'param c', 'anno': <class 'str'>, 'default': 'foo'}, 'return': {'docment': 'Result of doing it', 'anno': <class 'str'>, 'default': <class 'inspect._empty'>}}, verbose=True, returns=True, params=['a', 'b', 'c'])"
19251929
]
19261930
},
19271931
"execution_count": null,
@@ -2014,7 +2018,7 @@
20142018
"| d | bool | True | description of param d |"
20152019
],
20162020
"text/plain": [
2017-
"DocmentTbl(obj=<function _Test.foo at 0x131e5c040>, dm={'c': {'docment': 'description of param c', 'anno': <class 'int'>, 'default': <class 'inspect._empty'>}, 'd': {'docment': 'description of param d', 'anno': <class 'bool'>, 'default': True}, 'return': {'docment': <class 'inspect._empty'>, 'anno': <class 'inspect._empty'>, 'default': <class 'inspect._empty'>}}, verbose=True, returns=True, params=['self', 'c', 'd'])"
2021+
"DocmentTbl(obj=<function _Test.foo at 0x13517e8e0>, dm={'c': {'docment': 'description of param c', 'anno': <class 'int'>, 'default': <class 'inspect._empty'>}, 'd': {'docment': 'description of param d', 'anno': <class 'bool'>, 'default': True}, 'return': {'docment': <class 'inspect._empty'>, 'anno': <class 'inspect._empty'>, 'default': <class 'inspect._empty'>}}, verbose=True, returns=True, params=['self', 'c', 'd'])"
20182022
]
20192023
},
20202024
"execution_count": null,
@@ -2132,7 +2136,7 @@
21322136
" if curr: lines.append(', '.join(curr))\n",
21332137
" body = '\\n '.join(lines)\n",
21342138
" docstr = f' \"{self.obj.__doc__}\"' if self.docstring and self.obj.__doc__ else ''\n",
2135-
" return f\"def {self.obj.__name__}(\\n {body}\\n{self._ret_str}\\n{docstr}\"\n",
2139+
" return f\"def {get_name(self.obj)}(\\n {body}\\n{self._ret_str}\\n{docstr}\"\n",
21362140
" \n",
21372141
" __repr__ = __str__\n",
21382142
" def _repr_markdown_(self): return f\"```python\\n{self}\\n```\""
@@ -2234,6 +2238,41 @@
22342238
"DocmentText(_g, maxline=80, docstring=False)"
22352239
]
22362240
},
2241+
{
2242+
"cell_type": "code",
2243+
"execution_count": null,
2244+
"id": "88807401",
2245+
"metadata": {},
2246+
"outputs": [
2247+
{
2248+
"data": {
2249+
"text/markdown": [
2250+
"```python\n",
2251+
"def _g[partial: 1](\n",
2252+
" b:int, cccccccccccccccccccc:int, ccccccccdccccccccccc:int,\n",
2253+
" cccccccccccecccccccc:int, cccccccfcccccccccc:int, ccccccccccccgccccc:int,\n",
2254+
" c:str='foo'\n",
2255+
")->str:\n",
2256+
"\n",
2257+
"```"
2258+
],
2259+
"text/plain": [
2260+
"def _g[partial: 1](\n",
2261+
" b:int, cccccccccccccccccccc:int, ccccccccdccccccccccc:int,\n",
2262+
" cccccccccccecccccccc:int, cccccccfcccccccccc:int, ccccccccccccgccccc:int,\n",
2263+
" c:str='foo'\n",
2264+
")->str:"
2265+
]
2266+
},
2267+
"execution_count": null,
2268+
"metadata": {},
2269+
"output_type": "execute_result"
2270+
}
2271+
],
2272+
"source": [
2273+
"DocmentText(partial(_g, 1), maxline=80, docstring=False)"
2274+
]
2275+
},
22372276
{
22382277
"cell_type": "code",
22392278
"execution_count": null,

0 commit comments

Comments
 (0)