Extract parameter info from a function's signature and type hints.
(fn)
| 141 | |
| 142 | |
| 143 | def _extract_params(fn) -> list[dict]: |
| 144 | """Extract parameter info from a function's signature and type hints.""" |
| 145 | sig = inspect.signature(fn) |
| 146 | try: |
| 147 | hints = typing.get_type_hints(fn) |
| 148 | except Exception: |
| 149 | hints = {} |
| 150 | |
| 151 | params = [] |
| 152 | for name, param in sig.parameters.items(): |
| 153 | if name == "file" or name == "self": |
| 154 | continue |
| 155 | info = {"name": name} |
| 156 | if name in hints: |
| 157 | info["type"] = _format_type_hint(hints[name]) |
| 158 | if param.default is not inspect.Parameter.empty: |
| 159 | info["default"] = _serialize_default(param.default) |
| 160 | else: |
| 161 | info["required"] = True |
| 162 | params.append(info) |
| 163 | return params |
| 164 | |
| 165 | |
| 166 | def _format_type_hint(hint) -> str | None: |
no test coverage detected