99from sphinx .ext .autodoc ._member_finder import _gather_members
1010from sphinx .ext .autodoc ._renderer import _add_content , _directive_header_lines
1111from sphinx .ext .autodoc ._sentinels import ALL
12+ from sphinx .ext .autodoc ._shared import _get_render_mode
1213from sphinx .ext .autodoc .mock import ismock
1314from sphinx .locale import _ , __
1415from sphinx .pycode import ModuleAnalyzer
1718
1819if TYPE_CHECKING :
1920 from collections .abc import Iterator , Mapping , MutableSet
20- from typing import Literal
2121
22- from sphinx .config import Config
2322 from sphinx .environment import _CurrentDocument
2423 from sphinx .events import EventManager
2524 from sphinx .ext .autodoc ._directive_options import _AutoDocumenterOptions
26- from sphinx .ext .autodoc ._importer import _AttrGetter
2725 from sphinx .ext .autodoc ._property_types import _ItemProperties
26+ from sphinx .ext .autodoc ._shared import _AttrGetter , _AutodocConfig
2827 from sphinx .util .typing import _RestifyMode
2928
3029logger = logging .getLogger ('sphinx.ext.autodoc' )
@@ -36,7 +35,7 @@ def _generate_directives(
3635 check_module : bool = False ,
3736 all_members : bool = False ,
3837 * ,
39- config : Config ,
38+ config : _AutodocConfig ,
4039 current_document : _CurrentDocument ,
4140 events : EventManager ,
4241 get_attr : _AttrGetter ,
@@ -154,7 +153,7 @@ def _add_directive_lines(
154153 * ,
155154 more_content : StringList | None ,
156155 is_final : bool ,
157- config : Config ,
156+ config : _AutodocConfig ,
158157 indent : str ,
159158 options : _AutoDocumenterOptions ,
160159 props : _ItemProperties ,
@@ -180,8 +179,8 @@ def _add_directive_lines(
180179
181180 # add alias information, if applicable
182181 lines = _body_alias_lines (
183- typehints_format = config .autodoc_typehints_format ,
184- python_display_short_literal_types = config .python_display_short_literal_types ,
182+ render_mode = _get_render_mode ( config .autodoc_typehints_format ) ,
183+ short_literals = config .python_display_short_literal_types ,
185184 props = props ,
186185 )
187186 alias_lines = StringList (list (lines ), source = '' )
@@ -204,7 +203,7 @@ def _document_members(
204203 all_members : bool ,
205204 analyzer_order : dict [str , int ],
206205 attr_docs : dict [tuple [str , str ], list [str ]],
207- config : Config ,
206+ config : _AutodocConfig ,
208207 current_document : _CurrentDocument ,
209208 events : EventManager ,
210209 get_attr : _AttrGetter ,
@@ -276,22 +275,17 @@ def _document_members(
276275
277276
278277def _body_alias_lines (
279- * ,
280- props : _ItemProperties ,
281- typehints_format : Literal ['fully-qualified' , 'short' ],
282- python_display_short_literal_types : bool ,
278+ * , props : _ItemProperties , render_mode : _RestifyMode , short_literals : bool
283279) -> Iterator [str ]:
284280 """Add content from docstrings, attribute documentation and user."""
285281 if props .obj_type in {'data' , 'attribute' }:
286282 from sphinx .ext .autodoc ._property_types import _AssignStatementProperties
287283
288284 assert isinstance (props , _AssignStatementProperties )
289285
290- mode = _get_render_mode (typehints_format )
291-
292286 # Support for documenting GenericAliases
293287 if props ._obj_is_generic_alias :
294- alias = restify (props ._obj , mode = mode )
288+ alias = restify (props ._obj , mode = render_mode )
295289 yield _ ('alias of %s' ) % alias
296290 yield ''
297291 return
@@ -303,27 +297,25 @@ def _body_alias_lines(
303297 assert isinstance (props , _ClassDefProperties )
304298
305299 obj = props ._obj
306- mode = _get_render_mode (typehints_format )
307300
308301 if props ._obj_is_new_type :
309- supertype = restify (obj .__supertype__ , mode = mode )
302+ supertype = restify (obj .__supertype__ , mode = render_mode )
310303 yield _ ('alias of %s' ) % supertype
311304 yield ''
312305 return
313306
314307 if props ._obj_is_typevar :
315- short_literals = python_display_short_literal_types
316308 attrs = [
317309 repr (obj .__name__ ),
318310 * (
319311 stringify_annotation (
320- constraint , mode , short_literals = short_literals
312+ constraint , render_mode , short_literals = short_literals
321313 )
322314 for constraint in obj .__constraints__
323315 ),
324316 ]
325317 if obj .__bound__ :
326- attrs .append (rf'bound=\ { restify (obj .__bound__ , mode = mode )} ' )
318+ attrs .append (rf'bound=\ { restify (obj .__bound__ , mode = render_mode )} ' )
327319 if obj .__covariant__ :
328320 attrs .append ('covariant=True' )
329321 if obj .__contravariant__ :
@@ -345,7 +337,7 @@ def _body_alias_lines(
345337
346338 if class_var_doc_comment :
347339 return
348- alias = restify (obj , mode = mode )
340+ alias = restify (obj , mode = render_mode )
349341 yield _ ('alias of %s' ) % alias
350342 return
351343
@@ -367,11 +359,3 @@ def _docstring_source_name(*, props: _ItemProperties, source: str) -> str:
367359 if source :
368360 return f'{ source } :docstring of { fullname } '
369361 return f'docstring of { fullname } '
370-
371-
372- def _get_render_mode (
373- typehints_format : Literal ['fully-qualified' , 'short' ],
374- ) -> _RestifyMode :
375- if typehints_format == 'short' :
376- return 'smart'
377- return 'fully-qualified-except-typing'
0 commit comments