Skip to content

Avoiding BaseModel imports in model __init__#1138

Merged
davidorme merged 4 commits intodevelopfrom
1136-simplify-model-registration-structure
Nov 7, 2025
Merged

Avoiding BaseModel imports in model __init__#1138
davidorme merged 4 commits intodevelopfrom
1136-simplify-model-registration-structure

Conversation

@davidorme
Copy link
Copy Markdown
Collaborator

As noted in #1136, we currently use a pattern where we import the BaseModel subclass 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 placate ruff warning 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 abiotic and abiotic_simple configs are extremely similar and we use abiotic_simple methods in abiotic: 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_model function 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 pattern plants.plants_model works just fine.

Fixes #1136

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
  • Relevant documentation reviewed and updated

@davidorme davidorme linked an issue Nov 6, 2025 that may be closed by this pull request
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Nov 6, 2025

Codecov Report

❌ Patch coverage is 85.18519% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 94.33%. Comparing base (4e4a691) to head (e95f355).
⚠️ Report is 821 commits behind head on develop.

Files with missing lines Patch % Lines
virtual_ecosystem/core/registry.py 85.18% 4 Missing ⚠️
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.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@davidorme davidorme requested a review from dalonsoa November 6, 2025 14:51
@davidorme
Copy link
Copy Markdown
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.

Copy link
Copy Markdown
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.

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.
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Probably.

Copy link
Copy Markdown
Collaborator Author

@davidorme davidorme Nov 7, 2025

Choose a reason for hiding this comment

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

I'll park it for now. Again.

@davidorme davidorme merged commit 1a0ea4b into develop Nov 7, 2025
13 checks passed
@davidorme davidorme deleted the 1136-simplify-model-registration-structure branch November 7, 2025 09:59
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.

Simplify model registration structure.

3 participants