Skip to content

Commit 186be77

Browse files
feat(mathy_pydoc): cleanup return type annotations
1 parent cf83bfe commit 186be77

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
lines changed

libraries/mathy_pydoc_markdown/mathy_pydoc/loader.py

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,20 @@
4646
optional_replace = r"\1Optional[\2]\3"
4747

4848
# _ForwardRef('MathyEnvState') -> MathyEnvState
49-
fwd_ref_match = r"\_ForwardRef\(\'(.*)\'\)"
50-
fwd_ref_replace = r"\1"
49+
fwd_ref_match = r"(.*)\_ForwardRef\(\'(.*)\'\)(.*)"
50+
fwd_ref_replace = r"\1\2\3"
51+
52+
53+
def cleanup_type(type_string: str) -> str:
54+
# Optional[T] gets expanded to Union[T, NoneType], so change it back
55+
while re.search(optional_match, type_string) is not None:
56+
type_string = re.sub(optional_match, optional_replace, type_string)
57+
58+
# _ForwardRef('MathyEnvState') -> MathyEnvState
59+
while re.search(fwd_ref_match, type_string) is not None:
60+
type_string = re.sub(fwd_ref_match, fwd_ref_replace, type_string)
61+
62+
return type_string
5163

5264

5365
def trim(docstring):
@@ -180,18 +192,16 @@ def get_callable_placeholder(
180192
default_value = str(p.default)
181193

182194
if annotation is not None:
183-
# Optional[T] gets expanded to Union[T, NoneType], so change it back
184-
while re.search(optional_match, annotation) is not None:
185-
annotation = re.sub(optional_match, optional_replace, annotation)
186-
187-
annotation = re.sub(fwd_ref_match, fwd_ref_replace, annotation)
195+
annotation = cleanup_type(annotation)
188196
params.append(CallableArg(p.name, annotation, default_value))
189197

190198
return_annotation = None
191199
if sig.return_annotation is not inspect._empty: # type: ignore
192200
return_annotation = inspect.formatannotation(
193201
sig.return_annotation, base_module="mathy"
194202
)
203+
if return_annotation is not None:
204+
return_annotation = cleanup_type(return_annotation)
195205
return CallablePlaceholder(
196206
simple=str(sig), name=name, args=params, return_type=return_annotation
197207
)

libraries/mathy_pydoc_markdown/setup.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,23 @@
2121
import setuptools
2222
import io
2323

24-
from mathy_pydoc import about
24+
from pathlib import Path
25+
26+
package_name = "mathy_pydoc"
27+
root = Path(__file__).parent.resolve()
28+
29+
# Read in package meta from about.py
30+
about_path = root / package_name / "about.py"
31+
with about_path.open("r", encoding="utf8") as f:
32+
about = {}
33+
exec(f.read(), about)
34+
2535

2636
with io.open("README.md", encoding="utf8") as fp:
2737
readme = fp.read()
2838

2939
setuptools.setup(
30-
name="mathy_pydoc",
40+
name=package_name,
3141
description=about["__summary__"],
3242
author=about["__author__"],
3343
author_email=about["__email__"],
@@ -50,11 +60,6 @@
5060
],
5161
keywords="markdown pydoc generator docs documentation",
5262
packages=["mathy_pydoc"],
53-
install_requires=[
54-
"MkDocs>=0.16.0",
55-
"Markdown>=2.6.11",
56-
"PyYAML>=3.12",
57-
"six>=0.11.0",
58-
],
63+
install_requires=["Markdown>=2.6.11", "PyYAML>=3.12", "six>=0.11.0",],
5964
entry_points=dict(console_scripts=["mathy_pydoc=mathy_pydoc.__main__:main"]),
6065
)

libraries/mathy_pydoc_markdown/testmodule/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ def a_function_with_types(
77
inputs: List[Dict[str, Any]],
88
auxiliary: Tuple[Optional[str]],
99
final: int = 3,
10-
) -> List[str]:
10+
) -> List[Optional[str]]:
1111
"""Sweet typed function"""
1212
pass
1313

0 commit comments

Comments
 (0)