Skip to content

Commit

Permalink
reworked parse_param_type() function to increase performance, general…
Browse files Browse the repository at this point in the history
…ity and properly handle types: Union[], __main__.SomeCustomClass
  • Loading branch information
ds-jakub-cierocki committed Jul 4, 2024
1 parent 5fd802f commit 7a49b4d
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/dbally/views/exposed_functions.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import re
import typing_extensions as type_ext
from dataclasses import dataclass
from typing import _GenericAlias # type: ignore
from typing import Optional, Sequence, Type, Union
from inspect import isclass

from dbally.context.context import BaseCallerContext
from dbally.similarity import AbstractSimilarityIndex
Expand All @@ -12,15 +14,22 @@ def parse_param_type(param_type: Union[type, _GenericAlias]) -> str:
Parses the type of a method parameter and returns a string representation of it.
Args:
param_type: type of the parameter
param_type: Type of the parameter.
Returns:
str: string representation of the type
A string representation of the type.
"""
if param_type in {int, float, str, bool, list, dict, set, tuple}:

# TODO consider using hasattr() to ensure correctness of the IF's below
if isclass(param_type):
return param_type.__name__

if type_ext.get_origin(param_type) is Union:
args_str_repr = ', '.join(parse_param_type(arg) for arg in type_ext.get_args(param_type))
return f"Union[{args_str_repr}]"

if param_type.__module__ == "typing":
return re.sub(r"\btyping\.", "", str(param_type))
return param_type._name

return str(param_type)

Expand Down

0 comments on commit 7a49b4d

Please sign in to comment.