|
159 | 159 | " if isinstance(sym, str): return sym\n", |
160 | 160 | " res = getdoc(sym)\n", |
161 | 161 | " if not res and isclass(sym): res = getdoc(sym.__init__)\n", |
| 162 | + " if not res and callable(sym) and not isclass(sym): res = getdoc(sym.__call__)\n", |
162 | 163 | " return res or \"\"" |
163 | 164 | ] |
164 | 165 | }, |
|
414 | 415 | " elif getattr(obj, '_name', False): return obj._name\n", |
415 | 416 | " elif hasattr(obj,'__origin__'): return str(obj.__origin__).split('.')[-1]\n", |
416 | 417 | " elif type(obj)==property: return _get_property_name(obj)\n", |
417 | | - " else: return str(obj).split('.')[-1]" |
| 418 | + " elif callable(obj): return type(obj).__name__\n", |
| 419 | + " else: return str(obj).split('.')[-1]\n" |
418 | 420 | ] |
419 | 421 | }, |
420 | 422 | { |
|
2187 | 2189 | " def params(self): return [(self._fmt_param(k,v), v.get('docment','')) for k,v in self.dm.items() if k != 'return']\n", |
2188 | 2190 | "\n", |
2189 | 2191 | " def __str__(self):\n", |
2190 | | - " prefix = 'async def' if inspect.iscoroutinefunction(self.obj) else 'def'\n", |
2191 | | - " if (sig := _clean_text_sig(self.obj)) and not self.params: sig_str = f\"{prefix} {sig}\"\n", |
2192 | | - " else: sig_str = _fmt_sig(get_name(self.obj), self.params, self._ret_str, self.maxline, prefix=prefix)\n", |
2193 | | - " docstr = f' \"{self.obj.__doc__}\"' if self.docstring and self.obj.__doc__ else ''\n", |
| 2192 | + " o = self.obj\n", |
| 2193 | + " is_inst = callable(o) and not (isfunction(o) or isclass(o) or inspect.isbuiltin(o) or ismethod(o))\n", |
| 2194 | + " prefix = 'async def' if inspect.iscoroutinefunction(o) else 'def'\n", |
| 2195 | + " nm = f'{type(o).__name__}.__call__' if is_inst else get_name(o)\n", |
| 2196 | + " if (sig := _clean_text_sig(o)) and not self.params: sig_str = f\"{prefix} {sig}\"\n", |
| 2197 | + " else: sig_str = _fmt_sig(nm, self.params, self._ret_str, self.maxline, prefix=prefix)\n", |
| 2198 | + " doc = getattr(o.__call__, '__doc__', None) if is_inst else o.__doc__\n", |
| 2199 | + " docstr = f' \"{doc}\"' if self.docstring and doc else ''\n", |
2194 | 2200 | " return f\"{sig_str}\\n{docstr}\"\n", |
2195 | 2201 | " \n", |
2196 | 2202 | " __repr__ = __str__\n", |
|
2303 | 2309 | "data": { |
2304 | 2310 | "text/markdown": [ |
2305 | 2311 | "```python\n", |
2306 | | - "def _g[partial: 1](\n", |
| 2312 | + "def partial.__call__(\n", |
2307 | 2313 | " b:int, cccccccccccccccccccc:int, ccccccccdccccccccccc:int,\n", |
2308 | 2314 | " cccccccccccecccccccc:int, cccccccfcccccccccc:int, ccccccccccccgccccc:int,\n", |
2309 | 2315 | " c:str='foo'\n", |
|
2312 | 2318 | "```" |
2313 | 2319 | ], |
2314 | 2320 | "text/plain": [ |
2315 | | - "def _g[partial: 1](\n", |
| 2321 | + "def partial.__call__(\n", |
2316 | 2322 | " b:int, cccccccccccccccccccc:int, ccccccccdccccccccccc:int,\n", |
2317 | 2323 | " cccccccccccecccccccc:int, cccccccfcccccccccc:int, ccccccccccccgccccc:int,\n", |
2318 | 2324 | " c:str='foo'\n", |
|
2598 | 2604 | "):\n", |
2599 | 2605 | "\n", |
2600 | 2606 | "\n", |
2601 | | - "```" |
| 2607 | + "```\n", |
| 2608 | + "\n", |
| 2609 | + "*Call self as a function.*" |
2602 | 2610 | ], |
2603 | 2611 | "text/plain": [ |
2604 | | - "```python\n", |
2605 | | - "\n", |
2606 | 2612 | "def g(\n", |
2607 | 2613 | " b:int | str, # bb\n", |
2608 | 2614 | " a:int=0, # aa\n", |
2609 | 2615 | "):\n", |
2610 | | - "\n", |
2611 | | - "\n", |
2612 | | - "```" |
| 2616 | + "\"\"\"\n", |
| 2617 | + "Call self as a function.\n", |
| 2618 | + "\"\"\"" |
2613 | 2619 | ] |
2614 | 2620 | }, |
2615 | 2621 | "execution_count": null, |
|
2644 | 2650 | "*Add `a` to `b`*" |
2645 | 2651 | ], |
2646 | 2652 | "text/plain": [ |
2647 | | - "```python\n", |
2648 | | - "\n", |
2649 | 2653 | "async def add_async(\n", |
2650 | 2654 | " a:int, # The first operand\n", |
2651 | 2655 | " b:int, # This is the second of the operands to the *addition* operator.\n", |
2652 | 2656 | "Note that passing a negative value here is the equivalent of the *subtraction* operator.\n", |
2653 | 2657 | ")->int: # The result is calculated using Python's builtin `+` operator.\n", |
2654 | | - "\n", |
2655 | | - "\n", |
2656 | | - "```\n", |
2657 | | - "\n", |
2658 | | - "*Add `a` to `b`*" |
| 2658 | + "\"\"\"\n", |
| 2659 | + "Add `a` to `b`\n", |
| 2660 | + "\"\"\"" |
2659 | 2661 | ] |
2660 | 2662 | }, |
2661 | 2663 | "execution_count": null, |
|
2689 | 2691 | "it is returned instead of raising StopIteration." |
2690 | 2692 | ], |
2691 | 2693 | "text/plain": [ |
2692 | | - "```python\n", |
2693 | | - "\n", |
2694 | 2694 | "def next(iterator, default=..., /)\n", |
2695 | | - "\n", |
2696 | | - "\n", |
2697 | | - "```\n", |
2698 | | - "\n", |
2699 | | - "*Return the next item from the iterator.*\n", |
| 2695 | + "\"\"\"\n", |
| 2696 | + "Return the next item from the iterator.\n", |
2700 | 2697 | "\n", |
2701 | 2698 | "If default is given and the iterator is exhausted,\n", |
2702 | | - "it is returned instead of raising StopIteration." |
| 2699 | + "it is returned instead of raising StopIteration.\n", |
| 2700 | + "\"\"\"" |
2703 | 2701 | ] |
2704 | 2702 | }, |
2705 | 2703 | "execution_count": null, |
|
2782 | 2780 | "assert 'x:int' not in str(_r3.dm), \"Original params should not leak through\"" |
2783 | 2781 | ] |
2784 | 2782 | }, |
| 2783 | + { |
| 2784 | + "cell_type": "code", |
| 2785 | + "execution_count": null, |
| 2786 | + "id": "e7eaff97", |
| 2787 | + "metadata": {}, |
| 2788 | + "outputs": [ |
| 2789 | + { |
| 2790 | + "data": { |
| 2791 | + "text/markdown": [ |
| 2792 | + "```python\n", |
| 2793 | + "\n", |
| 2794 | + "def Foo.__call__(\n", |
| 2795 | + " x\n", |
| 2796 | + "):\n", |
| 2797 | + "\n", |
| 2798 | + "\n", |
| 2799 | + "```\n", |
| 2800 | + "\n", |
| 2801 | + "*Call foo*" |
| 2802 | + ], |
| 2803 | + "text/plain": [ |
| 2804 | + "def Foo.__call__(\n", |
| 2805 | + " x\n", |
| 2806 | + "):\n", |
| 2807 | + "\"\"\"\n", |
| 2808 | + "Call foo\n", |
| 2809 | + "\"\"\"" |
| 2810 | + ] |
| 2811 | + }, |
| 2812 | + "execution_count": null, |
| 2813 | + "metadata": {}, |
| 2814 | + "output_type": "execute_result" |
| 2815 | + } |
| 2816 | + ], |
| 2817 | + "source": [ |
| 2818 | + "class Foo:\n", |
| 2819 | + " def __call__(self, x):\n", |
| 2820 | + " \"Call foo\"\n", |
| 2821 | + " return x\n", |
| 2822 | + "\n", |
| 2823 | + "foo = Foo()\n", |
| 2824 | + "MarkdownRenderer(foo)" |
| 2825 | + ] |
| 2826 | + }, |
2785 | 2827 | { |
2786 | 2828 | "cell_type": "markdown", |
2787 | 2829 | "id": "51a04f95", |
|
0 commit comments