Skip to content

Commit 5d70682

Browse files
authored
Merge pull request #8108 from tk0miya/8099_NameError_for_TYPE_CHECKING
Fix #8099: autodoc: NameError is raised when script uses TYPE_CHECKING
2 parents 90e9b31 + d391212 commit 5d70682

File tree

4 files changed

+37
-0
lines changed

4 files changed

+37
-0
lines changed

CHANGES

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ Bugs fixed
4848
class
4949
* #8091: autodoc: AttributeError is raised on documenting an attribute on Python
5050
3.5.2
51+
* #8099: autodoc: NameError is raised when target code uses ``TYPE_CHECKING``
5152
* C++, fix parsing of template template paramters, broken by the fix of #7944
5253

5354
Testing

sphinx/ext/autodoc/__init__.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1608,6 +1608,9 @@ def add_directive_header(self, sig: str) -> None:
16081608
# obtain annotation for this data
16091609
try:
16101610
annotations = get_type_hints(self.parent)
1611+
except NameError:
1612+
# Failed to evaluate ForwardRef (maybe TYPE_CHECKING)
1613+
annotations = safe_getattr(self.parent, '__annotations__', {})
16111614
except TypeError:
16121615
annotations = {}
16131616
except KeyError:
@@ -1984,6 +1987,9 @@ def add_directive_header(self, sig: str) -> None:
19841987
# obtain type annotation for this attribute
19851988
try:
19861989
annotations = get_type_hints(self.parent)
1990+
except NameError:
1991+
# Failed to evaluate ForwardRef (maybe TYPE_CHECKING)
1992+
annotations = safe_getattr(self.parent, '__annotations__', {})
19871993
except TypeError:
19881994
annotations = {}
19891995
except KeyError:
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from typing import TYPE_CHECKING
2+
3+
if TYPE_CHECKING:
4+
from io import StringIO
5+
6+
7+
class Foo:
8+
attr1: "StringIO"

tests/test_ext_autodoc.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1740,6 +1740,28 @@ def test_autodoc_Annotated(app):
17401740
]
17411741

17421742

1743+
@pytest.mark.skipif(sys.version_info < (3, 6), reason='py36+ is required.')
1744+
@pytest.mark.sphinx('html', testroot='ext-autodoc')
1745+
def test_autodoc_TYPE_CHECKING(app):
1746+
options = {"members": None,
1747+
"undoc-members": None}
1748+
actual = do_autodoc(app, 'module', 'target.TYPE_CHECKING', options)
1749+
assert list(actual) == [
1750+
'',
1751+
'.. py:module:: target.TYPE_CHECKING',
1752+
'',
1753+
'',
1754+
'.. py:class:: Foo()',
1755+
' :module: target.TYPE_CHECKING',
1756+
'',
1757+
'',
1758+
' .. py:attribute:: Foo.attr1',
1759+
' :module: target.TYPE_CHECKING',
1760+
' :type: StringIO',
1761+
'',
1762+
]
1763+
1764+
17431765
@pytest.mark.sphinx('html', testroot='pycode-egg')
17441766
def test_autodoc_for_egged_code(app):
17451767
options = {"members": None,

0 commit comments

Comments
 (0)