Skip to content

Commit 9cb6f79

Browse files
committed
Fix tests for Python 3.9
1 parent 84654c9 commit 9cb6f79

File tree

2 files changed

+24
-14
lines changed

2 files changed

+24
-14
lines changed

flask_parameter_validation/docs_blueprint.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,19 @@ def get_arg_type_hint(fdocs, arg_name):
7676
"""
7777
arg_type = fdocs["argspec"].annotations[arg_name]
7878
def recursively_resolve_type_hint(type_to_resolve):
79+
if hasattr(type_to_resolve, "__name__"): # In Python 3.9, Optional and Union do not have __name__
80+
type_base_name = type_to_resolve.__name__
81+
elif hasattr(type_to_resolve, "_name") and type_to_resolve._name is not None:
82+
# In Python 3.9, _name exists on list[whatever] and has a non-None value
83+
type_base_name = type_to_resolve._name
84+
else:
85+
# But, in Python 3.9, Optional[whatever] has _name of None - but its __origin__ is Union
86+
type_base_name = type_to_resolve.__origin__._name
7987
if hasattr(type_to_resolve, "__args__"):
8088
return (
81-
f"{type_to_resolve.__name__}[{', '.join([recursively_resolve_type_hint(a) for a in type_to_resolve.__args__])}]"
89+
f"{type_base_name}[{', '.join([recursively_resolve_type_hint(a) for a in type_to_resolve.__args__])}]"
8290
)
83-
return type_to_resolve.__name__
91+
return type_base_name
8492
return recursively_resolve_type_hint(arg_type)
8593

8694

flask_parameter_validation/test/test_api_docs.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
import sys
12
from flask_parameter_validation.docs_blueprint import get_route_docs
23

34
def test_http_ok(client):
45
r = client.get("/docs/")
56
assert r.status_code == 200
67
r = client.get("/docs/json")
78
assert r.status_code == 200
8-
9+
import sys
910
def test_routes_added(app):
1011
routes = []
1112
for rule in app.url_map.iter_rules():
@@ -20,18 +21,19 @@ def test_doc_types_of_default(app):
2021
"query": "Query",
2122
"route": "Route"
2223
}
24+
optional_as_str = "Optional" if sys.version_info >= (3,10) else "Union"
2325
types = {
24-
"bool": {"opt": "Optional[bool, NoneType]", "n_opt": "bool"},
25-
"date": {"opt": "Optional[date, NoneType]", "n_opt": "date"},
26-
"datetime": {"opt": "Optional[datetime, NoneType]", "n_opt": "datetime"},
27-
"dict": {"opt": "Optional[dict, NoneType]", "n_opt": "dict"},
28-
"float": {"opt": "Optional[float, NoneType]", "n_opt": "float"},
29-
"int": {"opt": "Optional[int, NoneType]", "n_opt": "int"},
30-
"int_enum": {"opt": "Optional[Binary, NoneType]", "n_opt": "Binary"},
31-
"list": {"opt": "Optional[List[int], NoneType]", "n_opt": "List[str]"},
32-
"str": {"opt": "Optional[str, NoneType]", "n_opt": "str"},
33-
"str_enum": {"opt": "Optional[Fruits, NoneType]", "n_opt": "Fruits"},
34-
"time": {"opt": "Optional[time, NoneType]", "n_opt": "time"},
26+
"bool": {"opt": f"{optional_as_str}[bool, NoneType]", "n_opt": "bool"},
27+
"date": {"opt": f"{optional_as_str}[date, NoneType]", "n_opt": "date"},
28+
"datetime": {"opt": f"{optional_as_str}[datetime, NoneType]", "n_opt": "datetime"},
29+
"dict": {"opt": f"{optional_as_str}[dict, NoneType]", "n_opt": "dict"},
30+
"float": {"opt": f"{optional_as_str}[float, NoneType]", "n_opt": "float"},
31+
"int": {"opt": f"{optional_as_str}[int, NoneType]", "n_opt": "int"},
32+
"int_enum": {"opt": f"{optional_as_str}[Binary, NoneType]", "n_opt": "Binary"},
33+
"list": {"opt": f"{optional_as_str}[List[int], NoneType]", "n_opt": "List[str]"},
34+
"str": {"opt": f"{optional_as_str}[str, NoneType]", "n_opt": "str"},
35+
"str_enum": {"opt": f"{optional_as_str}[Fruits, NoneType]", "n_opt": "Fruits"},
36+
"time": {"opt": f"{optional_as_str}[time, NoneType]", "n_opt": "time"},
3537
"union": {"opt": "Union[bool, int, NoneType]", "n_opt": "Union[bool, int]"}
3638
}
3739
route_unsupported_types = ["dict", "list"]

0 commit comments

Comments
 (0)