Use `from_config` to obtain explicit arguments. Returns: dict: arguments to be used for cls.__init__
(from_config_func, *args, **kwargs)
| 110 | return False |
| 111 | |
| 112 | def _get_args_from_config(from_config_func, *args, **kwargs): |
| 113 | """ |
| 114 | Use `from_config` to obtain explicit arguments. |
| 115 | |
| 116 | Returns: |
| 117 | dict: arguments to be used for cls.__init__ |
| 118 | """ |
| 119 | signature = inspect.signature(from_config_func) |
| 120 | if list(signature.parameters.keys())[0] != "cfg": |
| 121 | if inspect.isfunction(from_config_func): |
| 122 | name = from_config_func.__name__ |
| 123 | else: |
| 124 | name = f"{from_config_func.__self__}.from_config" |
| 125 | raise TypeError(f"{name} must take 'cfg' as the first argument!") |
| 126 | support_var_arg = any( |
| 127 | param.kind in [param.VAR_POSITIONAL, param.VAR_KEYWORD] |
| 128 | for param in signature.parameters.values() |
| 129 | ) |
| 130 | if support_var_arg: # forward all arguments to from_config, if from_config accepts them |
| 131 | ret = from_config_func(*args, **kwargs) |
| 132 | else: |
| 133 | # forward supported arguments to from_config |
| 134 | supported_arg_names = set(signature.parameters.keys()) |
| 135 | extra_kwargs = {} |
| 136 | for name in list(kwargs.keys()): |
| 137 | if name not in supported_arg_names: |
| 138 | extra_kwargs[name] = kwargs.pop(name) |
| 139 | ret = from_config_func(*args, **kwargs) |
| 140 | # forward the other arguments to __init__ |
| 141 | ret.update(extra_kwargs) |
| 142 | return ret |