Rework nested models#47
Conversation
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
a9c5aa2 to
6b0a7dd
Compare
|
Works fine with |
f4d1023 to
cbae215
Compare
|
I would propose def parse_pydantic_model(model_cls: Type[BaseModel] | None) -> list[ParameterDescription]:
parameters = []
if model_cls is None:
return parameters
if not issubclass(model_cls, BaseModel):
raise ValueError("Not a pydantic model")
for name, field_info in model_cls.model_fields.items():
parameters.append(_parse_pydantic_field(name, field_info))
return parametersNot sure why Then we can avoid the qualname-import roundtrip input_model = task.get("input_model")
if input_model is not None:
input_model = _import_model(input_model)
inputs = parse_pydantic_model(input_model)
else:
inputs = _parse_parameter_names(
task.get("required_input_names", []),
task.get("optional_input_names", []),
)
output_model = task.get("output_model")
if output_model:
output_model = _import_model(output_model)
outputs = parse_pydantic_model(output_model)
else:
outputs = _parse_parameter_names(task.get("output_names", []), [])
submodels = extract_submodels(input_model, output_model)The def extract_submodels(*models: Type[BaseModel] | None) -> dict[str, List[TaskParameter]]:
models = tuple(m for m in models if m is not None)
if not models:
return {}
annotation = reduce(lambda a, b: a | b, models)
return {
qualname(model_cls): parse_pydantic_model(model_cls)
for model_cls in _iter_models(annotation, set())
if model_cls not in models
} |
78c3a7d to
3d9d0c7
Compare
|
Thanks for the review! I changed |
The reason I propose If you call class Shared(BaseModel):
class Model1(BaseModel):
name: str
shared: Shared
class Model2(BaseModel)
name: str
shared: Shared
class InputModel(BaseInputModel):
var: Model1
class OutputModel(BaseOutputModel):
var: Model2
When I start reading the docs I would want to know everything about Edit: even if we would prefer breadth-first instead of depth-first like we have now, we would still want only one |
I hadn't thought of that! Yes, in this case, you are right, it is best to do as you said. |
3caf39c to
58ead0a
Compare
|
@woutdenolf Should be good now! |
Fix #46