Last active
May 25, 2021 23:46
-
-
Save schlerp/a8b46dd4b94ec626b2a70340c8236ebc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TYPE_MAP = { | |
str: "str", | |
float: "float", | |
int: "int", | |
bool: "bool", | |
inspect._empty: "str", # the default return when type is undefined | |
List[str]: "array", | |
List[int]: "array", | |
List[float]: "array", | |
List[bool]: "array", | |
List[Any]: "array", | |
} | |
EMPTY_MAP = {"str": "", "float": 0.0, "int": 0, "bool": False, "array": []} | |
def get_init_args_from_class(_class: Any) -> Iterator[Dict[str, str]]: | |
class_init_sig = inspect.signature(_class.__init__) | |
for name, param in class_init_sig.parameters.items(): | |
if name != "self": | |
_type = TYPE_MAP.get(param.annotation, "str") | |
default_val = param.default if param.default != inspect._empty else None | |
if not default_val: | |
default_val = EMPTY_MAP[_type] | |
yield { | |
"name": name, | |
"type": _type, | |
"value": default_val, | |
"label": name.replace("_", " ").title(), | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment