Skip to content

Instantly share code, notes, and snippets.

@schlerp
Last active May 25, 2021 23:46
Show Gist options
  • Save schlerp/a8b46dd4b94ec626b2a70340c8236ebc to your computer and use it in GitHub Desktop.
Save schlerp/a8b46dd4b94ec626b2a70340c8236ebc to your computer and use it in GitHub Desktop.
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