Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

alexhroom
Copy link
Collaborator

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:
    1. 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
    2. 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:

Project(parameters=RATapi.ClassList([Parameter(name="My Parameter"])

Project(parameters=[Parameter(name="My Parameter")])

Project(parameters=[{'name': 'My Parameter'}]

In future, once Pydantic has integrated Numpy support (pydantic/pydantic#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.

@alexhroom alexhroom linked an issue Sep 13, 2024 that may be closed by this pull request
Copy link
Collaborator

@DrPaulSharp DrPaulSharp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is really outstanding work, and will help us hugely going forwards.

I have a few comments and questions. Of particular importance is the issue regarding coercion for ClassLists - I'm afraid coercion is a many-tentacled monster.

I'd also like you to review the test coverage please. There's the particular point I've raised in test_convert.py, where there are also a few other lines missing in the coverage.

Finally, recalling the conversation we had prior to your leave about the principle of having the ClassList as a general, transferrable type, it'd be good to have tests for coercion done in test_classlist, probably within a "test_pydantic features" class. Of course, we should establish the exact behaviour we want first.

@alexhroom alexhroom requested a review from DrPaulSharp October 3, 2024 16:23
Copy link
Collaborator

@DrPaulSharp DrPaulSharp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All looks good, just a bit of tidying up to do. In addition to the specifics below, could you please review check_contrasts in project.py and make it look more like the check_layers routine. Specifically, add a docstring, add the additional line to the error message and correct the mention of layers in the error message please.

@alexhroom alexhroom requested a review from DrPaulSharp October 4, 2024 13:30
Copy link
Collaborator

@DrPaulSharp DrPaulSharp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This all looks good, make sure to merge with pride! (I think that involves playing a fanfare or something)

@alexhroom alexhroom merged commit aff6405 into RascalSoftware:main Oct 7, 2024
7 checks passed
@alexhroom alexhroom deleted the 76-nicer-classlist branch October 7, 2024 08:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add ability to save and load Project objects
2 participants