@@ -172,11 +172,11 @@ def get_annotation_args(annotation: Any, module: str, class_name: str) -> tuple[
172
172
return () if len (result ) == 1 and result [0 ] == () else result # type: ignore[misc]
173
173
174
174
175
- def format_internal_tuple (t : tuple [Any , ...], config : Config ) -> str :
175
+ def format_internal_tuple (t : tuple [Any , ...], config : Config , * , short_literals : bool = False ) -> str :
176
176
# An annotation can be a tuple, e.g., for numpy.typing:
177
177
# In this case, format_annotation receives:
178
178
# This solution should hopefully be general for *any* type that allows tuples in annotations
179
- fmt = [format_annotation (a , config ) for a in t ]
179
+ fmt = [format_annotation (a , config , short_literals = short_literals ) for a in t ]
180
180
if len (fmt ) == 0 :
181
181
return "()"
182
182
if len (fmt ) == 1 :
@@ -196,12 +196,13 @@ def fixup_module_name(config: Config, module: str) -> str:
196
196
return module
197
197
198
198
199
- def format_annotation (annotation : Any , config : Config ) -> str : # noqa: C901, PLR0911, PLR0912, PLR0915, PLR0914
199
+ def format_annotation (annotation : Any , config : Config , * , short_literals : bool = False ) -> str : # noqa: C901, PLR0911, PLR0912, PLR0915, PLR0914
200
200
"""
201
201
Format the annotation.
202
202
203
203
:param annotation:
204
204
:param config:
205
+ :param short_literals: Render :py:class:`Literals` in PEP 604 style (``|``).
205
206
:return:
206
207
"""
207
208
typehints_formatter : Callable [..., str ] | None = getattr (config , "typehints_formatter" , None )
@@ -222,7 +223,7 @@ def format_annotation(annotation: Any, config: Config) -> str: # noqa: C901, PL
222
223
return format_internal_tuple (annotation , config )
223
224
224
225
if isinstance (annotation , TypeAliasForwardRef ):
225
- return str ( annotation )
226
+ return annotation . name
226
227
227
228
try :
228
229
module = get_annotation_module (annotation )
@@ -254,7 +255,7 @@ def format_annotation(annotation: Any, config: Config) -> str: # noqa: C901, PL
254
255
params = {k : getattr (annotation , f"__{ k } __" ) for k in ("bound" , "covariant" , "contravariant" )}
255
256
params = {k : v for k , v in params .items () if v }
256
257
if "bound" in params :
257
- params ["bound" ] = f" { format_annotation (params ['bound' ], config )} "
258
+ params ["bound" ] = f" { format_annotation (params ['bound' ], config , short_literals = short_literals )} "
258
259
args_format = f"\\ (``{ annotation .__name__ } ``{ ', {}' if args else '' } "
259
260
if params :
260
261
args_format += "" .join (f", { k } ={ v } " for k , v in params .items ())
@@ -275,20 +276,22 @@ def format_annotation(annotation: Any, config: Config) -> str: # noqa: C901, PL
275
276
args_format = f"\\ [:py:data:`{ prefix } typing.Union`\\ [{{}}]]"
276
277
args = tuple (x for x in args if x is not type (None ))
277
278
elif full_name in {"typing.Callable" , "collections.abc.Callable" } and args and args [0 ] is not ...:
278
- fmt = [format_annotation (arg , config ) for arg in args ]
279
+ fmt = [format_annotation (arg , config , short_literals = short_literals ) for arg in args ]
279
280
formatted_args = f"\\ [\\ [{ ', ' .join (fmt [:- 1 ])} ], { fmt [- 1 ]} ]"
280
281
elif full_name == "typing.Literal" :
282
+ if short_literals :
283
+ return f"\\ { ' | ' .join (f'``{ arg !r} ``' for arg in args )} "
281
284
formatted_args = f"\\ [{ ', ' .join (f'``{ arg !r} ``' for arg in args )} ]"
282
285
elif is_bars_union :
283
- return " | " .join ([format_annotation (arg , config ) for arg in args ])
286
+ return " | " .join ([format_annotation (arg , config , short_literals = short_literals ) for arg in args ])
284
287
285
288
if args and not formatted_args :
286
289
try :
287
290
iter (args )
288
291
except TypeError :
289
- fmt = [format_annotation (args , config )]
292
+ fmt = [format_annotation (args , config , short_literals = short_literals )]
290
293
else :
291
- fmt = [format_annotation (arg , config ) for arg in args ]
294
+ fmt = [format_annotation (arg , config , short_literals = short_literals ) for arg in args ]
292
295
formatted_args = args_format .format (", " .join (fmt ))
293
296
294
297
escape = "\\ " if formatted_args else ""
@@ -783,7 +786,10 @@ def _inject_signature(
783
786
if annotation is None :
784
787
type_annotation = f":type { arg_name } : "
785
788
else :
786
- formatted_annotation = add_type_css_class (format_annotation (annotation , app .config ))
789
+ short_literals = app .config .python_display_short_literal_types
790
+ formatted_annotation = add_type_css_class (
791
+ format_annotation (annotation , app .config , short_literals = short_literals )
792
+ )
787
793
type_annotation = f":type { arg_name } : { formatted_annotation } "
788
794
789
795
if app .config .typehints_defaults :
@@ -923,7 +929,10 @@ def _inject_rtype( # noqa: PLR0913, PLR0917
923
929
if not app .config .typehints_use_rtype and r .found_return and " -- " in lines [insert_index ]:
924
930
return
925
931
926
- formatted_annotation = add_type_css_class (format_annotation (type_hints ["return" ], app .config ))
932
+ short_literals = app .config .python_display_short_literal_types
933
+ formatted_annotation = add_type_css_class (
934
+ format_annotation (type_hints ["return" ], app .config , short_literals = short_literals )
935
+ )
927
936
928
937
if r .found_param and insert_index < len (lines ) and lines [insert_index ].strip ():
929
938
insert_index -= 1
0 commit comments