You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
With autodoc_typehints = 'description' in the sphinx config and from __future__ import annotations in the Pythonfile, the parameter type annotations of the constructor (__init__) are not resolved correctly.
How to Reproduce
mylib.__init__.py
from __future__ importannotationsimporttypingastclassDummy:
""" :param arg: unresolved type hint """def__init__(self, arg: t.Optional[str]):
pass
Removing from __future__ import annotations from mylib.__init__.py makes the problem go away
Like wise does (only) removing autodoc_typehints = "description" from conf.py resolve the issue (even though it makes the type info appear in a different place as intended)
IIUC, it only affect __init__ and possibly __new__, right?
I know that the constructor is treated a bit differently because we allow param lists to be specified at the class level. It is very likely that we cannot resolve the forward reference at that point and then, when we want to resolve the type annotation, we are actually lacking the t in the local namespaces, leading to this bug.
I know where to look roughly but since I cannot do anything (or not much) with only my phone+termux, I will only be able to look at it in mid October.
In the meantime, you could try to specify a type alias using autodoc_type_aliases (check the doc for that). As I said in #11652 (comment), we can improve the type aliases map to handle those cases. Upon my return, I will draft an improvement of the latter in order to fix autodoc a bit more.
That's the conclusion I came to as well. You can have a look at the linked PR and you'll see that I proposed a fix for that: namely, emitting an event when the type annotations are being resolved so that the post processor can pick them up. But I understand that introducing an event just for that is not desirable.
Thanks for hinting me at autodoc_type_aliases. I shall have a look and see if it allows me to work around the problem.
Describe the bug
Turning #10605 (comment) into its own issue.
With
autodoc_typehints = 'description'
in the sphinx config andfrom __future__ import annotations
in the Pythonfile, the parameter type annotations of the constructor (__init__
) are not resolved correctly.How to Reproduce
mylib.__init__.py
index.rst
Sphinx
conf.py
This results in
Removing
from __future__ import annotations
frommylib.__init__.py
makes the problem go awayLike wise does (only) removing
autodoc_typehints = "description"
fromconf.py
resolve the issue (even though it makes the type info appear in a different place as intended)Environment Information
Sphinx extensions
['sphinx.ext.autodoc']
The text was updated successfully, but these errors were encountered: