Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor sort_keys implementation #970

Merged
merged 4 commits into from
Jan 23, 2024
Merged

Refactor sort_keys implementation #970

merged 4 commits into from
Jan 23, 2024

Conversation

evetion
Copy link
Member

@evetion evetion commented Jan 19, 2024

Fixes #956

While ideally we set the sort_keys on the Schema of a TableModel, or on the TableModel themselves, this is not directly possible AFAIK:

  • Schemas are autogenerated, and sort_keys is not something we can serialize or parse in a JSONSchema
  • The TableModels are generic (TableModel[T]), so specific sort_keys can't be hardcoded to a class.

The previous approach was to hold the sort keys in a Dict[table, sort_keys] on a NodeModel (which holds multiple tables), and passing them on when save/sort was called from the Node. This PR improves on that approach by introducing a private _sort_keys on the TableModel, which is always set (from an empty list by default) on initialization from the NodeModel.

Instead of keeping a separate Dict with sort_keys on a NodeModel, we now set them in the Field attribute json_schema_extra (the only customizable attribute of a Field) for each field. Each field of a NodeModel is now validated by default (also on assignment), and if a sort_keys is present for a fieldtype, it is set on the TableModel.

Based on the initial exploration by @deltamarnix in https://github.com/Deltares/Ribasim/tree/feat/sort-keys-in-tablemodel

@evetion evetion requested a review from deltamarnix January 19, 2024 09:10
Copy link
Contributor

@deltamarnix deltamarnix left a comment

Choose a reason for hiding this comment

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

The code itself looks good, I would like to see some more error handling and documentation on the decisions that were made so that it will be in the git history

python/ribasim/ribasim/input_base.py Outdated Show resolved Hide resolved
python/ribasim/ribasim/input_base.py Show resolved Hide resolved
python/ribasim/ribasim/input_base.py Show resolved Hide resolved
Copy link

Quality Gate Passed Quality Gate passed

Kudos, no new issues were introduced!

0 New issues
0 Security Hotspots
No data about Coverage
0.0% Duplication on New Code

See analysis details on SonarCloud

@evetion evetion requested a review from deltamarnix January 19, 2024 13:57
@classmethod
def set_sort_keys(cls, v: Any, info: ValidationInfo) -> Any:
"""Set sort keys for all TableModels if present in FieldInfo."""
if isinstance(v, (TableModel,)):
Copy link
Contributor

Choose a reason for hiding this comment

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

Because of the nesting I would prefer some early returns here instead.

Copy link
Contributor

@deltamarnix deltamarnix left a comment

Choose a reason for hiding this comment

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

I like the solution. Making use of the schema to set the information. I would love to eventually see this coming from the generated code instead.

@visr visr merged commit eb7f512 into main Jan 23, 2024
19 checks passed
@visr visr deleted the feat/sort-keys branch January 23, 2024 09:07
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.

Sort keys in Ribasim python should should be moved to TableModel
3 participants