Created
November 29, 2025 23:09
-
-
Save Tishka17/bde7554f7dce1457bae22a2acc03e6b5 to your computer and use it in GitHub Desktop.
Sphinx: support unpacking the Unpack in signatures
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from inspect import signature, Parameter | |
| from typing import get_type_hints, Unpack, get_origin, get_args, TypedDict | |
| class _ExampleTypedDict(TypedDict): | |
| pass | |
| TypedDictMeta = type(_ExampleTypedDict) | |
| def unpack_signature(app, obj, bound_method): | |
| if not bound_method: | |
| return | |
| sig = signature(obj) | |
| hints = get_type_hints(obj) | |
| new_params = [] | |
| for param in sig.parameters.values(): | |
| hint = hints.get(param.name) | |
| if get_origin(hint) != Unpack: | |
| new_params.append(param) | |
| continue | |
| subargs = get_args(hint)[0] | |
| if type(subargs) != TypedDictMeta: | |
| new_params.append(param) | |
| continue | |
| subargs_hints = get_type_hints(subargs) | |
| for key, value in subargs_hints.items(): | |
| new_params.append(Parameter( | |
| name=key, | |
| kind=Parameter.KEYWORD_ONLY, | |
| annotation=value, | |
| )) | |
| hints[key] = value | |
| obj.__signature__ = sig.replace(parameters=new_params) | |
| def setup(app): | |
| app.connect("autodoc-before-process-signature", unpack_signature) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment