-
Notifications
You must be signed in to change notification settings - Fork 81
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add a look back constraint on spiral errors #1205
base: master
Are you sure you want to change the base?
Changes from all commits
fbcb28f
7c6eba3
32a5b42
f1dc190
a20efed
83004f1
456c01b
b093bd9
2c2ba93
272b140
0c13839
ab478d6
d43d73f
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -48,6 +48,8 @@ def __init__( | |
|
||
# controls the spirals detection; check for performance impact if > 1 | ||
self.max_spiral_loops: int = 1 | ||
self.max_spiral_lookback_months: int = 0 | ||
|
||
self.memory_config = None | ||
self._data_storage_dir = None | ||
|
||
|
@@ -400,8 +402,10 @@ def _check_for_cycle(self, variable: str, period): | |
raise errors.CycleError( | ||
"Circular definition detected on formula {}@{}".format(variable, period) | ||
) | ||
spiral = len(previous_periods) >= self.max_spiral_loops | ||
if spiral: | ||
|
||
too_many_spirals = len(previous_periods) >= self.max_spiral_loops | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. May I propose to give it another name and to extract it to a method? As it is a core domain rule that we're introducing here, it makes sense to me to be able to test it in isolation. Just an idea: def spiral_loops_within_threshold(previous_periods: list[Period | str]) -> bool:
return len(previous_periods) < self.max_spiral_loops |
||
too_backward = (previous_periods[0].date - period.date).in_months() > self.max_spiral_lookback_months if previous_periods and self.max_spiral_lookback_months > 0 else False | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't understand how this new domain rule relates to the previous one. Is it a specific check just for months that overrides the previous one? If it is, it makes all sense for me to allow contry package developers set thresholds for the period types that concern them independently (weeks, months, years...). Beyond that, it would be great, as with the previous one, to extract it to a method to give it more visibility. |
||
if too_many_spirals or too_backward: | ||
self.invalidate_spiral_variables(variable) | ||
message = "Quasicircular definition detected on formula {}@{} involving {}".format( | ||
variable, period, self.tracer.stack | ||
|
@@ -412,17 +416,9 @@ def invalidate_cache_entry(self, variable: str, period): | |
self.invalidated_caches.add(Cache(variable, period)) | ||
|
||
def invalidate_spiral_variables(self, variable: str): | ||
# Visit the stack, from the bottom (most recent) up; we know that we'll find | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be great if you could instead update the documentation. |
||
# the variable implicated in the spiral (max_spiral_loops+1) times; we keep the | ||
# intermediate values computed (to avoid impacting performance) but we mark them | ||
# for deletion from the cache once the calculation ends. | ||
count = 0 | ||
for frame in reversed(self.tracer.stack): | ||
first_variable_occurrence = next(index for (index, frame) in enumerate(self.tracer.stack) if frame["name"] == variable) | ||
for frame in self.tracer.stack[first_variable_occurrence:]: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I didn't quite get what this line changes compared to the previous one. Is it just a refactoring or the underlying logic is changed as well? (As this is already tested, I presume there is no change in logic here specifically. However, as you introduce a new constraint a couple of lines above, I'm not 100% sure). |
||
self.invalidate_cache_entry(str(frame["name"]), frame["period"]) | ||
if frame["name"] == variable: | ||
count += 1 | ||
if count > self.max_spiral_loops: | ||
break | ||
|
||
# ----- Methods to access stored values ----- # | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,7 +67,7 @@ | |
|
||
setup( | ||
name="OpenFisca-Core", | ||
version="41.4.2", | ||
version="41.5.0", | ||
author="OpenFisca Team", | ||
author_email="[email protected]", | ||
classifiers=[ | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What happens with days, weeks, years? Shouldn't we also cover for these? I'm still trying to grasp the desired behaviour in terms of the domain logic.