diff --git a/changelog_entry.yaml b/changelog_entry.yaml index e69de29b..9f82f639 100644 --- a/changelog_entry.yaml +++ b/changelog_entry.yaml @@ -0,0 +1,5 @@ +- bump: patch + changes: + changed: + - Ensure that every instance of 'Simulation' class has 'dataset' attribute, even if equal to 'None' + - Add better safeguards to 'check_macro_cache' method of 'Simulation' class \ No newline at end of file diff --git a/policyengine_core/simulations/simulation.py b/policyengine_core/simulations/simulation.py index c10ba3e2..d3d90b84 100644 --- a/policyengine_core/simulations/simulation.py +++ b/policyengine_core/simulations/simulation.py @@ -102,6 +102,7 @@ def __init__( self.reform = reform self.tax_benefit_system = tax_benefit_system self.branch_name = "default" + self.dataset = dataset if dataset is None: if self.default_dataset is not None: @@ -607,7 +608,7 @@ def _calculate( smc = SimulationMacroCache(self.tax_benefit_system) - # Check if cache could be used, if available, check if path exists + # Check if cache can be used, if available, check if path exists is_cache_available = self.check_macro_cache(variable_name, str(period)) if is_cache_available: smc.set_cache_path( @@ -1418,9 +1419,18 @@ def check_macro_cache(self, variable_name: str, period: str) -> bool: """ Check if the variable is able to have cached value """ - if hasattr(self, "dataset"): - if self.dataset.data_format == Dataset.FLAT_FILE: - return False + + # Dataset should always exist, but just in case + if not hasattr(self, "dataset"): + return False + + # If no dataset, no need to cache + if self.dataset is None: + return False + + # If using a flat file dataset, we're unable to cache + if self.dataset.data_format == Dataset.FLAT_FILE: + return False if self.is_over_dataset: return True