-
-
Notifications
You must be signed in to change notification settings - Fork 2.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Autodoc has trouble with type hinting generics and class name forward references #11327
Comments
While I was trying to investigate this, I see that not using
However, the value of What I found so far:
I will try investigating. The only difference I have with your build is the version of Generating script#!/usr/bin/env bash
mkdir -p gh-11327
cd gh-11327
cat <<-EOF > conf.py
import os
project = 'Foo'
copyright = '2023, picnixz'
author = 'picnixz'
nitpicky = True
extensions = ['sphinx.ext.autodoc']
autodoc_typehints = 'description'
master_doc = 'index'
include_patterns = ['index.rst']
html_theme = 'alabaster'
EOF
cat <<-EOF > index.rst
Project
=======
.. automodule:: main
:members:
EOF
cat <<-EOF > main.py_t
FUTURE_ANNOTATION
from typing import List
class Baz:
"""docstring"""
def baz(self) -> RETURN_ANNOTATION:
""":return: docstring"""
return [Baz()]
EOF
RESULTS=''
for autodoc_typehint in 'signature' 'description' ; do
for future_annotation in 'no' 'yes' ; do
for return_annotation in 'list["Baz"]' 'list[Baz]' 'List["Baz"]' 'List[Baz]' ; do
if [[ $future_annotation =~ 'no' ]] ; then
sed -E '/FUTURE_ANNOTATION/d' main.py_t > main.py
else
sed -E 's/FUTURE_ANNOTATION/from __future__ import annotations/' main.py_t > main.py
fi
sed -i'.bak' -E 's/RETURN_ANNOTATION/'"${return_annotation}"'/' main.py
export AUTODOC_TYPEHINT=$autodoc_typehint
python3.10 -m sphinx -M html . _build -q -E -W &>/dev/null
[[ $? -eq 0 ]] && { status='yes' ; } || { status='no'; }
printf 'autodoc_typehint=%s, future_annotation=%s, return_annotation=%s, working=%s\n' \
"${autodoc_typehint}" "${future_annotation}" "${return_annotation}" "$status"
read -r -d '' row <<- EOM
<tr>
<td><code>${autodoc_typehint}</code></td>
<td>${future_annotation}</td>
<td><code>${return_annotation}</code></td>
<td>$status</td>
</tr>
EOM
read -r -d '' RESULTS <<-EOM
$RESULTS
$row
EOM
done
done
done
cat <<-EOF > table.html
<table>
<thead>
<th><code>autodoc_typehints</code></th>
<th><code>from __future__ import annotations</code></th>
<th>annotation</th>
<th>working</th>
</thead>
<tbody>
$RESULTS
</tbody>
</table>
EOF Environment
|
Important update According to Python official documentation on ForwardRef:
This is the reason why I have created a branch which can be used as a playground for anyone who wants to work in that direction (https://github.com/picnixz/sphinx/tree/fix/11327-autodoc-forward-ref) by adding some testing roots. In the meantime, I would either suggest using Possibly related to: #10605 |
Yes, this is what I meant, apologies for the typo. |
Describe the bug
My library contains class methods with class name forward references like:
Starting with Python 3.9, we're now able to use type hinting generics in standard collections (PEP 585):
However, autodoc does not like this.
How to Reproduce
With the following
conf.py
:and the following
index.rst
:and the following
main.py
:we can reproduce the issue:
The Foo and Bar methods work fine, but the Baz method does not. The only way I've managed to get Baz working is via:
Environment Information
Sphinx extensions
Additional context
Also reproduced with Python 3.9, Sphinx 5.2/5.3.
The text was updated successfully, but these errors were encountered: