Skip to content

Commit d08ed14

Browse files
author
Seth Teichman
committed
Tidy type hint resolution in API docs
1 parent 3cc5a72 commit d08ed14

File tree

1 file changed

+20
-5
lines changed

1 file changed

+20
-5
lines changed

Diff for: flask_parameter_validation/docs_blueprint.py

+20-5
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from enum import Enum
12
import flask
23
from flask import Blueprint, current_app, jsonify
34

@@ -74,11 +75,13 @@ def get_arg_type_hint(fdocs, arg_name):
7475
Extract the type hint for a specific argument.
7576
"""
7677
arg_type = fdocs["argspec"].annotations[arg_name]
77-
if hasattr(arg_type, "__args__"):
78-
return (
79-
f"{arg_type.__name__}[{', '.join([a.__name__ for a in arg_type.__args__])}]"
80-
)
81-
return arg_type.__name__
78+
def recursively_resolve_type_hint(type_to_resolve):
79+
if hasattr(type_to_resolve, "__args__"):
80+
return (
81+
f"{type_to_resolve.__name__}[{', '.join([recursively_resolve_type_hint(a) for a in type_to_resolve.__args__])}]"
82+
)
83+
return type_to_resolve.__name__
84+
return recursively_resolve_type_hint(arg_type)
8285

8386

8487
def get_arg_location(fdocs, idx):
@@ -98,6 +101,18 @@ def get_arg_location_details(fdocs, idx):
98101
if value is not None:
99102
if callable(value):
100103
loc_details[param] = f"{value.__module__}.{value.__name__}"
104+
elif issubclass(type(value), Enum):
105+
loc_details[param] = f"{type(value).__name__}.{value.name}: "
106+
if issubclass(type(value), int):
107+
loc_details[param] += f"{value.value}"
108+
elif issubclass(type(value), str):
109+
loc_details[param] += f"'{value.value}'"
110+
else:
111+
loc_details[param] = f"FPV: Unsupported Enum type"
112+
elif type(value).__name__ == 'time':
113+
loc_details[param] = value.isoformat()
114+
elif param == 'sources':
115+
loc_details[param] = [type(source).__name__ for source in value]
101116
else:
102117
loc_details[param] = value
103118
return loc_details

0 commit comments

Comments
 (0)