@@ -95,7 +95,9 @@ def get_annotation_args(annotation, module: str, class_name: str) -> Tuple:
95
95
return getattr (annotation , '__args__' , ())
96
96
97
97
98
- def format_annotation (annotation , fully_qualified : bool = False ) -> str :
98
+ def format_annotation (annotation ,
99
+ fully_qualified : bool = False ,
100
+ simplify_optional_unions : bool = True ) -> str :
99
101
# Special cases
100
102
if annotation is None or annotation is type (None ): # noqa: E721
101
103
return ':py:obj:`None`'
@@ -130,18 +132,28 @@ def format_annotation(annotation, fully_qualified: bool = False) -> str:
130
132
if full_name == 'typing.NewType' :
131
133
args_format = '\\ (:py:data:`~{name}`, {{}})' .format (name = annotation .__name__ )
132
134
role = 'func'
133
- elif full_name == 'typing.Union' and len (args ) == 2 and type (None ) in args :
134
- full_name = 'typing.Optional'
135
- args = tuple (x for x in args if x is not type (None )) # noqa: E721
135
+ elif full_name == 'typing.Union' and type (None ) in args :
136
+ if len (args ) == 2 :
137
+ full_name = 'typing.Optional'
138
+ args = tuple (x for x in args if x is not type (None )) # noqa: E721
139
+ elif not simplify_optional_unions :
140
+ full_name = 'typing.Optional'
141
+ args_format = '\\ [:py:data:`{prefix}typing.Union`\\ [{{}}]]' .format (prefix = prefix )
142
+ args = tuple (x for x in args if x is not type (None )) # noqa: E721
136
143
elif full_name == 'typing.Callable' and args and args [0 ] is not ...:
137
- formatted_args = '\\ [\\ [' + ', ' .join (format_annotation (arg ) for arg in args [:- 1 ]) + ']'
138
- formatted_args += ', ' + format_annotation (args [- 1 ]) + ']'
144
+ formatted_args = '\\ [\\ [' + ', ' .join (
145
+ format_annotation (
146
+ arg , simplify_optional_unions = simplify_optional_unions )
147
+ for arg in args [:- 1 ]) + ']'
148
+ formatted_args += ', ' + format_annotation (
149
+ args [- 1 ], simplify_optional_unions = simplify_optional_unions ) + ']'
139
150
elif full_name == 'typing.Literal' :
140
151
formatted_args = '\\ [' + ', ' .join (repr (arg ) for arg in args ) + ']'
141
152
142
153
if args and not formatted_args :
143
- formatted_args = args_format .format (', ' .join (format_annotation (arg , fully_qualified )
144
- for arg in args ))
154
+ formatted_args = args_format .format (', ' .join (
155
+ format_annotation (arg , fully_qualified , simplify_optional_unions )
156
+ for arg in args ))
145
157
146
158
return ':py:{role}:`{prefix}{full_name}`{formatted_args}' .format (
147
159
role = role , prefix = prefix , full_name = full_name , formatted_args = formatted_args )
@@ -382,7 +394,9 @@ def process_docstring(app, what, name, obj, options, lines):
382
394
argname = '{}\\ _' .format (argname [:- 1 ])
383
395
384
396
formatted_annotation = format_annotation (
385
- annotation , fully_qualified = app .config .typehints_fully_qualified )
397
+ annotation ,
398
+ fully_qualified = app .config .typehints_fully_qualified ,
399
+ simplify_optional_unions = app .config .simplify_optional_unions )
386
400
387
401
searchfor = [':{} {}:' .format (field , argname )
388
402
for field in ('param' , 'parameter' , 'arg' , 'argument' )]
@@ -409,7 +423,9 @@ def process_docstring(app, what, name, obj, options, lines):
409
423
return
410
424
411
425
formatted_annotation = format_annotation (
412
- type_hints ['return' ], fully_qualified = app .config .typehints_fully_qualified )
426
+ type_hints ['return' ], fully_qualified = app .config .typehints_fully_qualified ,
427
+ simplify_optional_unions = app .config .simplify_optional_unions
428
+ )
413
429
414
430
insert_index = len (lines )
415
431
for i , line in enumerate (lines ):
@@ -439,6 +455,7 @@ def setup(app):
439
455
app .add_config_value ('always_document_param_types' , False , 'html' )
440
456
app .add_config_value ('typehints_fully_qualified' , False , 'env' )
441
457
app .add_config_value ('typehints_document_rtype' , True , 'env' )
458
+ app .add_config_value ('simplify_optional_unions' , True , 'env' )
442
459
app .connect ('builder-inited' , builder_ready )
443
460
app .connect ('autodoc-process-signature' , process_signature )
444
461
app .connect ('autodoc-process-docstring' , process_docstring )
0 commit comments