diff --git a/tests/manual_verification.py b/tests/manual_verification.py new file mode 100644 index 000000000..b4de092bf --- /dev/null +++ b/tests/manual_verification.py @@ -0,0 +1,48 @@ +import json + +from pydantic import parse_obj_as +from typing import Any, Optional +from pydantic import BaseModel, Field + + +class MyBaseModel(BaseModel): + name = "base" + + +class MyPlanModel(MyBaseModel): + name: str = "Plan model" + description: Optional[str] = Field( + description="Docstring of the plan", default=None + ) + parameter_schema: Optional[dict[str, Any]] = Field( + description="Schema of the plan's parameters", + alias="schema", + default_factory=dict, + ) + + +plan_model: MyPlanModel = MyPlanModel() +plan_model.parameter_schema = { + "title": "sleep", + "type": "object", + "properties": {"time": {"title": "Time", "type": "number"}}, + "required": ["time"], + "additionalProperties": False, +} + + +print(plan_model.dict()) + + +wrong_params: str = '{"wrong":"aboslutely wrong"}' + + +p = json.loads(wrong_params) or "{}" + + +try: + validated = parse_obj_as(type(plan_model.parameter_schema), p) + print(validated) +except ValueError as e: + print(f"Error: {e}") + validated = None diff --git a/tests/pydantic_verifiication.ipynb b/tests/pydantic_verifiication.ipynb new file mode 100644 index 000000000..4652e9bd4 --- /dev/null +++ b/tests/pydantic_verifiication.ipynb @@ -0,0 +1,136 @@ +{ + "cells": [ + { + "cell_type": "code", + "execution_count": 10, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'name': 'Plan model', 'description': None, 'parameter_schema': {'title': 'sleep', 'type': 'object', 'properties': {'time': {'title': 'Time', 'type': 'number'}}, 'required': ['time'], 'additionalProperties': False}}\n" + ] + } + ], + "source": [ + "from typing import Any, Optional\n", + "from pydantic import BaseModel, Field\n", + "class MyBaseModel(BaseModel):\n", + " name ='base'\n", + "\n", + "class MyPlanModel(MyBaseModel):\n", + " name:str = 'Plan model'\n", + " description: Optional[str] = Field(\n", + " description=\"Docstring of the plan\", default=None\n", + " )\n", + " parameter_schema: Optional[dict[str, Any]] = Field(\n", + " description=\"Schema of the plan's parameters\",\n", + " alias=\"schema\",\n", + " default_factory=dict,\n", + " )\n", + "\n", + "\n", + "plan_model: MyPlanModel = MyPlanModel()\n", + "plan_model.parameter_schema = {\"title\":\"sleep\",\"type\":\"object\",\"properties\":{\"time\":{\"title\":\"Time\",\"type\":\"number\"}},\"required\":[\"time\"],\"additionalProperties\":False}\n", + "\n", + "# '{\"name\":\"sleep\",\"description\":\"\\\\n Suspend all action for a given time, wrapper for `bp.sleep`\\\\n\\\\n Args:\\\\n time (float): Time to wait in seconds\\\\n\\\\n Returns:\\\\n MsgGenerator: Plan\\\\n\\\\n Yields:\\\\n Iterator[MsgGenerator]: Bluesky messages\\\\n \",\n", + "# \"schema\":{\"title\":\"sleep\",\"type\":\"object\",\"properties\":{\"time\":{\"title\":\"Time\",\"type\":\"number\"}},\"required\":[\"time\"],\"additionalProperties\":false}}'\n", + "\n", + "\n", + "\n", + "print(plan_model.dict())\n" + ] + }, + { + "cell_type": "code", + "execution_count": 12, + "metadata": {}, + "outputs": [], + "source": [ + "import json\n", + "\n", + "from pydantic import parse_obj_as\n", + "\n" + ] + }, + { + "cell_type": "code", + "execution_count": 13, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'wrong': 'aboslutely wrong'}\n" + ] + } + ], + "source": [ + "\n", + "wrong_params:str = '{\"wrong\":\"aboslutely wrong\"}'\n", + "\n", + "\n", + "p = json.loads(wrong_params) or \"{}\"\n", + "\n", + "\n", + "\n", + "try:\n", + " validated = parse_obj_as(type(plan_model.parameter_schema),p)\n", + " print(validated)\n", + "except ValueError as e:\n", + " print(f\"Error: {e}\")\n", + " validated = None\n" + ] + }, + { + "cell_type": "code", + "execution_count": 5, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "{'time': 5}\n" + ] + } + ], + "source": [ + "\n", + "params_str:str = '{\"time\":5}'\n", + "parameters = json.loads(params_str) or \"{}\"\n", + "\n", + "try:\n", + " validated = parse_obj_as(type(plan_model.parameter_schema), parameters)\n", + " print(validated)\n", + "except ValueError as e:\n", + " print(f\"Error: {e}\")\n", + " validated = None\n", + "\n" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "venv", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.11.7" + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}