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 BaseModel #365

Merged
merged 6 commits into from
Jan 19, 2024
Merged

Refactor BaseModel #365

merged 6 commits into from
Jan 19, 2024

Conversation

davidorme
Copy link
Collaborator

Description

This implements @dalonsoa's suggested refactor of BaseModel from #363. We thought this seemed like a good idea at the Github meeting so I've worked it through.

Briefly, that changes how we set model class attributes from:

class NewModel(BaseModel):
     model_name="base_model"
     ...

to

class NewModel(
     BaseModel,
     model_name="base_model"
):
     ...

Another change that fell out of this update is that it looked much cleaner to have a single model_update_bounds attribute set like ('1 day', '1 month') rather than separating those as lower_bound_on_time_scale and upper_bound_on_time_scale. It means _check_time_bounds_units is replaced by _check_model_update_bounds, which is only called once and also moves the test that 1 day < 1 month inside the check function.

Pros on the switch

  • The BaseModel code becomes much simpler. Before we had a bunch of properties that might or might not be set on a new model and had to test whether they'd been set or not and then what they were. Now they are explicitly set when __init__subclass__ runs.
  • Model definition becomes more robust. Now these values are arguments to the class creation signature, users get all the usual warnings about forgotten arguments and typing.
  • The testing code becomes much cleaner. Before, we were using exec() to run chunks of text that described model definitions in order to set different attributes for testing. We can now just pass those attributes in to tests as kwargs to the model creation call.

Cons on the switch

  • We now don't have an obvious place to put model specific docstrings on the attribute. For example, I had a docstring on PlantsModel.required_init_vars that gave an overview. I've moved that into a specific section in the model docstring instead. (Honestly I'm not sure this is really a con - that seems like a more logical place for people to read about model requirements).

Fixes #363 (issue)

Type of change

  • New feature (non-breaking change which adds functionality)
  • Optimization (back-end change that speeds up the code)
  • Bug fix (non-breaking change which fixes an issue)

Key checklist

  • Make sure you've run the pre-commit checks: $ pre-commit run -a
  • All tests pass: $ poetry run pytest

Further checks

  • Code is commented, particularly in hard-to-understand areas
  • Tests added that prove fix is effective or that feature works

@davidorme davidorme linked an issue Jan 19, 2024 that may be closed by this pull request
@davidorme davidorme requested review from jacobcook1995, alexdewar, dalonsoa and vgro and removed request for alexdewar January 19, 2024 11:35
@codecov-commenter
Copy link

codecov-commenter commented Jan 19, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (dd41f74) 93.50% compared to head (c1d9d8d) 93.30%.

Additional details and impacted files
@@             Coverage Diff             @@
##           develop     #365      +/-   ##
===========================================
- Coverage    93.50%   93.30%   -0.20%     
===========================================
  Files           59       59              
  Lines         2954     2866      -88     
===========================================
- Hits          2762     2674      -88     
  Misses         192      192              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Collaborator

@dalonsoa dalonsoa left a comment

Choose a reason for hiding this comment

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

What a great idea XDDD

Jokes aside, the code now looks much cleaner and I feel it will be more robust. Great work!

Copy link
Collaborator

@jacobcook1995 jacobcook1995 left a comment

Choose a reason for hiding this comment

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

LGTM!

Copy link
Collaborator

@vgro vgro left a comment

Choose a reason for hiding this comment

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

LGTM :-)

@davidorme davidorme merged commit e9365f0 into develop Jan 19, 2024
16 checks passed
@davidorme davidorme deleted the 363-refactor-basemodel-class branch January 19, 2024 16:31
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.

Refactor BaseModel class
5 participants