Sphere
Log in Join
☆ 10

functools.wraps keeps the runtime signature but Sphinx shows star args

Python 3.10. I have a decorator using @wraps(func) and inspect.signature() shows the wrapped function's signature correctly. But Sphinx autodoc renders the decorator's signature (args, *kwargs) instead of the real one. I'm on Sphinx 7.2. Any idea what autodoc looks at? Is there a config option I'm missing, or does wraps not propagate everything autodoc needs? The docs claim it copies __name__, __doc__, and __module__, but I can't find any mention of signature metadata.

⚑ Report

4 replies

☆ 0
H hex ·2026-10-06#1

Sphinx autodoc has its own signature handling. It might be reading __wrapped__ differently, or an extension config is interfering. Check autodoc_typehints and whether sphinx.ext.autodoc is loaded before your decorator is applied. Also — are you decorating a method? Sphinx sometimes strips the first argument in ways that make the whole thing look wrong.

⚑ Report
☆ 0
V vector ·2026-10-10#2

It's a plain function, not a method. I found autodoc_preserve_defaults in my conf.py — could that be it? Also the decorated functions are imported from another module into the one autodoc is documenting, so maybe there's an indirection I'm not accounting for.

⚑ Report
☆ 0
F fern ·2026-10-12#3

Try setting __signature__ explicitly on the wrapper — wrapper.__signature__ = inspect.signature(func) inside your decorator. Sphinx respects that attribute and it wins over its own inspection logic. That's more reliable than fiddling with autodoc settings and it fixes inspect.signature consistently too.

⚑ Report
☆ 0
F fern ·2026-10-16#4

Setting __signature__ fixed it for us. Also set __annotations__ if you want type hints in the docs — wraps copies __annotations__ but Sphinx 7 might need both present on the wrapper itself. Bit of a hack but harmless, and it makes the decorator behave the same under every introspection tool.

⚑ Report
Log in to reply.