This PR solves #76 by allowing Projects to be read and written from JSON via two new functions, project_to_json and project_from_json.
Changes
ClassList is now a Generic class, improving compatibility with static type checkers (e.g. you can now write ClassList[Parameter], and type checking will know a priori that the ClassList should have Parameters in it)
Added a Pydantic schema for ClassLists, which means that Pydantic can validate them automatically. The type of the ClassList is specified using square brackets, e.g. a ClassList of strings would be ClassList[str]. This schema does the following:
If the input to the model is a list, attempt to coerce it to a ClassList, and raise a ValidationError if the list types are not homogeneous
Validate ClassList[T].data as though it were a list[T] type, i.e. ensure that all entries in the ClassList are the type expected by the model.
This means that Pydantic integrates a lot better with the ClassList type, with improvements such as:
As mentioned above, a list can now be passed to the Project rather than a ClassList and it will be converted automatically
The parameters inside a ClassList can be given as dictionaries
If an item in a ClassList is the wrong class, Pydantic will highlight which specific item is wrong, rather than just saying that the ClassList is bad
For example, the following are now all valid inputs to a Project:
In future, once Pydantic has integrated Numpy support (https://github.com/pydantic/pydantic/issues/9677), this will be good enough for us to use Pydantic's model_dump_json to save Projects in JSON format (via converting each ClassList of RATModels to a list of dictionaries, as it now knows how to get back from that). But for now:
Adds methods convert_to_json and convert_from_json which convert a Project to and from JSON format, with workarounds to handle ClassLists and the Numpy array in the Data model.
This PR solves #76 by allowing
Project
s to be read and written from JSON via two new functions,project_to_json
andproject_from_json
.Changes
ClassList
is now aGeneric
class, improving compatibility with static type checkers (e.g. you can now writeClassList[Parameter]
, and type checking will know a priori that theClassList
should haveParameter
s in it)ClassList
s, which means that Pydantic can validate them automatically. The type of theClassList
is specified using square brackets, e.g. aClassList
of strings would beClassList[str]
. This schema does the following:ClassList
, and raise aValidationError
if the list types are not homogeneousClassList[T].data
as though it were alist[T]
type, i.e. ensure that all entries in theClassList
are the type expected by the model.Project
rather than aClassList
and it will be converted automaticallyClassList
can be given as dictionariesFor example, the following are now all valid inputs to a Project:
In future, once Pydantic has integrated Numpy support (https://github.com/pydantic/pydantic/issues/9677), this will be good enough for us to use Pydantic's
model_dump_json
to saveProject
s in JSON format (via converting eachClassList
ofRATModel
s to a list of dictionaries, as it now knows how to get back from that). But for now:convert_to_json
andconvert_from_json
which convert aProject
to and from JSON format, with workarounds to handleClassList
s and the Numpy array in theData
model.