@@ -91,8 +91,23 @@ def _init_vars(app: Sphinx, config: Config):
91
91
rtd_links_prefix = PurePosixPath (config .rtd_links_prefix )
92
92
93
93
94
+ def _get_annotations (obj : Any ) -> dict [str , Any ]:
95
+ try :
96
+ from inspect import get_annotations
97
+ except ImportError :
98
+ from get_annotations import get_annotations
99
+
100
+ try :
101
+ return get_annotations (obj )
102
+ except TypeError :
103
+ return {}
104
+
105
+
94
106
def _get_obj_module (qualname : str ) -> tuple [Any , ModuleType ]:
95
- """Get a module/class/attribute and its original module by qualname."""
107
+ """Get a module/class/attribute and its original module by qualname.
108
+
109
+ Returns `None` as `obj` if it’s an annotated field without value.
110
+ """
96
111
modname = qualname
97
112
attr_path = []
98
113
while modname not in sys .modules :
@@ -109,6 +124,8 @@ def _get_obj_module(qualname: str) -> tuple[Any, ModuleType]:
109
124
except AttributeError as e :
110
125
if is_dataclass (obj ):
111
126
thing = next (f for f in fields (obj ) if f .name == attr_name )
127
+ elif attr_name in _get_annotations (thing ):
128
+ thing = None
112
129
else :
113
130
try :
114
131
thing = import_module (f"{ mod .__name__ } .{ attr_name } " )
@@ -158,9 +175,10 @@ def github_url(qualname: str) -> str:
158
175
"""
159
176
try :
160
177
obj , module = _get_obj_module (qualname )
161
- except Exception :
162
- print (f"Error in github_url({ qualname !r} ):" , file = sys .stderr )
163
- raise
178
+ except Exception as e :
179
+ if hasattr (e , "__notes__" ):
180
+ e .__notes__ .append (f"Qualname: { qualname !r} " )
181
+ raise e
164
182
path = rtd_links_prefix / _module_path (obj , module )
165
183
start , end = _get_linenos (obj )
166
184
fragment = f"#L{ start } -L{ end } " if start and end else ""
@@ -220,5 +238,8 @@ def setup(app: Sphinx) -> dict[str, Any]:
220
238
from dataclasses import dataclass , field , fields , is_dataclass
221
239
222
240
@dataclass
223
- class _TestCls :
241
+ class _TestDataCls :
224
242
test_attr : dict [str , str ] = field (default_factory = dict )
243
+
244
+ class _TestCls :
245
+ test_anno : int
0 commit comments