Skip to content

Commit 67493fc

Browse files
committed
Extract evaluation of non-type objects
1 parent f4ff61a commit 67493fc

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

sphinx/util/typing.py

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,14 @@ def restify(cls: type | None, mode: _RestifyMode = 'fully-qualified-except-typin
179179
msg = f'mode must be one of {valid}; got {mode!r}'
180180
raise ValueError(msg)
181181

182+
# things that are not types
183+
if cls is None or cls is NoneType:
184+
return ':py:obj:`None`'
185+
if cls is Ellipsis:
186+
return '...'
187+
if isinstance(cls, str):
188+
return cls
189+
182190
# If the mode is 'smart', we always use '~'.
183191
# If the mode is 'fully-qualified-except-typing',
184192
# we use '~' only for the objects in the ``typing`` module.
@@ -188,13 +196,7 @@ def restify(cls: type | None, mode: _RestifyMode = 'fully-qualified-except-typin
188196
modprefix = ''
189197

190198
try:
191-
if cls is None or cls is NoneType:
192-
return ':py:obj:`None`'
193-
elif cls is Ellipsis:
194-
return '...'
195-
elif isinstance(cls, str):
196-
return cls
197-
elif ismockmodule(cls):
199+
if ismockmodule(cls):
198200
return f':py:class:`{modprefix}{cls.__name__}`'
199201
elif ismock(cls):
200202
return f':py:class:`{modprefix}{cls.__module__}.{cls.__name__}`'
@@ -307,6 +309,19 @@ def stringify_annotation(
307309
msg = f'mode must be one of {valid}; got {mode!r}'
308310
raise ValueError(msg)
309311

312+
# things that are not types
313+
if annotation is None or annotation is NoneType:
314+
return 'None'
315+
if annotation is Ellipsis:
316+
return '...'
317+
if isinstance(annotation, str):
318+
if annotation.startswith("'") and annotation.endswith("'"):
319+
# Might be a double Forward-ref'ed type. Go unquoting.
320+
return annotation[1:-1]
321+
return annotation
322+
if not annotation:
323+
return repr(annotation)
324+
310325
if mode == 'smart':
311326
module_prefix = '~'
312327
else:
@@ -317,13 +332,7 @@ def stringify_annotation(
317332
annotation_name = getattr(annotation, '__name__', '')
318333
annotation_module_is_typing = annotation_module == 'typing'
319334

320-
if isinstance(annotation, str):
321-
if annotation.startswith("'") and annotation.endswith("'"):
322-
# might be a double Forward-ref'ed type. Go unquoting.
323-
return annotation[1:-1]
324-
else:
325-
return annotation
326-
elif isinstance(annotation, TypeVar):
335+
if isinstance(annotation, TypeVar):
327336
if annotation_module_is_typing and mode in {'fully-qualified-except-typing', 'smart'}:
328337
return annotation_name
329338
else:
@@ -334,10 +343,6 @@ def stringify_annotation(
334343
return module_prefix + f'{annotation_module}.{annotation_name}'
335344
else:
336345
return annotation_name
337-
elif not annotation:
338-
return repr(annotation)
339-
elif annotation is NoneType:
340-
return 'None'
341346
elif ismockmodule(annotation):
342347
return module_prefix + annotation_name
343348
elif ismock(annotation):
@@ -355,8 +360,6 @@ def stringify_annotation(
355360
return f'{annotation_qualname}[{concatenated_args}]'
356361
else:
357362
return annotation_qualname
358-
elif annotation is Ellipsis:
359-
return '...'
360363

361364
module_prefix = f'{annotation_module}.'
362365
annotation_forward_arg = getattr(annotation, '__forward_arg__', None)

0 commit comments

Comments
 (0)