From 0ac58166c48e037df3344341aaf4fa59f42e0b56 Mon Sep 17 00:00:00 2001 From: Nikhil Woodruff <35577657+nikhilwoodruff@users.noreply.github.com> Date: Thu, 8 Feb 2024 14:32:20 +0000 Subject: [PATCH] Add variable inheritance in declarations (#155) * Format and improve error handling * Allow inheritance in variable declarations Fixes #154 * Changelog entry --- changelog_entry.yaml | 4 ++++ policyengine_core/variables/variable.py | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/changelog_entry.yaml b/changelog_entry.yaml index e69de29bb..08abda926 100644 --- a/changelog_entry.yaml +++ b/changelog_entry.yaml @@ -0,0 +1,4 @@ +- bump: minor + changes: + added: + - Variable inheritance in declarations. diff --git a/policyengine_core/variables/variable.py b/policyengine_core/variables/variable.py index 8fa44435e..0d02ffa34 100644 --- a/policyengine_core/variables/variable.py +++ b/policyengine_core/variables/variable.py @@ -136,6 +136,21 @@ def __init__(self, baseline_variable=None): for name, value in self.__class__.__dict__.items() if not name.startswith("__") } + + # Allow inheritance for some properties + INHERITED_ALLOWED_PROPERTIES = ( + "label", + "value_type", + "entity", + "definition_period", + ) + + for property_name in INHERITED_ALLOWED_PROPERTIES: + if not attr.get(property_name) and property_name in dir( + self.__class__ + ): + attr[property_name] = getattr(self, property_name) + self.baseline_variable = baseline_variable self.value_type = self.set( attr,