diff --git a/pdoc/__init__.py b/pdoc/__init__.py index 8d8c3b09..200063ae 100644 --- a/pdoc/__init__.py +++ b/pdoc/__init__.py @@ -771,7 +771,8 @@ def is_from_this_module(obj): for name, obj in public_objs: if _is_function(obj): - self.doc[name] = Function(name, self, obj) + self.doc[name] = Function( + name, self, obj, docstring=var_docstrings.get(name)) elif inspect.isclass(obj): self.doc[name] = Class(name, self, obj) elif name in var_docstrings: @@ -1123,7 +1124,8 @@ def definition_order_index( for name, obj in public_objs: if _is_function(obj): self.doc[name] = Function( - name, self.module, obj, cls=self) + name, self.module, obj, cls=self, + docstring=var_docstrings.get(name)) else: self.doc[name] = Variable( name, self.module, @@ -1407,10 +1409,12 @@ class Function(Doc): """ Representation of documentation for a function or method. """ - def __init__(self, name: str, module: Module, obj, *, cls: Optional[Class] = None): + def __init__(self, name: str, module: Module, obj, *, + cls: Optional[Class] = None, docstring: Optional[str] = None): """ Same as `pdoc.Doc`, except `obj` must be a - Python function object. The docstring is gathered automatically. + Python function object. Pass `docstring` to override + `inspect.getdoc()`, for example a PEP-224 string on an alias. `cls` should be set when this is a method or a static function beloing to a class. `cls` should be a `pdoc.Class` object. @@ -1419,7 +1423,7 @@ def __init__(self, name: str, module: Module, obj, *, cls: Optional[Class] = Non all other cases, it should be `False`. """ assert callable(obj), (name, module, obj) - super().__init__(name, module, obj) + super().__init__(name, module, obj, docstring=docstring or '') self.cls = cls """ diff --git a/pdoc/test/__init__.py b/pdoc/test/__init__.py index bc4a3a6f..35d630ce 100644 --- a/pdoc/test/__init__.py +++ b/pdoc/test/__init__.py @@ -11,6 +11,7 @@ import sys import typing import threading +import textwrap import unittest import warnings from contextlib import contextmanager, redirect_stderr, redirect_stdout @@ -572,6 +573,41 @@ class C: self.assertEqual(doc.doc['vars_dont'].docstring, expected) self.assertIn('integer', doc.doc['but_clss_have_doc'].docstring) + def test_pep224_docstring_overrides_callable_alias(self): + # https://github.com/pdoc3/pdoc/issues/418 + source = textwrap.dedent('''\ + from typing import Callable + + def _impl(*args, **initial_values) -> str: + """Implementation docstring.""" + return "x" + + __all__ = ["get_logger", "Box"] + + get_logger: Callable[..., str] = _impl + """Custom docstring""" + + class Box: + alias: Callable[..., str] = _impl + """Alias docstring""" + ''') + with temp_dir() as path: + filename = os.path.join(path, 'pep224_callable_alias.py') + with open(filename, 'w', encoding='utf-8') as handle: + handle.write(source) + sys.path.insert(0, path) + try: + text = pdoc.text('pep224_callable_alias') + finally: + sys.path.remove(path) + sys.modules.pop('pep224_callable_alias', None) + pdoc.reset() + self.assertIn('Custom docstring', text) + self.assertIn('Alias docstring', text) + self.assertNotIn('Implementation docstring', text) + self.assertIn('get_logger(*args, **initial_values)', text) + self.assertIn('alias(*args, **initial_values)', text) + @unittest.skipIf(sys.version_info >= (3, 10), 'No builtin module "parser" in Py3.10') def test_builtin_methoddescriptors(self): import parser # TODO: replace with another public binary builtin