When talking with @mstechly we identified several possible improvements to interacting with QREF's pydantic models, namely:
We are using lists instead of dictionaries for objects like children and ports. While this is well justified and already described in the docs, it might be beneficial to be able to obtain child or a port by its name instead.
Our pydantic models are not validating assignments (this is pydantic's default behaviour). However, turning assignment validation on might also be beneficial in scenarios like creating demo notebooks, where one might want to overwrite e.g. resources with some new list of resources.
This PR implements both of the improvements described above.
Caveat
Only top level assignments are validated. I.e. this is validated:
routine.resources = [{...}, {...}]
and this is not validated:
routine.resources[0] = {...}
Pydantic can only do the first one, and for good reason - if routine.resources is a list, it cannot override how its items are accessed. We could do this ourselves by using e.g. RootModel to wrap lists, but it seems like a lot of code which wouldn't add that much value IMO.
Please verify that you have completed the following steps
[x] I have self-reviewed my code.
[x] I have included test cases validating introduced feature/fix.
Description
When talking with @mstechly we identified several possible improvements to interacting with QREF's pydantic models, namely:
This PR implements both of the improvements described above.
Caveat Only top level assignments are validated. I.e. this is validated:
and this is not validated:
Pydantic can only do the first one, and for good reason - if
routine.resources
is a list, it cannot override how its items are accessed. We could do this ourselves by using e.g.RootModel
to wrap lists, but it seems like a lot of code which wouldn't add that much value IMO.Please verify that you have completed the following steps