Replies: 1 comment 1 reply
-
That's neat. Does the first option just create |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Don't panic! It is much simpler than it looks :)
At the moment, to create a new model, you need to derive from the
BaseModel
class, ensure that there are a few attributes present (up to 4 after #211 ), validate that those attributes exist in the child class and that they fulfil certain conditions. It all works and makes sense.However, it has always looked me as a bit convoluted, in particular the part about requiring the presence of the attributes and the check that they are indeed properties. I'm here to suggest an, I think, cleaner approach exploiting once more the
__init_subclass__
method.The idea would be to require the presence of certain keyword arguments directly when subclassing, and validate those before assigning them to the class attributes. This would be a simple example:
This enforces the
Child
class to be created with appropriate values fora
andb
without having to define any properties in the subclass. You just need to check if the values passed are valid - eg. if they are within certain bounds or something like that - but their presence is guarantee by design of the__init_subclass__
definition.In summary, checks like these become unnecessary: https://github.com/ImperialCollegeLondon/virtual_rainforest/blob/5a821952bdb25a044b135532f9f7ad7c161ffebf/virtual_rainforest/core/base_model.py#L304
And if you really, really want them to be
properties
, as you have now, you just make it so in theBaseModel
class directly, saving code everywhere else:Anyway, just an idea, and certainly not urgent.
Beta Was this translation helpful? Give feedback.
All reactions