Skip to content
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

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

guillett
Copy link
Member

@guillett guillett commented Feb 5, 2024

Depends on #1206

New features

  • Add a look back constraint on spiral errors

@coveralls
Copy link

coveralls commented Feb 5, 2024

Coverage Status

coverage: 74.483% (-0.009%) from 74.492%
when pulling d43d73f on spiral-management
into f6f6380 on master.

count += 1
if count > self.max_spiral_loops:
break
invalidate_entries = False
Copy link
Contributor

Choose a reason for hiding this comment

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

I notice that this delta removes a comment. The comment noted that the stack was visited in reverse order; I'd assume that was a significant decision. I'm a bit worried to see this delta use a different order AND remove the comment.

"The tests still pass, so it must be OK" is an objection that I could understand, but perhaps in the context of OF-Core it's not as convincing as it would be in other contexts…

Copy link
Member Author

Choose a reason for hiding this comment

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

Thanks for your comment @Morendil. Indeed, I changed the logic because the break logic was not general enough.
In order to avoid silent regressions, I updated and improved the relevant tests in #1206.

Copy link
Member Author

Choose a reason for hiding this comment

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

I realized that the intention could be improved incrementally. I update the function code. In my opinion, it is a much clearer version.

@@ -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
Copy link
Member

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.

Copy link
Member

@bonjourmauko bonjourmauko left a comment

Choose a reason for hiding this comment

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

Thanks a lot @guillett!

This looks like a very interesting proposal. If I'm getting it right this represents a major advance in spirals logic and therefore also a breaking change.

In order to accept this pull request, as a reviewer, it will help to have a better description of how this changes the current domain logic and, for impacted users, how can they migrate in case of experiencing errors after the upgrade. Also, given it is –it seems– a breaking change, I suggest you add the corresponding new tests.

In particular, to the months lookback logic.

spiral = len(previous_periods) >= self.max_spiral_loops
if spiral:

too_many_spirals = len(previous_periods) >= self.max_spiral_loops
Copy link
Member

@bonjourmauko bonjourmauko Mar 18, 2024

Choose a reason for hiding this comment

The 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

if spiral:

too_many_spirals = 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
Copy link
Member

@bonjourmauko bonjourmauko Mar 18, 2024

Choose a reason for hiding this comment

The 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.

@@ -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
Copy link
Member

Choose a reason for hiding this comment

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

It would be great if you could instead update the documentation.

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:]:
Copy link
Member

@bonjourmauko bonjourmauko Mar 18, 2024

Choose a reason for hiding this comment

The 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).

@bonjourmauko bonjourmauko added level:advanced Requires substantial OpenFisca experience kind:feat A feature request, a feature deprecation labels Mar 18, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind:feat A feature request, a feature deprecation level:advanced Requires substantial OpenFisca experience
Projects
Development

Successfully merging this pull request may close these issues.

4 participants