Avoiding BaseModel imports in model __init__#1138
Merged
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## develop #1138 +/- ##
===========================================
- Coverage 94.39% 94.33% -0.07%
===========================================
Files 78 70 -8
Lines 7035 7029 -6
===========================================
- Hits 6641 6631 -10
- Misses 394 398 +4 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Collaborator
Author
|
@dalonsoa Does this make sense to you? It just seems simpler and less intrusive to me and I think it'll make it easier to use model components across models. |
8 tasks
dalonsoa
approved these changes
Nov 7, 2025
Collaborator
dalonsoa
left a comment
There was a problem hiding this comment.
Have a minor comment, but the change makes total sense. Not sure the original reason for the design, to be honest.
Comment on lines
+156
to
+157
| # TODO - can we retire the model_name attribute if it just duplicates the module | ||
| # name or force it to match programmatically. |
Collaborator
Author
There was a problem hiding this comment.
I'll park it for now. Again.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
As noted in #1136, we currently use a pattern where we import the
BaseModelsubclass of a model within the model__init__.py. If I recall correctly, this is simply to make it easier to find that model class - the import is not used within__init__.py(and in fact we have to mark them all to placateruffwarning us about unused imports.That has the serious downside that it makes it impossible to share components between models without importing the BaseModel subclass module, which is often complex. The end result is that importing anything from one model drags in a whole load of imports and makes circular imports very hard to avoid.
The motivating example here is that the
abioticandabiotic_simpleconfigs are extremely similar and we useabiotic_simplemethods inabiotic: we want to share these definitions, but it is finicky because of these import chains.I think it gets a whole lot simpler if we simply adjust the
register_modelfunction to look for the BaseModel in it's actual source file rather than expecting to import it from the model root namespace. That's really not much more complex.We could do this by expecting to always look for the same file name (
plants.model) etc. That would be cleaner. But for the moment, the patternplants.plants_modelworks just fine.Fixes #1136
Type of change
Key checklist
pre-commitchecks:$ pre-commit run -a$ poetry run pytestFurther checks