Get the parameters of a function or class from source code. Args: object (Union[Type['T'], Callable]): The function or class to get parameters for
(self, object: Union[Type['T'], Callable])
| 773 | return list(self._loaded_modules.keys()) |
| 774 | |
| 775 | def get_parameters(self, object: Union[Type['T'], Callable]) -> Dict[str, Any]: |
| 776 | """Get the parameters of a function or class from source code. |
| 777 | |
| 778 | Args: |
| 779 | object (Union[Type['T'], Callable]): The function or class to get parameters for |
| 780 | """ |
| 781 | try: |
| 782 | if isinstance(object, Type): |
| 783 | signature = inspect.signature(object.__call__) |
| 784 | hints = get_type_hints(object.__call__) |
| 785 | docstring = inspect.getdoc(object.__call__) or "" |
| 786 | elif isinstance(object, Callable): |
| 787 | signature = inspect.signature(object) |
| 788 | hints = get_type_hints(object) |
| 789 | docstring = inspect.getdoc(object) or "" |
| 790 | except Exception as e: |
| 791 | raise ValueError(f"Failed to get parameters for {object}: {e}") |
| 792 | |
| 793 | # Get descriptions |
| 794 | doc_descriptions = self.parse_docstring_descriptions(docstring) |
| 795 | |
| 796 | properties = {} |
| 797 | required = [] |
| 798 | |
| 799 | for name, param in signature.parameters.items(): |
| 800 | if name == "self": |
| 801 | continue |
| 802 | |
| 803 | # Skip generic "input" parameter |
| 804 | if name == "input" and len(signature.parameters) == 2: # self + input |
| 805 | continue |
| 806 | |
| 807 | # Skip VAR_KEYWORD (**kwargs) and VAR_POSITIONAL (*args) parameters |
| 808 | if param.kind in (inspect.Parameter.VAR_POSITIONAL, inspect.Parameter.VAR_KEYWORD): |
| 809 | continue |
| 810 | |
| 811 | # Get type annotation |
| 812 | annotation = hints.get(name, param.annotation) |
| 813 | json_type, python_type = self.annotation_to_types(annotation) |
| 814 | |
| 815 | # Determine if required |
| 816 | is_required = param.default is inspect._empty |
| 817 | |
| 818 | # Build schema |
| 819 | schema: Dict[str, Any] = { |
| 820 | "type": json_type, |
| 821 | "description": doc_descriptions.get(name, ""), |
| 822 | } |
| 823 | schema[PYTHON_TYPE_FIELD] = python_type |
| 824 | |
| 825 | if not is_required: |
| 826 | schema["default"] = param.default |
| 827 | |
| 828 | properties[name] = schema |
| 829 | if is_required: |
| 830 | required.append(name) |
| 831 | |
| 832 | if not properties: |
no test coverage detected