File tree Expand file tree Collapse file tree 2 files changed +15
-1
lines changed
libraries/mathy_pydoc_markdown Expand file tree Collapse file tree 2 files changed +15
-1
lines changed Original file line number Diff line number Diff line change 2929import inspect
3030import types
3131from typing import Callable , Optional , List , Any
32+ import re
3233
3334function_types = (
3435 types .FunctionType ,
4041if hasattr (types , "UnboundMethodType" ):
4142 function_types += (types .UnboundMethodType ,)
4243
44+ # Used to replace verbose type reported, e.g. "Union[str, NoneType]" with the simpler
45+ # form that is usually used in code "Optional[str]"
46+ optional_match = r"(.*)Union\[(.*),\sNoneType\](.*)"
47+ optional_replace = r"\1Optional[\2]\3"
48+
4349
4450def trim (docstring ):
4551 if not docstring :
@@ -169,6 +175,10 @@ def get_callable_placeholder(
169175 annotation = inspect .formatannotation (p .annotation )
170176 if p .default is not inspect ._empty : # type: ignore
171177 default_value = str (p .default )
178+
179+ # The type annotations expand Optional[T] to Union[T, NoneType]
180+ while re .search (optional_match , annotation ) is not None :
181+ annotation = re .sub (optional_match , optional_replace , annotation )
172182 params .append (CallableArg (p .name , annotation , default_value ))
173183
174184 return_annotation = None
Original file line number Diff line number Diff line change 22
33
44def a_function_with_types (
5- inputs : List [Dict [str , Any ]], auxiliary : Tuple [Optional [str ]], final : int = 3
5+ optional : Optional [str ],
6+ nested_optional : Optional [Tuple [Optional [str ], Optional [int ]]],
7+ inputs : List [Dict [str , Any ]],
8+ auxiliary : Tuple [Optional [str ]],
9+ final : int = 3 ,
610) -> List [str ]:
711 """Sweet typed function"""
812 pass
You can’t perform that action at this time.
0 commit comments