|
736 | 736 | { |
737 | 737 | "cell_type": "code", |
738 | 738 | "execution_count": null, |
739 | | - "id": "23f54386", |
| 739 | + "id": "748da965", |
740 | 740 | "metadata": {}, |
741 | 741 | "outputs": [], |
742 | 742 | "source": [ |
743 | | - "#| exports\n", |
| 743 | + "#| export\n", |
744 | 744 | "def get_schema(\n", |
745 | 745 | " f:Union[callable,dict], # Function to get schema for\n", |
746 | 746 | " pname='input_schema', # Key name for parameters\n", |
|
754 | 754 | " assert desc, \"Docstring missing!\"\n", |
755 | 755 | " d = docments(f, full=True)\n", |
756 | 756 | " ret = d.pop('return')\n", |
757 | | - " if (ret.anno is not empty) and (ret.anno is not None): desc += f'\\n\\nReturns:\\n- type: {_types(ret.anno)[0]}'\n", |
| 757 | + " has_type = (ret.anno is not empty) and (ret.anno is not None)\n", |
| 758 | + " has_doc = ret.docment\n", |
| 759 | + " if has_type or has_doc:\n", |
| 760 | + " type_str = f'type: {_types(ret.anno)[0]}'\n", |
| 761 | + " ret_str = f'{ret.docment} ({type_str})' if has_type and has_doc else (type_str if has_type else ret.docment)\n", |
| 762 | + " desc += f'\\n\\nReturns:\\n- {ret_str}'\n", |
758 | 763 | " return {\"name\": f.__name__, \"description\": desc, pname: schema}" |
759 | 764 | ] |
760 | 765 | }, |
|
768 | 773 | "data": { |
769 | 774 | "text/plain": [ |
770 | 775 | "{'name': 'get_schema',\n", |
771 | | - " 'description': 'Generate JSON schema for a class, function, or method\\n\\nReturns:\\n- type: object',\n", |
| 776 | + " 'description': \"Generate JSON schema for a class, function, or method\\n\\nReturns:\\n- {'name':..., 'description':..., pname:...} (type: object)\",\n", |
772 | 777 | " 'input_schema': {'type': 'object',\n", |
773 | 778 | " 'properties': {'f': {'type': 'object',\n", |
774 | 779 | " 'description': 'Function to get schema for',\n", |
|
823 | 828 | "Adds a + b.\n", |
824 | 829 | "\n", |
825 | 830 | "Returns:\n", |
826 | | - "- type: integer\n" |
| 831 | + "- The sum of the inputs (type: integer)\n" |
827 | 832 | ] |
828 | 833 | }, |
829 | 834 | { |
|
872 | 877 | "data": { |
873 | 878 | "text/plain": [ |
874 | 879 | "{'name': 'silly_test',\n", |
875 | | - " 'description': 'Mandatory docstring',\n", |
| 880 | + " 'description': 'Mandatory docstring\\n\\nReturns:\\n- type: integer',\n", |
876 | 881 | " 'input_schema': {'type': 'object',\n", |
877 | 882 | " 'properties': {'a': {'type': 'integer', 'description': 'quoted type hint'}},\n", |
878 | 883 | " 'required': ['a']}}" |
|
886 | 891 | "source": [ |
887 | 892 | "def silly_test(\n", |
888 | 893 | " a: 'int', # quoted type hint\n", |
889 | | - "):\n", |
| 894 | + ")->int:\n", |
890 | 895 | " \"Mandatory docstring\"\n", |
891 | 896 | " return a\n", |
892 | 897 | "\n", |
|
911 | 916 | "data": { |
912 | 917 | "text/plain": [ |
913 | 918 | "{'name': 'sums',\n", |
914 | | - " 'description': 'Adds a + b.\\n\\nReturns:\\n- type: integer',\n", |
| 919 | + " 'description': 'Adds a + b.',\n", |
915 | 920 | " 'input_schema': {'type': 'object',\n", |
916 | 921 | " 'properties': {'a': {'type': 'integer', 'description': 'First thing to sum'},\n", |
917 | 922 | " 'b': {'type': 'integer',\n", |
|
931 | 936 | " self,\n", |
932 | 937 | " a:int, # First thing to sum\n", |
933 | 938 | " b:int=1 # Second thing to sum\n", |
934 | | - " ) -> int: # The sum of the inputs\n", |
| 939 | + " ): # The sum of the inputs\n", |
935 | 940 | " \"Adds a + b.\"\n", |
936 | 941 | " print(f\"Finding the sum of {a} and {b}\")\n", |
937 | 942 | " return a + b\n", |
|
0 commit comments