Skip to content

Commit

Permalink
fix pydantic test broken yaml file
Browse files Browse the repository at this point in the history
  • Loading branch information
gilesknap committed Jul 17, 2023
1 parent 479c330 commit b44b119
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 13 deletions.
38 changes: 26 additions & 12 deletions examples/test_refs3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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:
Expand All @@ -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"},
]
}
)
Expand All @@ -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"},
]
}
)
2 changes: 1 addition & 1 deletion tests/samples/pydantic/test.ibek.ioc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b44b119

Please sign in to comment.