Skip to content

Commit 3c87020

Browse files
reworked parse_param_type() function to increase performance, generality and properly handle types: Union[], __main__.SomeCustomClass
1 parent 5fd802f commit 3c87020

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

src/dbally/views/exposed_functions.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
import re
21
from dataclasses import dataclass
2+
from inspect import isclass
33
from typing import _GenericAlias # type: ignore
44
from typing import Optional, Sequence, Type, Union
55

6+
import typing_extensions as type_ext
7+
68
from dbally.context.context import BaseCallerContext
79
from dbally.similarity import AbstractSimilarityIndex
810

@@ -12,15 +14,22 @@ def parse_param_type(param_type: Union[type, _GenericAlias]) -> str:
1214
Parses the type of a method parameter and returns a string representation of it.
1315
1416
Args:
15-
param_type: type of the parameter
17+
param_type: Type of the parameter.
1618
1719
Returns:
18-
str: string representation of the type
20+
A string representation of the type.
1921
"""
20-
if param_type in {int, float, str, bool, list, dict, set, tuple}:
22+
23+
# TODO consider using hasattr() to ensure correctness of the IF's below
24+
if isclass(param_type):
2125
return param_type.__name__
26+
27+
if type_ext.get_origin(param_type) is Union:
28+
args_str_repr = ", ".join(parse_param_type(arg) for arg in type_ext.get_args(param_type))
29+
return f"Union[{args_str_repr}]"
30+
2231
if param_type.__module__ == "typing":
23-
return re.sub(r"\btyping\.", "", str(param_type))
32+
return param_type._name # pylint: disable=protected-access
2433

2534
return str(param_type)
2635

0 commit comments

Comments
 (0)