|
1049 | 1049 | ]
|
1050 | 1050 | },
|
1051 | 1051 | {
|
1052 |
| - "cell_type": "code", |
1053 |
| - "execution_count": null, |
1054 |
| - "id": "ac8f3d19", |
| 1052 | + "cell_type": "markdown", |
| 1053 | + "id": "369320d4", |
1055 | 1054 | "metadata": {},
|
1056 |
| - "outputs": [ |
1057 |
| - { |
1058 |
| - "data": { |
1059 |
| - "text/plain": [ |
1060 |
| - "{'name': '_optional_test',\n", |
1061 |
| - " 'description': 'Schema test for Optional / Union\\n\\nReturns:\\n- type: string',\n", |
1062 |
| - " 'input_schema': {'type': 'object',\n", |
1063 |
| - " 'properties': {'opt_tup': {'type': 'object',\n", |
1064 |
| - " 'description': '',\n", |
1065 |
| - " 'default': None,\n", |
1066 |
| - " 'anyOf': [{'type': 'array'}, {'type': 'null'}]}},\n", |
1067 |
| - " 'title': None}}" |
1068 |
| - ] |
1069 |
| - }, |
1070 |
| - "execution_count": null, |
1071 |
| - "metadata": {}, |
1072 |
| - "output_type": "execute_result" |
1073 |
| - } |
1074 |
| - ], |
1075 | 1055 | "source": [
|
1076 |
| - "def _optional_test(opt_tup: Optional[Tuple[int, int]]=None) -> str:\n", |
1077 |
| - " \"Schema test for Optional / Union\"\n", |
1078 |
| - " return \"\"\n", |
1079 |
| - "get_schema(_optional_test)" |
| 1056 | + "### Additional `get_schema()` Test Cases" |
| 1057 | + ] |
| 1058 | + }, |
| 1059 | + { |
| 1060 | + "cell_type": "markdown", |
| 1061 | + "id": "a8052380", |
| 1062 | + "metadata": {}, |
| 1063 | + "source": [ |
| 1064 | + "Union types are approximately mapped to JSON schema 'anyOf' with two or more value types." |
1080 | 1065 | ]
|
1081 | 1066 | },
|
1082 | 1067 | {
|
1083 | 1068 | "cell_type": "code",
|
1084 | 1069 | "execution_count": null,
|
1085 |
| - "id": "825769a5", |
| 1070 | + "id": "6fc1d6f9", |
1086 | 1071 | "metadata": {},
|
1087 | 1072 | "outputs": [
|
1088 | 1073 | {
|
1089 | 1074 | "data": {
|
1090 | 1075 | "text/plain": [
|
1091 | 1076 | "{'name': '_union_test',\n",
|
1092 |
| - " 'description': 'Schema test for Union\\n\\nReturns:\\n- type: string',\n", |
| 1077 | + " 'description': 'Mandatory docstring',\n", |
1093 | 1078 | " 'input_schema': {'type': 'object',\n",
|
1094 |
| - " 'properties': {'opt_tup': {'type': 'array',\n", |
| 1079 | + " 'properties': {'opt_tup': {'type': 'object',\n", |
1095 | 1080 | " 'description': '',\n",
|
1096 |
| - " 'items': {'type': 'integer'},\n", |
1097 |
| - " 'default': None}},\n", |
| 1081 | + " 'default': None,\n", |
| 1082 | + " 'anyOf': [{'type': 'array'}, {'type': 'string'}, {'type': 'integer'}]}},\n", |
1098 | 1083 | " 'title': None}}"
|
1099 | 1084 | ]
|
1100 | 1085 | },
|
|
1104 | 1089 | }
|
1105 | 1090 | ],
|
1106 | 1091 | "source": [
|
1107 |
| - "def _union_test(opt_tup: Union[Tuple[int, int]]=None) -> str:\n", |
1108 |
| - " \"Schema test for Union\"\n", |
| 1092 | + "def _union_test(opt_tup: Union[Tuple[int, int], str, int]=None):\n", |
| 1093 | + " \"Mandatory docstring\"\n", |
1109 | 1094 | " return \"\"\n",
|
1110 | 1095 | "get_schema(_union_test)"
|
1111 | 1096 | ]
|
1112 | 1097 | },
|
| 1098 | + { |
| 1099 | + "cell_type": "markdown", |
| 1100 | + "id": "7641aca8", |
| 1101 | + "metadata": {}, |
| 1102 | + "source": [ |
| 1103 | + "The new (Python 3.10+) union syntax can also be used, producing an equivalent schema." |
| 1104 | + ] |
| 1105 | + }, |
1113 | 1106 | {
|
1114 | 1107 | "cell_type": "code",
|
1115 | 1108 | "execution_count": null,
|
1116 |
| - "id": "df9b9d13", |
| 1109 | + "id": "a1a11b3b", |
1117 | 1110 | "metadata": {},
|
1118 | 1111 | "outputs": [
|
1119 | 1112 | {
|
1120 | 1113 | "data": {
|
1121 | 1114 | "text/plain": [
|
1122 | 1115 | "{'name': '_new_union_test',\n",
|
1123 |
| - " 'description': 'Schema test for Union with the > 3.10 syntax\\n\\nReturns:\\n- type: string',\n", |
| 1116 | + " 'description': 'Mandatory docstring',\n", |
1124 | 1117 | " 'input_schema': {'type': 'object',\n",
|
1125 | 1118 | " 'properties': {'opt_tup': {'type': 'object',\n",
|
1126 | 1119 | " 'description': '',\n",
|
1127 |
| - " 'default': None}},\n", |
| 1120 | + " 'default': None,\n", |
| 1121 | + " 'anyOf': [{'type': 'array'}, {'type': 'string'}, {'type': 'integer'}]}},\n", |
1128 | 1122 | " 'title': None}}"
|
1129 | 1123 | ]
|
1130 | 1124 | },
|
|
1134 | 1128 | }
|
1135 | 1129 | ],
|
1136 | 1130 | "source": [
|
1137 |
| - "def _new_union_test(opt_tup: str | None =None) -> str:\n", |
1138 |
| - " \"Schema test for Union with the > 3.10 syntax\"\n", |
1139 |
| - " return \"\"\n", |
| 1131 | + "def _new_union_test(opt_tup: Tuple[int, int] | str | int =None):\n", |
| 1132 | + " \"Mandatory docstring\"\n", |
| 1133 | + " pass\n", |
1140 | 1134 | "get_schema(_new_union_test)"
|
1141 | 1135 | ]
|
1142 | 1136 | },
|
| 1137 | + { |
| 1138 | + "cell_type": "markdown", |
| 1139 | + "id": "8d24cc0a", |
| 1140 | + "metadata": {}, |
| 1141 | + "source": [ |
| 1142 | + "Optional is a special case of union types, limited to two types, one of which is None (mapped to null in JSON schema):" |
| 1143 | + ] |
| 1144 | + }, |
1143 | 1145 | {
|
1144 | 1146 | "cell_type": "code",
|
1145 | 1147 | "execution_count": null,
|
1146 |
| - "id": "41ad5756", |
| 1148 | + "id": "ac8f3d19", |
1147 | 1149 | "metadata": {},
|
1148 | 1150 | "outputs": [
|
1149 | 1151 | {
|
1150 | 1152 | "data": {
|
1151 | 1153 | "text/plain": [
|
1152 |
| - "{'name': '_none_test',\n", |
1153 |
| - " 'description': 'Schema test for NoneType.',\n", |
| 1154 | + "{'name': '_optional_test',\n", |
| 1155 | + " 'description': 'Mandatory docstring',\n", |
1154 | 1156 | " 'input_schema': {'type': 'object',\n",
|
1155 |
| - " 'properties': {'none': {'type': 'null', 'description': ''}},\n", |
1156 |
| - " 'title': None,\n", |
1157 |
| - " 'required': ['none']}}" |
| 1157 | + " 'properties': {'opt_tup': {'type': 'object',\n", |
| 1158 | + " 'description': '',\n", |
| 1159 | + " 'default': None,\n", |
| 1160 | + " 'anyOf': [{'type': 'array'}, {'type': 'null'}]}},\n", |
| 1161 | + " 'title': None}}" |
1158 | 1162 | ]
|
1159 | 1163 | },
|
1160 | 1164 | "execution_count": null,
|
|
1163 | 1167 | }
|
1164 | 1168 | ],
|
1165 | 1169 | "source": [
|
1166 |
| - "def _none_test(none: type(None)):\n", |
1167 |
| - " \"Schema test for NoneType.\"\n", |
| 1170 | + "def _optional_test(opt_tup: Optional[Tuple[int, int]]=None):\n", |
| 1171 | + " \"Mandatory docstring\"\n", |
1168 | 1172 | " pass\n",
|
1169 |
| - "get_schema(_none_test)" |
| 1173 | + "get_schema(_optional_test)" |
| 1174 | + ] |
| 1175 | + }, |
| 1176 | + { |
| 1177 | + "cell_type": "markdown", |
| 1178 | + "id": "c969721b", |
| 1179 | + "metadata": {}, |
| 1180 | + "source": [ |
| 1181 | + "Containers can also be used, both in their parameterized form (`List[int]`) or as their unparameterized raw type (`List`). In the latter case, the item type is mapped to `object` in JSON schema." |
1170 | 1182 | ]
|
1171 | 1183 | },
|
1172 | 1184 | {
|
|
1179 | 1191 | "data": {
|
1180 | 1192 | "text/plain": [
|
1181 | 1193 | "{'name': '_list_test',\n",
|
1182 |
| - " 'description': 'Schema test for List\\n\\nReturns:\\n- type: string',\n", |
| 1194 | + " 'description': 'Mandatory docstring',\n", |
1183 | 1195 | " 'input_schema': {'type': 'object',\n",
|
1184 | 1196 | " 'properties': {'l': {'type': 'array',\n",
|
1185 | 1197 | " 'description': '',\n",
|
|
1194 | 1206 | }
|
1195 | 1207 | ],
|
1196 | 1208 | "source": [
|
1197 |
| - "def _list_test(l: List[int]) -> str:\n", |
1198 |
| - " \"Schema test for List\"\n", |
1199 |
| - " return \"\"\n", |
| 1209 | + "def _list_test(l: List[int]):\n", |
| 1210 | + " \"Mandatory docstring\"\n", |
| 1211 | + " pass\n", |
1200 | 1212 | "get_schema(_list_test)"
|
1201 | 1213 | ]
|
1202 | 1214 | },
|
|
1210 | 1222 | "data": {
|
1211 | 1223 | "text/plain": [
|
1212 | 1224 | "{'name': '_raw_list_test',\n",
|
1213 |
| - " 'description': 'Schema test for List\\n\\nReturns:\\n- type: string',\n", |
| 1225 | + " 'description': 'Mandatory docstring',\n", |
1214 | 1226 | " 'input_schema': {'type': 'object',\n",
|
1215 | 1227 | " 'properties': {'l': {'type': 'array',\n",
|
1216 | 1228 | " 'description': '',\n",
|
|
1225 | 1237 | }
|
1226 | 1238 | ],
|
1227 | 1239 | "source": [
|
1228 |
| - "def _raw_list_test(l: List) -> str:\n", |
1229 |
| - " \"Schema test for List\"\n", |
1230 |
| - " return \"\"\n", |
| 1240 | + "def _raw_list_test(l: List):\n", |
| 1241 | + " \"Mandatory docstring\"\n", |
| 1242 | + " pass\n", |
1231 | 1243 | "get_schema(_raw_list_test)"
|
1232 | 1244 | ]
|
1233 | 1245 | },
|
| 1246 | + { |
| 1247 | + "cell_type": "markdown", |
| 1248 | + "id": "5704c197", |
| 1249 | + "metadata": {}, |
| 1250 | + "source": [ |
| 1251 | + "The same applies to dictionary, which can similarly be parameterized with key/value types or specified as a raw type." |
| 1252 | + ] |
| 1253 | + }, |
1234 | 1254 | {
|
1235 | 1255 | "cell_type": "code",
|
1236 | 1256 | "execution_count": null,
|
|
1241 | 1261 | "data": {
|
1242 | 1262 | "text/plain": [
|
1243 | 1263 | "{'name': '_dict_test',\n",
|
1244 |
| - " 'description': 'Schema test for Dict.',\n", |
| 1264 | + " 'description': 'Mandatory docstring',\n", |
1245 | 1265 | " 'input_schema': {'type': 'object',\n",
|
1246 | 1266 | " 'properties': {'d': {'type': 'object',\n",
|
1247 | 1267 | " 'description': '',\n",
|
|
1256 | 1276 | }
|
1257 | 1277 | ],
|
1258 | 1278 | "source": [
|
1259 |
| - "def _dict_test(d: Dict[int, int]):\n", |
1260 |
| - " \"Schema test for Dict.\"\n", |
| 1279 | + "def _dict_test(d: Dict[str, int]):\n", |
| 1280 | + " \"Mandatory docstring\"\n", |
1261 | 1281 | " pass\n",
|
1262 | 1282 | "get_schema(_dict_test)"
|
1263 | 1283 | ]
|
|
1272 | 1292 | "data": {
|
1273 | 1293 | "text/plain": [
|
1274 | 1294 | "{'name': '_raw_dict_test',\n",
|
1275 |
| - " 'description': 'Schema test for Dict.',\n", |
| 1295 | + " 'description': 'Mandatory docstring',\n", |
1276 | 1296 | " 'input_schema': {'type': 'object',\n",
|
1277 | 1297 | " 'properties': {'d': {'type': 'object', 'description': ''}},\n",
|
1278 | 1298 | " 'title': None,\n",
|
|
1286 | 1306 | ],
|
1287 | 1307 | "source": [
|
1288 | 1308 | "def _raw_dict_test(d: Dict):\n",
|
1289 |
| - " \"Schema test for Dict.\"\n", |
1290 |
| - " pass\n", |
| 1309 | + " \"Mandatory docstring\"\n", |
1291 | 1310 | "get_schema(_raw_dict_test)"
|
1292 | 1311 | ]
|
1293 | 1312 | },
|
|
0 commit comments