From b44b119618f582ac84eb72f4663f77289a563f00 Mon Sep 17 00:00:00 2001 From: Giles Knap Date: Mon, 17 Jul 2023 19:49:57 +0000 Subject: [PATCH] fix pydantic test broken yaml file --- examples/test_refs3.py | 38 ++++++++++++++++------- tests/samples/pydantic/test.ibek.ioc.yaml | 2 +- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/examples/test_refs3.py b/examples/test_refs3.py index c6607aa4a..6b7c0935b 100644 --- a/examples/test_refs3.py +++ b/examples/test_refs3.py @@ -16,7 +16,7 @@ class Entity(BaseModel): - type: Literal["e"] + type: str = Field(description="The type of this entity") name: str = Field(..., description="The name of this entity") value: str = Field(..., description="The value of this entity") ref: Optional[str] = Field( @@ -31,6 +31,14 @@ def add_ibek_attributes(cls, entity: Entity): return entity +class Entity1(Entity): + type: Literal["e1"] = Field(description="The type of this entity") + + +class Entity2(Entity): + type: Literal["e2"] = Field(description="The type of this entity") + + @field_validator("ref", mode="after") def lookup_instance(cls, id): try: @@ -41,32 +49,38 @@ def lookup_instance(cls, id): validators = {"Entity": lookup_instance} -# add validator to the Entity class using create model -Entity2 = create_model( - "Entity", +# add validator to the Entity classes using create model +EntityOne = create_model( + "EntityOne", + __validators__=validators, + __base__=Entity1, +) # type: ignore + +EntityTwo = create_model( + "EntityTwo", __validators__=validators, - __base__=Entity, + __base__=Entity2, ) # type: ignore -entity_models = [Entity2, Entity2] +entity_models = (EntityOne, EntityTwo) class EntityModel(RootModel): - root: Union[tuple(entity_models)] = Field(discriminator="type") # type: ignore + root: Union[entity_models] = Field(discriminator="type") # type: ignore class Entities(BaseModel): model_config = ConfigDict(extra="forbid") entities: Sequence[EntityModel] = Field( # type: ignore - description="List of entities this IOC instantiates" + description="List of entities classes we want to create" ) model1 = Entities( **{ "entities": [ - {"type": "e", "name": "one", "value": "OneValue"}, - {"type": "e", "name": "two", "value": "TwoValue", "ref": "one"}, + {"type": "e1", "name": "one", "value": "OneValue"}, + {"type": "e2", "name": "two", "value": "TwoValue", "ref": "one"}, ] } ) @@ -78,13 +92,13 @@ class Entities(BaseModel): model2 = Entities( **{ "entities": [ + {"type": "e2", "name": "two", "value": "TwoValue", "ref": "one"}, { - "type": "e", + "type": "e1", "name": "one", "value": "OneValue", "illegal": "bad argument", }, - {"type": "e", "name": "two", "value": "TwoValue", "ref": "one"}, ] } ) diff --git a/tests/samples/pydantic/test.ibek.ioc.yaml b/tests/samples/pydantic/test.ibek.ioc.yaml index 398bc42bc..51bbeb2f9 100644 --- a/tests/samples/pydantic/test.ibek.ioc.yaml +++ b/tests/samples/pydantic/test.ibek.ioc.yaml @@ -12,7 +12,7 @@ entities: - type: pydantic_test.AnAsynPort name: AsynPort2 - IPpp: 10.0.0.2 + IP: 10.0.0.2 - type: pydantic_test.Consumer name: A Consumer