From 9e57aa5e1debfada7f3295d93eb1e1b7a53f964a Mon Sep 17 00:00:00 2001 From: dangotbanned <125183946+dangotbanned@users.noreply.github.com> Date: Sat, 29 Jun 2024 14:35:40 +0100 Subject: [PATCH] docs: Fix `use_signature` formatting issue Related to https://github.com/vega/altair/pull/3444#issuecomment-2196481574 *Placeholder for screenshot(s) documenting the bug* --- altair/utils/core.py | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/altair/utils/core.py b/altair/utils/core.py index 15453028e..a001f7a12 100644 --- a/altair/utils/core.py +++ b/altair/utils/core.py @@ -688,29 +688,32 @@ def infer_vegalite_type_for_dfi_column( raise ValueError(msg) -def use_signature(Obj: Callable[P, Any]): # -> Callable[..., Callable[P, V]]: - """Apply call signature and documentation of Obj to the decorated method""" +def use_signature(obj: Callable[P, Any]): # -> Callable[..., Callable[P, V]]: + """Apply call signature and documentation of `obj` to the decorated method""" - def decorate(f: Callable[..., V]) -> Callable[P, V]: - # call-signature of f is exposed via __wrapped__. - # we want it to mimic Obj.__init__ - f.__wrapped__ = Obj.__init__ # type: ignore - f._uses_signature = Obj # type: ignore + def decorate(func: Callable[..., V]) -> Callable[P, V]: + # call-signature of func is exposed via __wrapped__. + # we want it to mimic obj.__init__ - # Supplement the docstring of f with information from Obj - if Obj.__doc__: + # error: Accessing "__init__" on an instance is unsound, + # since instance.__init__ could be from an incompatible subclass [misc] + wrapped = ( + obj.__init__ if (isinstance(obj, type) and issubclass(obj, object)) else obj # type: ignore [misc] + ) + func.__wrapped__ = wrapped # type: ignore[attr-defined] + func._uses_signature = obj # type: ignore[attr-defined] + + # Supplement the docstring of func with information from obj + if doc_in := obj.__doc__: + doc_lines = doc_in.splitlines(keepends=True)[1:] # Patch in a reference to the class this docstring is copied from, # to generate a hyperlink. - doclines = Obj.__doc__.splitlines() - doclines[0] = f"Refer to :class:`{Obj.__name__}`" - - if f.__doc__: - doc = f.__doc__ + "\n".join(doclines[1:]) - else: - doc = "\n".join(doclines) - f.__doc__ = doc - - return f + line_1 = f"{func.__doc__ or f'Refer to :class:`{obj.__name__}`'}\n" + func.__doc__ = "".join((line_1, *doc_lines)) + return func + else: + msg = f"Found no doc for {obj!r}" + raise AttributeError(msg) return decorate